CT1 Sol
CT1 Sol
[Write your answers in the question paper itself. Answer all questions. All programs must be written in C.
Fill in the blanks in the following codes to make them work as described. Do not use extra variables.
Not all blanks carry equal marks. Evaluation will depend on overall performance.]
1. The following program reads a positive integer n from the user, and computes the smallest power of
two, that is greater than or equal to n, that is, it computes p = 2e such that p / 2 < n ⩽ p. The program
should use math library functions to do this. The program must not contain any loop. Instead it
should first compute e as log2 n truncated to an integer. It should then compute p = 2e. If p ⩾ n, then
we are done. Otherwise, the above value of p is updated to the correct value. Fill in the blanks below
so that the program behaves as mentioned above.
[10]
#include <stdio.h>
#include <math.h>
int main ( )
{
int n, e, p;
/* First compute e */
p = pow(2,e) ;
if ( p < n ) p = p * 2 ;
Page 1 of 4
2. The following program is meant for computing the equation of the straight line passing through two
points P1 = (x1, y1) and P2 = (x2, y2). The equation of the line is to be specified in the form y = mx + c.
Two special cases need to be handled. First, if P1 and P2 are the same point, then this problem cannot
be solved. Second, a vertical line cannot be written in the above form, and is instead printed in the
form x = c. Assume that the coordinates x1, y1, x2, y2 are integers. However, m and c computed as
above need not be integers. We use the floating-point data type double to store these values. Fill in
the blanks below so that the program works as intended.
[10]
#include <stdio.h>
int main ( )
{
int x1, y1, x2, y2;
double m, c;
if ( x1 == x2 ) {
else {
c = x1 ;
} else {
c = y1 – m * x1 [or y2 – m * x2] ;
Page 2 of 4
3. The following program reads a positive integer n from the user, and attempts to express n as the sum
of two squares, that is, as n = a2 + b2, where a and b are positive integers with a ⩽ b. This inequality
implies that a is no larger than the truncated integer value of √ n / 2. Call this maximum value of a as
amax. The program carries out a search for a in the sequence 1, 2, 3, ⋯, amax. For each a, an integer b
is computed as the truncated value of √ n – a2 . If these integers a and b satisfy n = a2 + b2, then the
search loop is broken. If none of the values of a in the above range can produce a suitable b, then
also the loop is broken. At the end, the program prints its decision (failure or the values a and b). Fill
in the blanks below to make the program work as explained above. Write only the main() function.
Use math library functions whenever needed.
[10]
int main ( )
{
int n, a, b, amax;
amax = sqrt(n / 2) ;
b = sqrt(n – a * a) ;
if ( n == a * a + b * b ) break;
}
if ( a > amax )
printf("%d cannot be written as the sum of two squares\n", n);
else
printf("%d = %d^2 + %d^2\n", n, a, b);
return 0;
}
Page 3 of 4
4. The following program reads a positive integer n from the user. It then computes and prints the
number of times each digit (decimal) appears in n. For example, if n = 250168005, then the digit 0
appears three times, the digits 1, 2, 6, and 8 once each, and the digit 5 twice. The digits 3, 4, 7, and 9
do not appear in this n. Notice that leading 0 digits should not be counted. For example, if the user
enters 00250168005 as n, the digit 0 will still be reported to have appeared three times in n. You do
not have to do anything special to ensure this, because scanf will ignore the leading zero digits while
storing n. Fill in the blanks in the code below to perform the task mentioned above. Write only the
main() function. Do not use math library functions.
[10]
int main ()
{
int num, n;
int digit, count;
while ( n > 0 ) {
if ( n % 10 == digit ) ++count;
}
printf("Digit %d occurred %d times\n", digit, count);
}
return 0;
}
Page 4 of 4