CL1 Solution
CL1 Solution
CL1 Solution
KHARAGPUR
Stamp / Signature of the Invigilator
Marks Obtained
Marks obtained (in words) Signature of the Examiner Signature of the Scrutineer
Answer all the Questions. Write the answers in the boxes only. You can use
the designated spaces for rough works.
1. Determine the normalized number from the given single precession floating point representation. The
normalized number is represented as (−1)s × 1.(mantissa) × 2exponent . Determine each component
and show the number in the format mentioned above. [4]
10111010101101111010000000000000
(a) (2 marks) Write a condition which will be true if any pair of the int variables x,y,z are equal
to each other, false otherwise.
(c) (3 marks) In the above program, what is the range of input c for which the output is “Third
condition”?
(d) (2 marks) Write the output of the program for input value of the variable c = 2.
#include <stdio.h>
int main() {
int c;
scanf("%d",&c);
switch(c) {
case 2-1: printf("One\n");
case 1+1: printf("Two\n");
default: printf("Default\n");
}}
#include <stdio.h>
int main() {
int a = 10, b = 3, c, d, i = 20, j = 30, x;
x = a / b;
c = x++;
d = x / c;
d++;
i *= ++c;
j /= i++;
printf("x = %d c = %d d = %d i = %d j = %d", x, c, d, i, j);
return 0;
}
2
4. The following code computes the prime factorization of a number a (≥ 2) given as input. For example,
if a = 72, it will print: Prime factorization = 2 x 2 x 2 x 3 x 3. Fill up the blanks
in this code. [5]
#include <stdio.h>
int main(){
int a, d = 2;
printf("Enter a positive integer: ");
scanf("%d", &a);
printf("Prime factorization = ");
do{
if (_____________){ // 1.5 marks
printf("%d x ", d);
______________; // 2 marks
}
else
______________; // 1.5 marks
}
while(a >= d);
printf("\b\b \b\b.\n");
return 0;
}
5. For the C program given below, what will be the output for the input 1234 5678 f (three data items
with two spaces in between) are given? [5]
#include <stdio.h>
int main() {
int a,b;
char c;
printf("Enter the values of a, b, c:");
scanf("%2d%3d%c",&a,&b,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
scanf("%2d%3d%c%c",&a,&b,&c,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
return 0;
}
3
6. What is the output of the following program? [2]
#include <stdio.h>
int main() {
double x = 123456789.99;
int y = x;
printf("y = %d\n y = %0.2lf\n", y, (double)y);
return 0;
}
4
1. Determine the normalized number from the given single precession floating point representation. The
normalized number is represented as (−1)s × 1.(mantissa) × 2exponent . Determine each component
and show the number in the format mentioned above. [4]
10111010101101111010000000000000
Ans:
Sign bit = 1
Mantissa = 01101111010000000000000 (last 23 bits)
Exponent = 117 - 127 = -10
Normalized Number = (−1)1 × 1.0110111101 × 2−10
(a) (2 marks) Write a condition which will be true if any pair of the int variables x,y,z are equal
to each other, false otherwise.
((x==y)||(y==z)||(x==z))
#include <stdio.h>
int main() {
char a=’s’-’g’,b=’U’-’L’;
int c;
scanf("%d",&c);
if(c>a) printf("First condition\n");
if(c<b) printf("Second condition\n");
else printf("Third condition\n");
}
Second condition
(c) (3 marks) In the above program, what is the range of input c for which the output is “Third
condition”?
9 − 12
(d) (2 marks) Write the output of the program for input value of the variable c = 2.
#include <stdio.h>
int main() {
int c;
scanf("%d",&c);
switch(c) {
case 2-1: printf("One\n");
case 1+1: printf("Two\n");
default: printf("Default\n");
}
}
5
3. What will be printed by the following program? [5]
#include <stdio.h>
int main() {
int a = 10, b = 3, c, d, i = 20, j = 30, x;
x = a / b;
c = x++;
d = x / c;
d++;
i *= ++c;
j /= i++;
printf("x = %d c = %d d = %d i = %d j = %d", x, c, d, i, j);
return 0;
}
x = 4 c = 4 d = 2 i = 81 j = 0
4. The following code computes the prime factorization of a number a (≥ 2) given as input. For example,
if a = 72, it will print: Prime factorization = 2 x 2 x 2 x 3 x 3. Fill up the blanks
in this code. [5]
#include <stdio.h>
int main(){
int a, d = 2;
printf("Enter a positive integer: ");
scanf("%d", &a);
printf("Prime factorization = ");
do{
if (_____________){ // 1.5 marks
printf("%d x ", d);
______________; // 2 marks
}
else
______________; // 1.5 marks
}
while(a >= d);
printf("\b\b \b\b.\n");
return 0;
}
5. For the C program given below, what will be the output for the input 1234 5678 f (three data items
with two spaces in between) are given against a, b, c? [5]
6
#include <stdio.h>
int main() {
int a,b;
char c;
printf("Enter the values of a, b, c:");
scanf("%2d%3d%c",&a,&b,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
scanf("%2d%3d%c%c",&a,&b,&c,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
return 0;
}
Ans:
a = 12
b = 34
c=‘’
a = 56
b = 78
c=f
#include <stdio.h>
int main() {
double x = 123456789.99;
int y = x;
printf("y = %d\n y = %0.2lf\n", y, (double)y);
return 0;
}
Ans:
y = 123456789
y = 123456789.00