10 Basic C programs with solutions

Some basic C programs with their solutions

 

1).  A student will not be allowed to sit in an exam if his/her attendance is less than 75%.

 Take the following input from user :
 Number of classes held Number of classes attended. And the print percentage of class attended, Is student is allowed to sit in exam or not?


#include <stdio.h>

int main(){

    float classheld , attended; 

    printf("Enter the number of classes held \n");

    scanf("%f",  &classheld);

    printf("Enter the value of classes attended \n");

    scanf("%f", &attended);

     float Total = (attended/classheld) * 100;

     printf("you have attended  %.f%% of classes \n\n" , Total );

     if (Total < 75)

     {

       printf("Your attendance is less than 75%% , You can not sit in exam hall \n");

     }

     else{

          printf("Your attendance is more than 75%% ,  You can sit in exam Hall");

     }

    return 0;

}


 2). Write a program to find the absolute value of a number entered through the keyboard 

#include <stdio.h>

int main(){

    int i, num;

     printf("Enter the value of number \n");

     scanf("%d" , &num);

  if(num < 0){

           num =  num*(-1);

  }

  else if(num > 0) {

           num =  num*(1);

  }

  printf("The absolute  value of  entered number  is %d \n", num);

    return 0;

}

3). write a program using for loop which prints the multiplication table of 4  

#include <stdio.h>

int main(){

     for(int i = 1 ,  j =4 ; i<=10 ; ++i){

             printf("%d \n" , i*j);

     }

    return 0;

} 

 

4). write  a program to print multiplication table of a number input by the user 

#include <stdio.h>

int main(){

     int i,j, mul;

     printf("Enter the number for the multiplication table  you want to display \n");

     scanf("%d" , &j);

      for(i=1 ; i<=10 ; i++){

         mul = i*j

              printf("%d \n" , mul);

      }

    return 0;

}

 

5).  Create a simple calculator using switch statements - C programming 

#include <stdio.h>

int main(){

   float value1 , value2;

   char  operator;

   printf("Enter your expression that you want  to be calculated \n");

   printf("The order of the expression is :  Value1  operator Value2 \n");

   scanf("%f %c %f", &value1,  &operator, &value2);

  switch(operator){

      case '+' :

           printf("%.2f" , value1 + value2);

           break;

      case '-' : 

            printf("%.2f" , value1 -  value2);

            break;

       case '*' : 

            printf("%.2f" , value1 * value2);

            break;

       case '/' : 

            if(value2 == 0){            

                    printf("Can not divide  by zero \n");

            }              

            else{ 

                printf("%.2f" , value1/value2);

            }

             break;

         default : 

              printf("Unknown operator or invalid values \n");

              break;

  }

    return 0;

 

6).  Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard.  A triangle is valid if the sum of all three angles is equal to 180  degrees

#include <stdio.h>

int main(){

     int  length , breadth , height ; 

     printf("Enter the length of  the triangle : \n");

     scanf("%d", &length);

     printf("Enter the breadth of the triangle : \n");  

     scanf("%d" , &breadth);

     printf("Enter the height of the triangle : \n");

     scanf("%d" , &height);

     if (length +  breadth + height  ==  180)

     {

           printf("It is a triangle \n");

     }

     else {    

             printf("It is not a triangle \n");

     }

    return 0;

}

 

7).  If the ages of Ram, Shyam, and Ajay are input through the keyboard, write a program to determine the youngest of them 

#include <stdio.h>

int main(){

      int ram , shyam , ajay;

   printf("Enter the age of Ram Shyam and Ajay in  correct order : \n ");

   scanf("%d %d %d" , &ram , &shyam,  &ajay);

   if (ram <  shyam && ram <  ajay)

   {

       printf("Ram is the youngest \n");

   }

   if (shyam < ram && shyam < ajay)

   {

      printf("Shyam is the younest  \n");

   }

   if (ajay < ram && ajay < shyam)

    {

        printf("Ajay is the youngest \n");

    }

    return 0;

}

 

8).  Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not. 

#include <stdio.h>

int main(){

       int year;

         printf("Enter the year (Ex. 2002) \n");

         scanf("%d" , &year);

         if ((year%4)==0  &&  (year%100)!=0 ||    (year%400)==0 )

         {

               printf(" %d is a leap year \n" , year);

         }

      else{

              printf(" %d is not a leap year \n", year);

      }

    return 0;

}

 

9).  Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

#include <stdio.h>

int  main() {

     int  number;

      printf("Enter the number \n");

      scanf("%d" , &number);        

       if(number%2 == 0)

       {

                  printf("The  entered number is even \n");

       }

       else{

                 printf("The entered number is odd \n");

       }

         return 0;           

}

 

10).  Create a C program that converts the number of minutes to days and years

#include <stdio.h>

int main(){

    int minutes;

   double days , years, minutesInDay , minutesInYear;

    minutesInDay = 1440;

    minutesInYear =  60*24*365;

    printf("Enter the number of  MINUTES : \n" );

    scanf("%d" , &minutes);

     days =  minutes/minutesInDay;

    years =   minutes/minutesInYear;

     printf("There are %.2lf number of days in  %d minutes \n" , days , minutes);

     printf("There are %.2lf number of years in  %d minutes \n" , years , minutes);

    return 0;

}


 

Post a Comment

Please dont write spam messages

Previous Post Next Post