You are on page 1of 4

Question 1 - Which of the following statement is true?

(8pts)

a. scanf( "%d%d", number1, number2);


b. printf( "Values are %d and %d\n", &number1, &number2 )
c. scanf( "%d%d", &number1, &number2 );
d. scanf( "Values are %d and %d\n", number1, number2 )
e. printf( "%d%d", &number1, &number2);

Question 2 - What does the following program print? (8pts)

printf( "****\n***\n**\n*\n**\n***\n****\n" );

Answer 2:

Question 3 - What does the following program print? (8pts)

#include <stdio.h>
int main( void )
{
int c = 5;
printf( "%d\n", c++ );
printf( "%d\n\n", c );
c = 8;
printf( "%d\n", ++c );
printf( "%d\n", c );
}

Answer 3:
Question 4 - What does the following program print? (8pts)

#include <stdio.h>

int main( void )


{
int outer_count = 1;
while ( outer_count <= 5 )
{
int inner_count = 1;
while ( inner_count <= outer_count )
{
printf( "%d,", inner_count*outer_count );
inner_count++;
}
printf( "\n" );
outer_count++;
}
}

Answer 4:
Question 5 - Following C program that takes two integers and one of addition, subtraction, multiplication,
division, and remainder opeartions from the user as input, and calculates the entered operation between two
numbers. Complete the missing parts of the program given below by writing them in the boxes. (24pts)

Answer 5:

#include <stdio.h>
int main()
{
int num1, num2;
char option, control;
float result;
do{
printf("Enter 2 integer numbers : ");
scanf("%d%d", );

printf("Enter the operation you want to do (+,-,*,/%) : ");


scanf(" %c",&option);

switch(option) {
case '+' :
result = num1 + num2;
printf("Operation : %d %c %d = %f \n",num1,option,num2,result);
break;
case '-' :
;

printf("Operation : %d %c %d = %f \n",num1,option,num2,result);
break;
case '*' :
;

printf("Operation : %d %c %d = %f \n",num1,option,num2,result);
break;
case '/' :
;

printf("Operation : %d %c %d = %f \n",num1,option,num2,result);
break;
case '%' :
;

printf("Operation : %d %c %d = %f \n",num1,option,num2,result);
break;
:

printf("You entered wrong operation.\n");


}
printf("Do you want to continue (YES(y / Y) - NO(n/N)):");
scanf(" %c", &control);
}while(control == 'y' || control == 'Y');
printf("The program has been terminated.\n");

return 0;
}
Question 5 - Convert following Switch structure to corresponding if Else structure. (24pts)

switch (shape)
{
case '1':
printf("Please input the radius:\n");
scanf("%lf", &r);
area = r * r * 3.14159;
break;
case '2':
printf("Please input the side length:\n");
scanf("%lf", &x);
area = x * x;
break;
case '3':
printf("Please input the side lengths:\n");
scanf("%lf %lf", &x, &y);
area = x * y;
break;
default:
printf("Invalid input.");
return 0;
}

Answer 6 :

if (shape == '1')
{
printf("Please input the radius:\n");
scanf("%lf", &r);
area = r * r * 3.14159;
}
else if (shape == '2')
{
printf("Please input the side length:\n");
scanf("%lf", &x);
area = x * x;
}
else if (shape == '3')
{
printf("Please input the side lengths:\n");
scanf("%lf %lf", &x, &y);
area = x * y;
}
else
{
printf("Invalid input.");
return 0;
}

You might also like