Professional Documents
Culture Documents
2016
Course: CSF111 Semester-II, 2015-16, Mid-sem (Open Book) Time: 2.30 PM to 4.00 PM
Computer Programming Full mark : 75 Duration: 1.5 Hrs
P.T.O.. . . 1
CS F111
c++;
}
return 0;
}
Ans: 0 time. The condition inside while loop is never satisfied.
(c) What will be the output of following code? 2
int main()
{
float x=5.333;
if(x==5.333)
printf("\nIts easy.");
else
printf("\nNo. Its not.");
}
Ans: No. Its not. (reason: 5.333 is a recurring float. And, hence the value in 64-bit is more than
value in 32-bit representation.
(d) Convert the following nested if-else code to statement with ternary operator ‘?:’. 2
int a;
if(5>3)
if(8>4)
if(4!=4)
a=1;
else
a=2;
else
a=3;
else
a=4;
Ans: a=(5 > 3?(8 > 4?(4! = 4?1 : 2) : 3) : 4);
(e) What will be the output of the program? Also, re-write the code using ‘While’ loop. 3
#include<stdio.h>
int main()
{
int i=0;
for( ;i<=5;i++);
printf("%d ", i);
return 0;
}
Ans: 6 //2 marks +1 mark for while loop below
while(i<=5)
i++;
printf("%d ",i);
(f) What is the output? 4
P.T.O.. . . 2
CS F111
main(){
int x[]={1,2,3,4,5,6,7,8};
int i;
for(i=2;i<6;i++)
x[x[i]]=x[i];
for(i=0;i<8;i++)
printf("%d ",x[i]);
}
Ans: 1 2 3 3 5 5 7 8
(g) Write the output. 4
#include<stdio.h>
int main()
{
int i=-2, j=3, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
Ans: -1, 3, 0, 1
4. We call a square matrix “Lucky” if the left diagonal sum and the right diagonal sum are equal;
otherwise the matrix is “Unlucky”. Complete the following code to check whether an input matrix is
“Lucky” or “Unlucky”. Don’t use any additional variables in the following code. 12
#include<stdio.h>
int main(){
int n;
printf("\nEnter dimension(n) of matrix:");
scanf("%d",&n);
int A[n][n],i,j;
/*Part I. Write a code to read the matrix from user*/
int ld_sum,rd_sum;
/*Part II. Write a code to check whether matrix is Lucky or Unlucky.*/
return 0;
}
Ans: Part-I (reading matrix from user) –3 marks, Intitializing ld sum and rd sum to 0 –2 marks,
Part-II –5 marks, Final condition for checking –2 marks
//Part-I
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
ld_sum=rd_sum=0;
//Part-II
P.T.O.. . . 3
CS F111
for(i=0;i<n;i++){
ld_sum += A[i][i];
rd_sum += A[i][n-i-1];
}
if(ld_sum==rd_sum)
printf("\nLucky");
else
printf("\nUnlucky");
5. The following code takes an input n and finds out the next prime number if n is not prime. Else it
prints n. However, the programmer has made some errors in the code. Correct it. (Only identify the
statement and correct it. Don’t use additional variables or codes.) 10
#include <stdio.h>
int main(){
int n,i;
scanf("%d",&n);
if(n<=3){
printf("\nNext prime: %d\n",n);
return 0;
}
int isPrime=0;
do{
for(i=1;i<n-1;i++){
if(n%i==0){
isPrime=0;
continue;
}
isPrime=1;
}
if(!isPrime)
continue;
n=n+1;
}while(1);
printf("\nNext prime:%d\n",n);
return 0;
}
Ans:
#include <stdio.h>
int main(){
int n,i;
scanf("%d",&n);
if(n<=3 && n>1){ //2 marks
printf("\nNext prime: %d\n",n);
P.T.O.. . . 4
CS F111
return 0;
}
int isPrime=0;
do{
for(i=2;i<n-1;i++){//2 marks
if(n%i==0){
isPrime=1; //2 marks
break; //1 marks
}
isPrime=0; //2 marks
}
if(!isPrime)
break; //1 mark
n=n+1;
}while(1);
printf("\nNext prime:%d\n",n);
return 0;
}
6. Write a C program to checks whether a number is divisible by 11 using 11’s divisibility rule. [11’s
divisibility rule: (Sum of all odd position digits – sum of all even positions) is divisible by 11] 15
#include <stdio.h>
int main(){
int n,n1,sum_even=0,sum_odd=0,i=0; //2 marks
scanf("%d",&n);
n1=n;
while(n){ //10 marks
if(i%2)
sum_odd += (n%10);
else
sum_even += (n%10);
i++;
n /= 10;
}
if((sum_even-sum_odd)%11==0) //if-else 3 marks
printf("\n%d is divisible by 11.",n1);
else
printf("\n%d is NOT divisible by 11.",n1);
return 0;
}