Practice Programs

  • Doing Programs gives you the great understanding about what you have learned.
  • Doing Programs gives you the Confidence.
  • So let's Start by doing some programing.

  • Q:Declare a variable and take its value from the user and if it is even number print it is a even number if not then print it is not a even number.

    Solution:
    #include<stdio.h>
    
    void main()
    {
        int a;
        printf("Enter the number:");
        scanf("%d",&a);
        if(a % 2==0)   //modulo  operator gives you a remainder.
                     //if remainder is equal to 0 then it is a even number.
        {            // other wise it is a  odd number.
           printf("\nNumber is a even number.");
        }
        else
        {
            printf("\nNumber is not an even number");
        }
    }
    

    Output:

    Enter the number:12
    Number is a even number

    Exercise:

    Q:Declare a variable and take its value from the user and check it is odd number or not, if it is odd print number is odd otherwise print not an odd number.


  • Q:declare a variable 'n' and take its value from the user,and print the 'n' even number?

    Solution:

    #include<stdio.h>
    void main()
    {
        int n;
        int result,i=0;
    
        printf("Enter how many even number you want:");
        scanf("%d",&n);
    
        printf("Even numbers are:");
        while(i<=n)  //condition
        {
        printf("%d\t",i*2);
           i++;      //incrementing  the variable
    
        }
    }
    

    Output:

    Enter how many even number you want:6
    Even numbers are:0    2    4    6    8    10

    Exercise:

    Q:declare a variable 'n' and take its value from the user,and print the 'n' odd numbers? try this with for or do-while loop


  • Q:declare a variable 'n' and take its value from the user,and print numbers from 1 to 'n' variable through loop ,if at certain iteration if you get '6' than break the loop.

    Solution:

    #include<stdio.h>
    void main()
    {
        int n;
        int result,i=0;
    
        printf("Enter the value of n:");
        scanf("%d",&n);
    
        while(i<=n)  //condition
        {
          if(i==6)
          {
              break;    //this will execute if I value will become to 6.
          }
          else{
             printf("%d\n",i);
          }
    
        i++; //increment the variable
        }
    }
    

    Output:

    Enter the value of n:10
    1
    2
    3
    4
    5

    Exercise:

    Q:declare a variable 'n' and take its value from the user,and print numbers from 1 to 'n' variable through loop, if at certain iteration if you get '6' than Skip that part and continue with next iteration .

    Hint:Use continue keyword.

  • Q:Declare variable n and take its value from the user and print it in a reverse order.if number is 123 then output should be 321.

    Solution:

    #include<stdio.h>
    
    void main()
    {
        int n;
        int i,j=0;  //initialized j value to 0
                  //because it should start with garbage value
        printf("enter the number:");
        scanf("%d",&n);
        while(n!=0)
        {       //if we do modulo of 10 to any number its gives last digit.
                      //eg-if: 123 % 10 = 3
            i = n%10;  //at every iteration we are
            j = j*10 + i;  //storing that last variable in i variable.
                       //assigning current j*10 + value of i to new j variable for every iteration.
            n = n/10; //dividing cuurent n value with 10 and assigning it to new 'n' value,for every iteration.
        }   //if we divide 10 by any number  it gives that
            //number but by  removing the last digit.
            //eg -if:123/10 = 12
        printf("The number in reverse order:  %d",j);
    }
    

    Output:

    enter the number:123
    The number in reverse order: 321

    Exercise:

    Q:Declare a variable n and take its value from the user and if its is palindrome number then print Number is a palindrome number otherwise print Number is not an Palindrome number.

    Hint:A Palindrome number is number which remains same number if it is print in a reverse order.eg:101,151,212,313,etc are palindrome numbers.

  • Q:Create a menu driven program which should perform operations like Power,floor,ceil.

    Solution:

    #include<stdio.h>
    #include<math.h>
    void main()
    {
    int choice;      //variable choice
    int a,b;
    int result;
    float result1;
    printf("enter the value of a:");
    scanf("%d",&a);
    printf("\nenter the value of b:");
    scanf("%d",&b); //taking values of a and b from the user.
    printf("\n1:Power");
    printf("\n2:floor");
    printf("\n3:ceil");   //menus
    
    
    
    printf("\nEnter the  your choice:");
    scanf("%d",&choice);    //taking choice as input from the user
    
    
    switch(choice)       //switching the choice variable.
    {
    case 1: //if choice is 1 then this case will get executed.
    
        result  = pow(a,b);
        printf("\n %d raise to the power of  %d is %d",a,b,result);
        break;
    
    case 2: //if choice is 2 then this case will get executed.
        result  = floor(a);
        result1= floor(b);
        printf("\nfloor value for %d is %d\nfloor value for %d is %d",a,result,b,result1);
        break;
    
    
    case 3: //if choice is 3 then this case will get executed.
        result  = ceil(a);
        result1 = ceil(b);
        printf("\nceil value for %d is %d\n ceil value for  %d is %d",a,result,b,result1);
        break;
    
    
    default:
        printf("\nNot Valid choice.");
        break;
    }
     }
    

    Output:


    enter the value of a:10
    enter the value of b:5
    1:Power
    2:floor
    3:ceil
    Enter your choice:1
    10 raise to the power of 5 is 100000

    Exercise:

    Q:Create a menu driven program which should perform operations like to calculate log function, sine function, cosine function,exponential function of any number.

    Hint:use #include<math.h> library




Practice Programs


  • Programs Regarding If-else Statements
  • Program Regarding Nested If - statements
  • Programs Regarding loops



Further Topics