C Programming Practical Questions with solutions| C Programming Exercises, Practice, Solution

1. Write a C Program to print Welcome to C Programming.

    #include<stdio.h>

    int main()

    {

    printf(“Welcome to C Programming”);

    return 0;

    }

    Output:

    Welcome to C Programming

    2. Write a program in C to print the following:

    Welcome to C Programming

    It is invented by Denis Ritche

    It is portable, flexible and versatile

    #include<stdio.h>

    int main()

    {

    printf(“Welcome to C Programming”);

    printf(“\nIt is invented by Denis Ritche”);

    printf(“\nIt is portable, flexible and versatile”);

    return 0;

    }

    Output:

    Welcome to C Programming

    It is invented by Denis Ritche

    It is portable, flexible and versatile

    3. Write a C program to print the following:

    Welcome to

                 C Programming

    Invented by

              Denis Ritche

    #include<stdio.h>

    int main()

    {

              printf(“Welcome to”);

              printf(“\n\tC Programming”);

              printf(“\nInvented by”);

              printf(“\n\tDenis Ritche”);

              return 0;

    }

    Output:

    Welcome to

            C Programming

    Invented by

            Denis Ritche

    4. Write a C program to print the sum of two numbers.

          OR

    Write a program in C to add two numbers.

    #include<stdio.h>

    int main()

    {

              int n1,n2,result;

              num1=10;

              num2=8;

              result=n1+n2;

              printf(“Result: %d”,result);

              return 0;

    }

    OR

    #include<stdio.h>

    int main()

    {

              int n1=10,n2=8,result;

              printf(“Result: %d”,n1+n2);

              return 0;

    }

    Output:

    Result: 18

    5. Write a C program to add two numbers taking input from user.

    #include<stdio.h>

    int main()

    {

              int n1,n2,result;

              printf(“Enter 1st number: “);

              scanf(“%d”,&n1);

              printf(“Enter 2nd number: “);

              scanf(“%d”,&n2);

              result=n1+n2;

              printf(“Result: %d”,result);

              return 0;

    }

    Output:

    Enter 1st number: 34

    Enter 2nd number: 45

    Result: 79

    OR

    #include<stdio.h>

    int main()

    {

              int n1,n2,result;

              printf(“Enter two numbers: “);

              scanf(“%d%d”,&n1,&n2);

              printf(“Result: %d”,n1+n2);

              return 0;

    }

    Output:

    Enter two numbers: 34 45

    Result: 79

    6. Write a C program to display sum and difference of two numbers.

    #include<stdio.h>

    int main()

    {

              int n1=10,n2=7;

              printf(“Sum: %d”,n1+n2);

              printf(“\nDifference: %d”,n1-n2);

              return 0;

    }

    Output:

    Sum: 17

    Difference: 3

    OR

    #include<stdio.h>

    int main()

    {

              int n1,n2;

              printf(“Enter two numbers: “);

              scanf(“%d%d”,&n1,&n2);

              printf(“Sum: %d”,n1+n2);

              printf(“\nDifference: %d”,n1-n2);

              return 0;

    }

    Output:

    Enter two numbers: 34 23

    Sum: 57

    Difference: 11

    7. Write   a program in C to store two integer values in two variables and using them calculate addition, subtraction, multiplication and division.

    #include<stdio.h>

    int main()

    {

     int n1,n2;

    printf(“Enter two integer values: “);

     scanf(“%d%d”,&n1,&n2);

    printf(“Addition= %d”,n1+n2);

    printf(“\nSubtraction= %d”,n1-n2);

    printf(“\nMultiplication= %d”,n1*n2);

    printf(“\nDivision= %d”,n1/n2);

    return 0;  

    }

    Output:

    Enter two integer values: 56 34

    Addition= 90

    Subtraction= 22

    Multiplication= 1904

    Division= 1

    8. Write a program in C to find the average of three numbers.

    #include<stdio.h>

    int main()

    {

              int n1,n2,n3;

              float avg;

              printf(“Enter three numbers: “);

              scanf(“%d%d%d”,&n1,&n2,&n3);

              avg=(n1+n2+n3)/3.0;

              printf(“Average: %.2f”,avg);

              return 0;  

    }

    Output:

    Enter three numbers: 4 6 7

    Average: 5.67

    9. Write a C program to calculate the square of a number.

    #include<stdio.h>

    int main()

    {

    int num,square;

    printf(“Enter a number: “);

    scanf(“%d”,&num);

    square=num*num;

    printf(“Square of %d is %d”, num, square);

    return 0;  

    }

    Output:

    Enter a number: 7

    Square of 7 is 49

    10. Write a program in C to convert Rupees into Paisa.

      OR

      Write a C program to read the price of an item in rupees in decimal format and prints the output in paisa.

      OR

      Write a C program to read a price of an item in (float) like 10.25 and print output in (int) paisa like1025.

      OR

      Make a program in C to enter the amount in Rupees and print into paisa.

      #include<stdio.h>

      int main()

      {

      float rs;

      int ps;

      printf(“Enter price of the item in Rupees: “);

      scanf(“%f”,&rs);

      ps=rs*100;

      printf(“Price of the item in Paisa: %d”, ps);

      return 0;  

      }

      Output:

      Enter price of the item in Rupees: 20.50

      Price of the item in Paisa: 2050

      11. Write a program in C to find the distance travelled in miles given the speed and time. The speed is 4.5 MPH and time is 2.1 sec.

        #include<stdio.h>

        int main()

        {

        float speed,time,distance;

        speed=4.5;

        time=2.1;

        distance=speed*time;

        printf(“The distance travelled is:%.2f”,distance);

        return 0;  

        }

        Output:

        The distance travelled is:9.45

        12. Write a C program to find the Kinetic Energy of a particle. Given the Mass and Velocity as 14 and 3.

          #include<stdio.h>

          int main()

          {

                    float m=14,v=3,KE;

                    KE=(m*v*v)/2;

                    printf(“Kinetic Energy:%.3f”,KE);

                    return 0;  

          }

          Output:

          Kinetic Energy:63.000

          13. Write a program in C to calculate Kinetic Energy of a particle.

            #include<stdio.h>

            int main()

            {

                      float m,v,KE;

                      printf(“Enter the mass: “);

                      scanf(“%f”,&m);

                      printf(“Enter the velocity: “);

                      scanf(“%f”,&v);

                      KE=(m*v*v)/2;

                      printf(“Kinetic Energy:%.3f”,KE);

                      return 0;  

            }

            Output:

            Enter the mass: 25

            Enter the velocity: 5

            Kinetic Energy:312.500

            14. Write a C program to calculate total marks and percentage of 5 subjects of a student.

              #include<stdio.h>

              int main()

              {

              int eng,math,phy,chem,csc;

              float total, percentage;

              printf(“Enter english marks: “);

              scanf(“%d”,&eng);

              printf(“Enter maths marks: “);

              scanf(“%d”,&math);

              printf(“Enter physics marks: “);

              scanf(“%d”,&phy);

              printf(“Enter chemistry marks: “);

              scanf(“%d”,&chem);

              printf(“Enter computer science marks: “);

              scanf(“%d”,&csc);

              total=eng+math+phy+chem+csc;

              printf(“———————————“);

              printf(“\nTotal marks obtained: %.2f”,total);

              percentage=(total/500)*100;

              printf(“\nPercentage: %.2f”,percentage);

              return 0;  

              }

              Output:

              Enter english marks: 67

              Enter maths marks: 78

              Enter physics marks: 85

              Enter chemistry marks: 87

              Enter computer science marks: 92

              ———————————

              Total marks obtained: 409.00

              Percentage: 81.80

              OR

              #include<stdio.h>

              int main()

              {

              int eng,math,phy,chem,csc;

              float total,percentage;

              printf(“Enter marks of 5 subjects: “);

              scanf(“%d%d%d%d%d”,&eng,&math,&phy,&chem,&csc);

              total=eng+math+phy+chem+csc;

              printf(“———————————“);

              printf(“\nTotal marks obtained: %.2f”,total);

              percentage=(total/500)*100;

              printf(“\nPercentage: %.2f”,percentage);

              return 0;  

              }

              Output:

              Enter marks of 5 subjects: 55 65 45 70 85

              ———————————

              Total marks obtained: 320.00

              Percentage: 64.00

              15. Write a program in C to input name and marks of the student and print the statement of marks.

                #include<stdio.h>

                int main()

                {

                int eng,math,sc;

                float total,percentage;

                char name[20];

                printf(“Enter name of the student: “);

                 //scanf(“%s”,name);

                gets(name);

                printf(“Enter english marks: “);

                scanf(“%d”,&eng);

                printf(“Enter maths marks: “);

                scanf(“%d”,&math);

                printf(“Enter science marks: “);

                scanf(“%d”,&sc);

                total=eng+math+sc;

                percentage=(total/300)*100;

                printf(“—————————“);

                printf(“\nName: %s”,name);

                printf(“\nEnglish marks: %d”,eng);

                printf(“\nMaths marks: %d”,math);

                printf(“\nScience marks: %d”,sc);

                printf(“\n—————————“);

                printf(“\nTotal marks obtained: %.2f”,total);

                printf(“\nPercentage: %.2f”,percentage);

                return 0;  

                }

                Output:

                Enter name of the student: Pramila Barla

                Enter english marks: 78

                Enter maths marks: 67

                Enter science marks: 85

                —————————

                Name: Promila Barla

                English marks: 78

                Maths marks: 67

                Science marks: 85

                —————————

                Total marks obtained: 230.00

                Percentage: 76.67

                Leave a Comment