cs3251 UNIT II QUESTION BANK
cs3251 UNIT II QUESTION BANK
cs3251 UNIT II QUESTION BANK
PART A
1. Write a C program to find mean of n numbers using one dimensional array. AP
Ans: #include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
printf("%d. Enter a number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f\n", avg);
return 0;
}
2. Point out an example code to initialize values for a two dimensional array. AP
Ans : #include <stdio.h>
int main() {
// Directly initializing a 2D array at the time of declaration
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
}
10. Write a C function to reverse array of characters in a string using strrev() function.
AP
Ans: #include <stdio.h>
#include <string.h>
101 102 103 104 105 106 107 108 109 110
arr[0]
13.
lower bound upper
bound
length of an array : upper bound – lower bound +1
= 9-0+1
=10
14. 15.Write a function call to the calculateArea function, passing arguments 5.0 and 3.0 for
length and width respectively, and print the result.
AP
Ans: #include <stdio.h>
void FindArea(float length, float width) {
float area = length * width;
printf("Area of the rectangle is: %f units\n", area);
}
int main() {
float length = 5.0;
float width = 3.0;
FindArea(length, width);
return 0;
}
16. Write a program that utilizes the sqrt function from the <math.h> library to calculate the
square root of a user-inputted number.
AP
Ans: #include <stdio.h>
#include <math.h>
int main() {
17. double num, root;
printf("Enter any number to find the square root: ");
scanf("%lf", &num);
root = sqrt(num);
printf("Square root of %.2lf = %.2lf\n", num, root);
return 0;
}
18. What is the useof „\0‟ character? U
When declaring character arrays (strings), „\0‟ (NULL) character is automatically added at
19.end.
#include <stdio.h>main(){
printf(“%s\n”, name1);
printf(“%s\n”, name2);}
#include<stdio.h>#include<conio.h>void main(){int
24. List the different methods for reading and writing a string. U
4. strcmp(s1,s2)Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
28. Write a C program to Read & write Strings in C using Printf() and Scanf() functions AP
PART B
1 Write C program
To perform string copy, string concatenation,
AP
and string length given s1= ”COMPUTER” s2 = “SCIENCE”
2 to perform matrix addition using two dimensional array.
AP
3 Write a C program
(i) to perform addition operation by passing arguments and return values (8)
AP