Nirma University: Institute of Technology
Nirma University: Institute of Technology
Nirma University: Institute of Technology
Nirma University
Institute of Technology
Semester End Examination, February 2021
B. Tech in CSE/EC/IC, Semester I
2CS101: Computer Programming
Q.2 Trace the code snippet given and show the output and tracing. In [10]
case of error, mention the error and justify it. Assume header files
are included.
(i) main( ) 2
{
int i = 4, j = 0, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "\ny = %d z = %d", y, z ) ;
}
(ii) main( ) 2
{
int x = 4, y = 0, z ;
while ( x >= 0 )
{
if ( x == y )
break ;
else
printf ("\n%d %d", x, y ) ;
x-- ;
y++ ;
}
}
Page 1 of 3
2CS101 Computer Programming
(iii) main( ) 2
{
int k ;
float j = 2.0 ;
switch ( k = j + 1 )
{
case 1: printf("\n Free");
case 2: printf("\n Escaped");
case 3: printf("\n Trapped") ;
default: printf("\n Caught!") ;
}
}
(iv) main() 2
{
char p[20];
char s[] = "Nirma";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s", p);
}
(v) int func(int, int); 2
main( )
{
printf("%d", func(10,20));
}
int func(int a, int b)
{
int a;
a = 20 + b;
return a;
}
Q.3 Draw flowchart to find the perfect numbers within a given range. [6]
Perfect number, a positive integer that is equal to the sum of its
proper divisors. For example, 6 is a perfect number, which is the
sum of 1, 2, and 3. Take starting range and ending range from
user. For example:
Input the starting range or number : 1
Input the ending range of number : 50
The Perfect numbers within the given range : 6 28
Q.4 Write a C program for reversing 1-D array elements using [6]
function. Pass the array to a user defined function, reverse the
array elements in the array itself and print the reversed array
from the main function. All the values needed should be taken
from user.
OR
Q.4 Write a program in C to print Fibonacci series using recursive [6]
Page 2 of 3
2CS101 Computer Programming
function (recursion). For example,
Input number of terms for the Series : 10
The Series are :
1 1 2 3 5 8 13 21 34 55
Q.5 Write a C program to calculate time difference between two time [8]
periods using structure and pointer. Create a structure for
denoting a time period (hours, minutes and seconds). Make a
pointer that points to the structure. Use pointers to access
structure members and calculate the difference. Display the
difference between two time periods. All the values needed should
be taken from user. Perform validations to enter correct time
period.
*******************************
Page 3 of 3