Questions 1: Fix the errors, recompile and match against the output provided.
Output:
This is an "error free" program.
Given Code:
#include < stdio.h >
int main()
{
printf("This is an
"error free" program");
return 0;
}
#include < stdio.h >
int main()
{
printf("This is an
\"error free\" program");
return 0;
}
Questions 2: Complete the code to display the factorial value of the given number as shown in Sample Ouptut:
Output:
Factorial of 5 is 120
Given Code:
#include < stdio.h >
int main(){
int num,fact;
scanf("%d",&num);
// User Code
printf("Factorial of %d
is %d",num,fact);
return 0;
}
#include < stdio.h >
int main(){
int num,fact;
scanf("%d",&num);
// User Code
fact = 1;
int temp = num;
while( temp!=0 )
{
fact = fact * temp;
temp--;
}
printf("Factorial of %d
is %d",num,fact);
return 0;
}
Questions 3: Check whether the given program can find greatest of 3 numbers.
Input: 2 8 1
Output: 8
Given Code:
#include < stdio.h >
int main(){
int n1,n2,n3;
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2) && (n1>n3)
printf("%d",n1);
elseif ( n2>n3)
printf("%d",n2);
else
printf("%d",n3);
return 0;
}
#include < stdio.h >
int main(){
int n1,n2,n3;
scanf("%d %d %d",&n1,&n2,&n3);
if ( (n1>n2) && (n1>n3) )
printf("%d",n1);
else if( n2>n3)
printf("%d",n2);
else
printf("%d",n3);
return 0;
}
Questions 4: The function patternPrint(int n) is supposed to print n number of lines in the following pattern:
for n=4 pattern should be
1
1 1
1 1 1
1 1 1 1
The function compiles successfully but fails to return the desired results due to logical errors.
Your task is to debug the program to pass all the test cases.
Given Code:
void patternPrint(int n)
{
int print=1,i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",print);
printf("\n");
}
}
}
void patternPrint(int n)
{
int print=1,i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",print);
}
printf("\n");
}
}
Questions 5: Correct the syntax error in the given code without modifying the logic to get the below output.
Output:
Choice is 1
Given Code:
#include < stdio.h >
int main(){
float x=1.1;
switch(x){
case1: printf("Choice is 1");
break;
default: printf("Invalid Choice");
break;
}
return 0;
}
#include < stdio.h >
int main(){
int x=1;
switch(x){
case1: printf("Choice is 1");
break;
default: printf("Invalid Choice");
break;
}
return 0;
}
Questions 6: Debug and fix the code so that it can print the below pattern.
Pattern:
2 4
2 4
2 4
2 4
Given Code:
#include < stdio.h >
int main()
{
for(int i=1;i <= 4 ;i++)
{
for( z = 1 ; z <= 4 ; z++)
{
if(z % 3 == 0)
{
printf("%d ",z);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
#include < stdio.h >
int main()
{
for(int i=1;i <= 4;i++)
{
for(int z=1;z <= 4;z++)
{
if(z % 2 == 0)
{
printf("%d ",z);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Questions 7: Debug and fix the code so that it can print the below pattern.
Pattern:
* * * *
#include < stdio.h >
int main()
{
for(int i=1;i <= 4;i--)
{
for(int j=1;j <= 4; j++)
{
if(i != j)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
#include < stdio.h >
int main()
{
for(int i = 1 ; i <= 4 ; i++)
{
for(int j = 1 ; j <= 4 ; j++)
{
if(i == j)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Questions 8: Debug and fix the below code so that it can print 0 as an output.
Given Code:
#include < stdio.h >
int main()
{
int z = 8 = 9 = 7 ;
printf("%d",z);
return 0;
}
#include < stdio.h >
int main()
{
int z = 8 == 9 == 7 ;
printf("%d",z);
return 0;
}
Questions 9: Debug and fix the code so that it can print the below pattern.
Pattern:
**** ** ****
#include < stdio.h >
int main()
{
int x = 1;
switch(x)
{
case 1 :
printf("****");
printf("\n");
break;
case 2:
printf(" ** ");
printf("\n");
case 3 :
printf("****");
printf("\n");
default :
printf("****");
break;
}
return 0;
}
#include < stdio.h >
int main()
{
int x = 1;
switch(x)
{
case 1 :
printf("****");
printf("\n");
case 2:
printf(" ** ");
printf("\n");
case 3 :
printf("****");
printf("\n");
break;
default :
printf("****");
break;
}
return 0;
}
Questions 10: Debug and fix the below code so that it can print the below output.
Output:
9 8 7 6 5 4 3 2 1 0
Given Code:
#include < stdio.h >
int main()
{
int x = 10;
do
{
printf("%d ",x);
x--;
}while(x >= 0);
return 0;
}
#include < stdio.h >
int main()
{
int x = 10;
do
{
x--;
printf("%d ",x);
}while(x > 0);
return 0;
}