logo codesdope
Level 1
1.
What would be the output of:
#include <stdio.h>
int main(){
    for(int i = 0;1;i++){
        printf("%d\n",i);
    }
    return 0;
}
2.
What would be the output of:
#include <stdio.h>
int main( )
{
    int x = 10, y = 3, z;
    for(z = 0; z<x; )
    z = z++ +y;
    printf("%d\n", z) ;
    return 0;
}
3.
What would be the output of:
#include <stdio.h>
int main(){
    int x = 10, y = 20, z;
    for(z = 0,z<y;z++){
        if(z==x){
            printf("%d\n",z);
        }
        else{
            break;
        }
    }
}
4.
Write the same code using the while loop.
#include <stdio.h>
int main(){
    for(int i = 0;1;i++){
        printf("%d\n",i);
    }
    return 0;
}
Level 2
1.
Take 10 integers from keyboard using loop and print their average value on the screen.
Answer
#include <stdio.h>
int main()
{
  int sum = 0,i;
  for(i = 0;i<10;i++){
    int x;
    printf("Enter a number.\n");
    scanf("%d",&x);
    sum = sum+x;
  }
  printf("Average is %f\n",sum/10.0);
  return 0;
}
									

2.
Print ASCII values and their equivalent characters. ASCII value vary from 0 to 255.
Answer
#include <stdio.h>
int main()
{
  int i;
  for(i=0;i<255;i++)
  {
    printf("%c\n",i);
  }
}
									

3.
Print the following patterns using loop :
a.
*
**
***
****
b.
   *  
 *** 
*****
 *** 
   *  
c.
1010101
 10101 
  101  
   1   

4.
Print multiplication table of 24, 50 and 29 using loop.
Answer
#include <stdio.h>
int main()
{
  int i;
  for(i=1;i<=10;i++)
  {
    printf("24 * %d\t=\t%d\n",i,24*i);
  }
}
									

5.
Write an infinite loop.

6.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0
Write a C program to calculate factorial of a number.
Answer
#include <stdio.h>
int main()
{
  int x,fact=1,i;
  printf("Enter a number\n");
  scanf("%d",&x);
  for(i = x; i>=1 ;i--){
    fact = fact*i;
  }
  printf("Factorial is %d\n",fact);
}
									

7.
Write a C program to find greatest common divisor (GCD) or highest common factor (HCF) of given two numbers.
Answer
#include <stdio.h>
int main()
{
  int x,y,gcd,lcm,t,b,a;
  printf("Enter two integers\n");
  scanf("%d %d", &x, &y);
  a=x;
  b=y;
  while (b != 0)
  {
    t = b;
    b = a % b;
    a = t;
  }
  gcd = a;
  lcm = (x*y)/gcd;
  printf("GCD is %d and LCM is %d\n",gcd,lcm);
}
									

Level 3
1.
Calculate the sum of digits of a number given by user. E.g.-
INUPT : 123        OUPUT : 6
INUPT : 12345        OUPUT : 15

2.
A three digit number is called Armstrong number if sum of cube of its digit is equal to number itself.
E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153.
Write all Armstrong numbers between 100 to 500.
Answer
#include <stdio.h>
int main()
{
  int i;
  for(i=1;i<=500;i++)
  {
      int sum = 0;
      int t = i;
      while(t!=0)
      {
        sum = sum+((t%10)*(t%10)*(t%10));
        t = t/10;
      }
      if(sum==i)
      {
        printf("%d\n",i);
      }
  }
  return 0;
}
									

3.
Write a C program to print all prime number in between 1 to 100.

4.
Write a C program to print a number given by user but digits reversed. E.g.-
INPUT : 123        OUTPUT : 321
INPUT : 12345        OUTPUT : 54321

5.
Write a C program to find prime factor of a number.
If a factor of a number is prime number then it is its prime factor.
Answer
#include <stdio.h>
int main()
{
    int x,i,j;

    printf("Enter the number\n");
    scanf("%d",&x);

    printf("Prime factors of %d are:\n",x);

    for(i=2;i<=x;i++){
      //checking for factor
      if(x%i==0){
        //checking if i is prime or not
        int p = 0;
        for(j=2;j<i;j++){
          if(i%j==0){
            //i is not prime
            p++;
            break;
          }
        }
        if(p==0){
          //if p is 0
          //then i is prime
          printf("%d\n",i);
        }
      }
    }
  return 0;
}
									
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.