Be First Year Engineering Semester 2 2022 December C Programming Rev 2019c Scheme
Be First Year Engineering Semester 2 2022 December C Programming Rev 2019c Scheme
Q. P. CODE: 13334
Q.1. (15
marks)
(5marks)
A flowchart is graphical representation of the algorithm of a
program. The flowchart gives a nice idea of the flow of the
algorithm in sequence as well as the condition changes in the
sequence.
MUquestionpapers.com 1
1. pow( )
This function is available in the header file “math.h”. This
function calculates and returns the value of x y. The
parameters x and y are to be passed to it. The parameters as
well the answer returned are double data type. But it can
also accept parameters of different data type.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
int x = 7; // base
double y = 5.2; // power
Output:
24803.319527
2. ceil( )
This function returns the smallest integral value of the
parameter passed to the function. It rounds the value of x
upward and returns the value. This function is also in the
header file math.h. The parameter accepted and returned of
the data type double.
Code
#include <stdio.h>
#include <math.h>
int main()
MUquestionpapers.com 2
{
double num = 8.33;
int result;
result = ceil(num);
printf("Ceiling integer of %.2f = %d", num, result);
return 0;}
Output
Ceiling integer of 7.56 = 8
3. floor( )
This function returns the largest integral value of the
parameter passed to the function. It rounds the value of x
downward and returns the value. This function is also in the
header file math.h. The parameter accepted and returned is
of double data type.
Code
#include <stdio.h>
#include <math.h>
int main()
{
double num = -4.56;
double result = floor(num);
Output
Floor integer of -4.56 =-5
4. sqrt( )
This function finds the square root of the parameter passed
to it and the result is returned to the caller function. It is
available in math.h. This function also accepts a double as
input parameter and returns the result also of double data
type.
Code
#include <math.h>
MUquestionpapers.com 3
#include <stdio.h>
int main()
{
double number, squareRoot;
printf("Enter a number: ");
scanf("%lf", &number);
return 0;
}
Output
Enter a number: 5
Square root of 5 = 2.24
MUquestionpapers.com 4
}
printf(“\n”);
}
Output:
Enter the number of rows: 4
1
23
456
7 8 9 10
D. Differentiate between structure and union.
(5marks)
Sr. Structure Union
No.
1. Memory allotted for a Memory allotted for a
structure is equal to the union is equal to the space
space required collectively by required by the largest
all the members of that member of that union.
structure.
2. Data is more secure in Data can be corrupted in a
structures. union.
3. Structure provides ease of Unions are comparatively
programming. difficult for programming.
4. Structures require more Union requires less
memory. memory.
5. Structure must be used when Unions must to be used
information of all the when only one of the
member elements of member elements of the
structure is to be stored. union is to be stored.
MUquestionpapers.com 5
Example: Program to find greatest of three numbers
#include <stdio.h>
int main()
{
int n1,n2,n3,greater;
printf("Enter three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
greater=(n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3);
printf("The largest number is %d",greater);
return 0;
}
Output:
Enter three numbers:10
20
30
The largest number is 30
Q.2. (15
marks)
Code:
#include <stdio.h>
if (b == 0) {
return a;
MUquestionpapers.com 6
int main() {
return 0;
Output:
10
GCD of 5 and 10 is 5
1. signed / unsigned:
MUquestionpapers.com 7
2. short / long:
The `long` modifier is used to declare a variable with a larger storage size
than its default data type. For example, `long int` uses more memory
than a regular `int`, allowing you to represent larger values.
3. long long:
The `long long` modifier is used to declare a variable with an even larger
storage size than the regular `long` modifier. This is often used to
represent very large integers.
4. const:
5. volatile:
6. Bool:
The `Bool` data type modifier is used to declare variables that can hold
only two values: `0` (false) or `1` (true). It is typically used in boolean
expressions and is often used in conjunction with the <stdbool.h>
header.
7. Complex:
8. The Complex modifier is used to declare variables that hold complex
numbers. It is used in conjunction with the <complex.h> header and
allows you to work with complex arithmetic.
MUquestionpapers.com 8
8. Imaginary:
(4marks)
#include <stdio.h>
#include <string.h>
int main() {
char inputString[100];
scanf("%s", inputString);
return 0;
Output:
MUquestionpapers.com 9
Q.3. (15 marks)
#include <stdio.h>
int is_prime(int num) {
if (num <= 1)
{
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
printf("Prime numbers between 100 and 500:\n");
for (int i = 100; i <= 500; i++) {
if (is_prime(i)) {
printf("%d,", i);
}
}
return 0;
}
MUquestionpapers.com 10
Output:
Prime numbers between 100 and, 500:
101,103,107,109,113,127,131,139,149,151,163,167,173,181,191,193,
197,
199,211,233,227,229,233,239,241,251,257,263,269,271,277,281,283,
293,
307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,
401, 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499
MUquestionpapers.com 11
float average = (float)sum / N;
printf("Average of %d elements: %.2f\n", N, average);
return 0;
}
Output:
Enter the number of elements: 4
Enter element 1: 25
Enter element 2: 12
Enter element 3: 13
Enter element 4: 14
Average of 4 elements: 16.00
The left shift operator shifts the bits of a number to the left by the
specified number of positions. It's equivalent to multiplying the number
by 2 raised to the power of the shift count.
The right shift operator shifts the bits of a number to the right by the
specified number of positions. It's equivalent to integer division of the
number by 2 raised to the power of the shift count.
MUquestionpapers.com 12
Here's a complete example program that demonstrates both left and
right shift operators:
#include <stdio.h>
int main() {
// Left shift
// Right shift
return 0;
Q.4.
(15 marks)
#include <stdio.h>
#include <string.h>
MUquestionpapers.com 13
struct Student {
int roll;
char name[50];
int marks;
};
int main() {
printf("Roll: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf("%s",students[i].name);
printf("Marks: ");
scanf("%d", &students[i].marks);
printf("\nStudent Information:\n");
printf("Roll\tname\tmarks\t\n");
printf("---------------------------------\n");
MUquestionpapers.com 14
printf("%d\t%s\t %d\n",students[i].roll,students[i].name,students[i].mar
ks);
return 0;
Output:
(5marks)
#include<stdio.h>
void main()
MUquestionpapers.com 15
{
int n=0,i;
char a[100],rev[100];
printf("Enter a string:");
scanf("%s",a);
while(a[n]!='\0');
n++;
for(i=0;i<=(n-1);i++)
rev[n-i-1]=a[i];
for(i=0;i<=n-1;i++)
if(a[i]!=rev[i])
break;
if(i==n)
else
MUquestionpapers.com 16
}
Output:
#include<stdio.h>
void main()
int i;
for(i=0;i<5i++);
printf(“%d”,i);
printf(“\n hi”);
OUTPUT:
hi
Q.5.
#include <stdio.h>
int main()
MUquestionpapers.com 17
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
printf("%d is greater",a);
else
printf("%d is greater",c);
else
if(b>c)
printf("%d is greater",b);
else
MUquestionpapers.com 18
printf("%d is greater",c);
return 0;
Output:
8 is greater
1. Entry-Controlled Loop:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
MUquestionpapers.com 19
2. Exit-Controlled Loop:
int i = 0;
do {
printf("%d\n", i);
i++;
In summary:
Entry-Controlled Loop (while loop and for loop): The loop body is
executed only if the condition is initially true.
MUquestionpapers.com 20
The general syntax of function prototype in c :
return_type function_name(parameter1_type, parameter2_type, ...);
Example:
#include <stdio.h>
// Function prototype
int main() {
int result;
return 0;
// Function definition
return a + b;
MUquestionpapers.com 21
Q.6.
(6marks)
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 10
if (matrix[i][j] != matrix[j][i])
return false;
return true;
MUquestionpapers.com 22
int main() {
int size;
scanf("%d", &size);
return 1;
int matrix[MAX_SIZE][MAX_SIZE];
scanf("%d", &matrix[i][j]);
if (isSymmetric(matrix, size)) {
MUquestionpapers.com 23
} else {
return 0;
Output:
MUquestionpapers.com 24
- Iterate from `i` starting at 2 up to the square root of `num`:
- If `num` is divisible by `i` (i.e., `num % i == 0`), return 0 (indicating
not prime).
- If no divisors were found, return 1 (indicating prime).
3. In the `main` function:
- Declare an integer variable `num` to store the user-entered number.
- Display a prompt to the user: "Enter a number: ".
- Read the user's input into the `num` variable using `scanf`.
4. Call the `isPrime` function with `num` as an argument to check
whether it's prime or not.
5. Based on the return value of `isPrime`, display an appropriate
message:
- If `isPrime(num)` returns 1, print "num is a prime number."
- If `isPrime(num)` returns 0, print "num is not a prime number."
6. End the program.
(4marks)
Syntax:
switch (expression) {
case constant1:
break;
case constant2:
break;
MUquestionpapers.com 25
// Add more cases as needed
default:
Example:
#include <stdio.h>
int main() {
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
MUquestionpapers.com 26
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
return 0;
Output:
Wednesday
MUquestionpapers.com 27