Computer Science Assignment
Computer Science Assignment
1a > b is 0
2a < b is 1
3a == b is 0
4a != b is 1
5a >= b is 0
6a <= b is 1
Logical operators are used to combine two or more relational
expressions and determine if they are true or false. Here are
some examples:
1#include <stdio.h>
2int main() {
3 int a = 5, b = 10, c = 20;
4 printf("(a > b) && (b > c) is %d \n", (a > b) && (b > c));
5 printf("(a < b) || (b < c) is %d \n", (a < b) || (b < c));
6 printf("!(a > b) is %d \n", !(a > b));
7 return 0;
8}
Output:
1(a > b) && (b > c) is 0
2(a < b) || (b < c) is 1
3!(a > b) is 1
b) Int main() and Void main():
The difference between int main() and void main() lies in the
return type of the main function. int main() returns an integer
value to the operating system after the program has nished
executing, while void main() does not return any value. Here are
some examples:
c
// int main()
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
#include <stdio.h>
void main() {
printf("Hello World!\n");
// No return statement required
}
c) Variable and constant:
Variables are used to store values that can change during the
execution of a program, while constants are used to store values
that cannot be changed. Here is an example:
#include <stdio.h>
int main() {
int variable = 5; // Variable
const int constant = 10;
printf("Variable value: %d \n", variable);
fi
printf("Constant value: %d \n", constant);
variable = 15;
1#include <stdio.h>
2int main()
3{
4 int a = 5, b = 10, c;
5
6 c = a + b;
7 printf("a + b = %d \n", c);
8 c = a - b;
9 printf("a - b = %d \n", c);
10 c = a * b;
11 printf("a * b = %d \n", c);
12 c = b / a;
13 printf("b / a = %d \n", c);
14 c = b % a;
15 printf("Remainder when b divided by a = %d \n", c);
16 c = -a;
17 printf("Unary minus of a = %d \n", c);
18
19 return 0;
20}
Output:
Relational Operators: Relational operators are used to compare
two values. They return a boolean value indicating whether the
comparison is true or false. Here's an example:
1#include <stdio.h>
2int main()
3{
4 int a = 5, b = 10;
5
6 printf("5 == 5 is %d \n", a == b);
7 printf("5 == 10 is %d \n", a == b);
8 printf("5 != 10 is %d \n", a != b);
9 printf("5 < 10 is %d \n", a < b);
10 printf("5 > 10 is %d \n", a > b);
11 printf("5 <= 10 is %d \n", a <= b);
12 printf("5 >= 10 is %d \n", a >= b);
13
14 return 0;
15}
1#include <stdio.h>
2
3int main() {
4 int score;
5
6 printf("Enter your score: ");
7 scanf("%d", &score);
8
9 if (score >= 90) {
10 printf("Grade A\n");
11 } else if (score >= 80) {
12 printf("Grade B\n");
13 } else if (score >= 70) {
14 printf("Grade C\n");
15 } else if (score >= 60) {
16 printf("Grade D\n");
17 } else {
18 printf("Grade F\n");
19 }
20
21 return 0;
22}
For example , the user enters a score, and the program checks if
the score is greater than or equal to 90, 80, 70, 60, and nally, if
none of the conditions are met, it defaults to grade F.
The switch statement is an alternative to the if-else-if ladder. It is
used when you want to execute a block of code based on the
value of a single variable or expression. example:
1#include <stdio.h>
2
3int main() {
4 int day;
5
6 printf("Enter a number from 1 to 7: ");
7 scanf("%d", &day);
8
9 switch (day) {
10 case 1:
11 printf("Monday\n");
12 break;
13 case 2:
14 printf("Tuesday\n");
15 break;
16 case 3:
17 printf("Wednesday\n");
18 break;
fi
19 case 4:
20 printf("Thursday\n");
21 break;
22 case 5:
23 printf("Friday\n");
24 break;
25 case 6:
26 printf("Saturday\n");
27 break;
28 case 7:
29 printf("Sunday\n");
30 break;
31 default:
32 printf("Invalid day number\n");
33 break;
34 }
35
36 return 0;
37}
In this example, the user enters a number from 1 to 7, and the
program checks the value of the variable 'day' and prints the
corresponding day of the week.
Recursion is a programming technique where a function calls
itself repeatedly until it meets a certain condition. Here's an
example of a recursive function in C that calculates the factorial of
a number
1#include <stdio.h>
5b)
A recursive function is a function in C that calls itself repeatedly
until it meets a certain condition. Recursion is a powerful
programming technique that simpli es the solution to complex
problems. Here's an example of a recursive function in C that
calculates the factorial of a number entered by the user:
1#include <stdio.h>
2
3int factorial(int n);
4
5int main() {
6 int num;
7
8 printf("Enter a positive integer: ");
9 scanf("%d", &num);
10
11 printf("Factorial of %d is: %d\n", num, factorial(num));
12
13 return 0;
14}
15
16int factorial(int n) {
17 if (n == 0) {
fi
fi
18 return 1;
19 } else {
20 return n * factorial(n - 1);
21 }
22}
In this example, the factorial function calls itself repeatedly,
decrementing the value of 'n' until it reaches 0, at which point it
returns 1, and the recursion stops. The nal result is the factorial
of the number entered by the user.
The factorial function uses an if-else statement to check the value
of 'n'. If 'n' is 0, the function returns 1. Otherwise, it calls itself with
the argument n-1 and multiplies the result by 'n'. This process
continues until 'n' is 0, at which point the recursion stops and the
nal result is returned.
The main function prompts the user to enter a positive integer,
reads the input, and calls the factorial function to calculate the
factorial of the entered number. The result is then printed to the
console.
1struct Student {
2 char name[50];
3 int id;
4 oat gpa;
5};
To initialize a structure variable,
c
fi
fl
fi
fi
fi
1struct Student s1;
2s1.id = 12345;
3strcpy(s1.name, "John Smith");
4s1.gpa = 3.5;
Alternatively, you can initialize a structure variable at the time of
declaration:
1#include <iostream>
2using namespace std;
3int main()
4{
5 int* largest;
6 int a[5]={4,5,666,7,8};
7 largest=a;
8 for(int i=1; i<5; i++)
9 {
10 if(*largest < a[i])
11 {
12 largest = &a[i];
13 }
14 }
15 cout<<*largest;
16}
1#include <iostream>
2)Int main(){
4 int arr[] = {5, 3, 8, 1, 9};
5 int* largest = arr; array
6 int* current;
7
8 for (current = arr + 1; current < arr + 5; current++) {
9 if (*current > *largest) {
10 largest = current;
11 }
12 }
13
14 printf("The largest number in the array is %d\n", *largest);
15
16 return 0;
17}
1int arr[5]; // an array with one row and ve columns will be created.
You can initialize a one-dimensional array at the time of
declaration or explicitly at runtime. example of initializing a one-
dimensional array at the time of declaration:
1#include <stdio.h>
2
3int main() {
4 int arr[5] = {1, 2, 3, 4, 5};
5 int sum = 0;
6 for(int i = 0; i < 5; i++) {
7 sum += arr[i];
8 }
9 printf("The sum is: %d\n", sum);
10 return 0;
11}
Two-dimensional arrays, also known as multi-dimensional arrays,
are used to store a collection of elements of the same data type in
a table format. Here's an example of a two-dimensional array:
Edit
1int arr[2][5];
You can initialize a two-dimensional array at the time of
declaration or explicitly at runtime. example of initializing a two-
dimensional array at the time of declaration:
1int arr[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
example of initializing a two-dimensional array at runtime
1int arr[2][5];
2arr[0][0] = 1;
3arr[0][1] = 2;
4arr[0][2] = 3;
5arr[0][3] = 4;
6arr[0][4] = 5;
7arr[1][0] = 6;
8arr[1][1] = 7;
9arr[1][2] = 8;
10arr[1][3] = 9;
11arr[1][4] = 10
7b)#include <stdio.h>
2
3int main() {
4 int m, n, i, j;
5 printf("Enter the number of rows and columns of the matrices: ");
6 scanf("%d %d", &m, &n);
7
8 double mat1[m][n];
9 printf("\nEnter the elements of the rst matrix:\n");
10 for (i = 0; i < m; i++) {
11 for (j = 0; j < n; j++) {
12 printf("Enter element a%d%d: ", i + 1, j + 1);
13 scanf("%lf", &mat1[i][j]);
14 }
15 }
16
17 double mat2[m][n];
18 printf("\nEnter the elements of the second matrix:\n");
19 for (i = 0; i < m; i++) {
20 for (j = 0; j < n; j++) {
21 printf("Enter element b%d%d: ", i + 1, j + 1);
fi
22 scanf("%lf", &mat2[i][j]);
23 }
24 }
25
26 double sum[m][n];
27 for (i = 0; i < m; i++) {
28 for (j = 0; j < n; j++) {
29 sum[i][j] = mat1[i][j] + mat2[i][j];
30 }
31 }
32
33 printf("\nThe sum of the matrices is:\n");
34 for (i = 0; i < m; i++) {
35 for (j = 0; j < n; j++) {
36 printf("%.2lf\t", sum[i][j]);
37 }
38 printf("\n");
39 }
40
41 return 0;
42}
8) switch case
The switch statement is a control statement in C that allows you to
execute different blocks of code based on the value of an
expression. It is often used as an alternative to the if-else
statement for making decisions based on a set of discrete values.
Here is an example program that prints the day of the week based
on user input using a switch statement:
1#include <stdio.h>
2
3int main() {
4 int day;
6 printf("Enter a number for a day of the week (1-7): ");
7 scanf("%d", &day);
8
9 switch (day) {
10 case 1:
11 printf("Sunday\n");
12 break;
13 case 2:
14 printf("Monday\n");
15 break;
16 case 3:
17 printf("Tuesday\n");
18 break;
19 case 4:
20 printf("Wednesday\n");
21 break;
22 case 5:
23 printf("Thursday\n");
24 break;
25 case 6:
26 printf("Friday\n");
27 break;
28 case 7:
29 printf("Saturday\n");
30 break;
31 default:
32 printf("Invalid input! Please enter a number between 1 and
7.\n");
33 }
34
35 return 0;
36}
9a)Create a structure called "Student" with members name, age, and
total marks. Write a C program to input data for two students, display
their information, and nd the average of total marks.
fi
1struct Student { 5 char name[50];
2 int age;
7 oat totalMarks;
8};
10int main() {
11
12 struct Student student1, student2;
15 printf("Input details for Student 1:\n");
16 printf("Name: ");
17 scanf("%s", student1 :\n”);
18 printf("Age: ");
19 scanf("%d", &student1.age);
20 printf("Total Marks: ");
21 scanf("%f", &student1.totalMarks);
22
33 printf("\nStudent 1 Information:\n");
34 printf("Name: %s\n", student1.name);
35 printf("Age: %d\n", student1.age);
36 printf("Total Marks: %.2f\n", student1.totalMarks);
fl
37
38 printf("\nStudent 2 Information:\n");
39 printf("Name: %s\n", student2.name);
40 printf("Age: %d\n", student2.age);
41 printf("Total Marks: %.2f\n", student2.totalMarks);
42 oat averageMarks = (student1.totalMarks + student2.totalMarks) / 2;
43 printf("\nAverage Total Marks: %.2f\n", averageMarks);
44return 0;
#include <stdio.h>
2
3// Function to nd the second largest element in an array
4void secondLargest(int arr[], int n) {
5 int i, rst, second;
6 if (n < 2) {
7 printf("Invalid array size!\n");
8 return;
9 }
10 rst = second = arr[0];
11 for (i = 1; i < n; i++) {
12 if (arr[i] > rst) {
13 second = rst;
14 rst = arr[i];
15 } else if (arr[i] > second && arr[i] != rst) {
16 second = arr[i];
17 }
18 }
19 printf("The second largest element is %d\n", second);
20}
21
22int main() {
23 int arr[] = {12, 34, 10, 6, 40};
24 int n = sizeof(arr) / sizeof(arr[0]);
25 secondLargest(arr, n);
26 return 0;
27}
fi
fi
fi
fi
fi
fi
fi
fi
d)Explain fopen, fread, fwrite, fseek
1#include <stdio.h>
2
3int main(int argc, char *argv[]) {
4 int i;
5 printf("Number of arguments: %d\n", argc);
6 for (i = 0; i < argc; i++)
7 printf("Argument %d: %s\n", i, argv[i]);
8 return 0;
9}
1#include <stdio.h>
2
3int main() {
4 int i;
fi
5 for(i = 1; i <= 10; i++) {
6 if(i % 2 == 0)
7 continue;
8 printf("%d ", i);
9 }
10 return 0;
11}
1#include <stdio.h>
2
3// Function prototype
4int add(int a, int b);
5
fi
fi
fi
6int main() {
7 int x = 10, y = 20, sum;
8 sum = add(x, y);
9 printf("The sum is: %d\n", sum);
10 return 0;
11}
12
13// Function definition
14int add(int a, int b) {
15 return a + b;
16}
D)Differentiate between Structure and Union.
A structure is a collection of variables of different data types, where each variable has
its own memory allocation. This means that all members of a structure get their own
block of memory.
On the other hand, a union is a data type in C programming that allows storing different
data types in the same memory location. It provides an ef cient way of using the same
memory location for multiple purposes. However, only one member can contain a value
at any given time.
|
Perform the following
A) (3041.125)10=(5741.1)8= (be1.2)16=(101111100001.001)2
B) (A010D)16=(50015)8=(655629)10=(101000000001101)2
fi