If Condition Programs Set 8

Question:34
C program to take input student details like rollno, name, marks of three subjects (m1,m2,m3). Calculate and print total and per. Also print the grade based on the given conditions:
condition Grade
if(per>=90)                             A+
if(per>=80 and per<90)       A
if(per>=70 and per<80)       B+
if(per>=60 and per<70)       B
if(per>=50 and per<60)       C
if(per>=40 and per<50)       D
if(per<40)                               F
Sol:

#include <stdio.h>
 int main()
 {
     int roll;
     char name[20];
     float m1,m2,m3,total,per;
     printf("Enter roll no ");
     scanf("%d",&roll);
     printf("Enter name ");
     scanf("%s",name);
     printf("Enter marks of m1,m2 and m3 ");
     scanf("%f %f %f",&m1,&m2,&m3);
     total=m1+m2+m3;
     per=total/3;
     printf("Total = %.3f  Per = %.2f\n",total,per);
     if(per>=90)
         printf("Grade : A+");
     else
         if(per>=80 && per<90)         
          printf("Grade : A");     
      else         
         if(per>=70 && per<80)         
          printf("Grade : B+");     
       else         
          if(per>=60 && per<70)         
           printf("Grade : B");     
        else         
           if(per>=50 && per<60)         
            printf("Grade : C");     
         else         
            if(per>=40 && per<50)
            printf("Grade : D");
         else
            printf("Grade : F");
 return 0;
 }

Output:

Enter roll no 101
Enter name Amit
Enter marks of m1,m2 and m3 98
97
95
Total = 290.000 Per = 96.67
Grade : A+

Question:35
C program to take input for 2 numbers and an operator(+,-,*,/). Based on the entered operator calculate and print the result?
Sol:

#include <stdio.h>
 int main()
 {
     float a,b,c;
     char op;
     printf("Enter 2 nos ");
     scanf("%f %f",&a,&b);
     printf("Enter the operator (+,-,*,/) ");     
     fflush(stdin);     
     scanf("%c",&op);     
     if(op=='+')     
     {         
         c=a+b;         
         printf("Sum = %f",c);     
     }     
     else         
        if(op=='*')
     {
         c=a*b;
         printf("Prod = %f",c);
     }
     else
         if(op=='-')
     {
         if(a>b)
             c=a-b;
         else
             c=b-a;
         printf("Diff = %f",c);
     }
     else
         if(op=='/')
     {
         c=a/b;
         printf("Div = %f",c);
     }
     else
         printf("Invalid operator");
     return 0;
 }

Output:

case 1:

Enter 2 nos 10
20
Enter the operator (+,-,*,/) +
Sum = 30.000000

case 2:

Enter 2 nos 10
20
Enter the operator (+,-,*,/) *
Prod = 200.000000

case 3:

Enter 2 nos 50
5
Enter the operator (+,-,*,/) /
Div = 10.000000

case 4:

Enter 2 nos 25
6
Enter the operator (+,-,*,/) –
Diff = 19.000000

case 5:

Enter 2 nos 25
6
Enter the operator (+,-,*,/) )
Invalid operator


Next : goto and labelname statements

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects