Sample Questions and answers

1. /* Four function Cacutalor uSing switch-case construct */

#include <stdio.h>
main()
{
int a,b, result;
int choice;
printf ("Enter two numbers 
");
scanf ("%d%d", &a, &b);
print f ("	  1. Adition   	”);
print f ("	  2. Division   	”);
print f ("	  3. Substraction   	”);
print f ("	  4. Multiplication  	”);
printf ( "Enter Your choice (1-4)..... 
");
scanf ( "%d", &choice) ;

switch(choice )
{
case 1:
result: a+b;
             break;
case 2:
            result: a / b;
             break;
case 3:
            result: a - b;
             break;
case 4:
            result: a * b;
             break;
default:
           printf(“That was not a valid choice
”)
           break;
}
Printf(“The result is %d”,result);
}

2. /* Program to check whether the year is leap year or not */

#include <stdio.h>
main()
{
Int year;
printf (“Enter the year  
”);
scanf (“%d”, &year);
if((year%4 =  = 0 && year%100 != 0) || (year%400 = = 0))
{
printf (“The year is a leap year”);
}
 	else
{
  	 printf (“The year is not a leap year”)
 }
}

3. /* To calculate the factorial of a given number using for loop */

#include <stdio.h>
main()
{
int number;
int count;
int fact=1;
printf(“ Enter the number 
”);
scanf(“%d”, &number);
for(count=1; count <=number, count++)
{
fact = fact * count;
}
printf(“The factorial of the given number is %d”, fact);
}

4. /* Program to calculate Fibonacci series using while loop */

#include <stdio.h>
main()
{
Int a, b, c, d;
a=0;
b=1;
printf(“Enter the number of terms”);
scanf(“%d”, &d);
while(d >= 1)
{
c= a+ b;
a= b;
b = c;
d = d -1;
printf(“%d
”,c);
}
}

5. /* Program to generate prime numbers */

#include <stdio.h>
main()
{
int i, j, n, c;
printf(“Enter the number upto which the prime numbers are to be generated”);
scanf(“%d”, &n);
printf(“1
”);
printf(“2
”);
       for(i=3; i <= n ; i ++);
       {
           for ( j = 2, c= 0 ;  j  <  i + ½   &&  c = = 0;  j++);
          {
                if( i % j = = 0)
                         c = 1;
            }
         If( c = = 0)
        printf(“%d
”, i);
      }

}