January 21, 2013

Decision Making- If statement

decision making banner in if statement



It is used whenever any condition is to be implemented by the user it means some conditions like:
1. If the price of the item is more than 5000 then discount is 10%.
2. If the students marks are greater than 60% then he should be in 1st division.


For all these conditions a decision making statement called ‘if’ statement is used.
‘if’ statement is that statement which executes the expression given with if.
It will only execute if it’s true.

But if the expression given with if statement is not true then this statement does not contribute to the result i.e. output.
Here keyword “if” is used.


/*Program to enter roll number and name of student with his 5 subject marks  by the user to find percentage and get grades*/

#include<stdio.h>
#include<conio.h>
int main()
{
     int rno;
     char nm[40];
     float s1,s2,s3,s4,s5, percent=0;
     printf("\n enter students name and roll number");
     scanf("%s%d",nm,&rno);
     printf("\n enter marks in physics, chemistry, maths, english, physical");
     scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
     percent=(s1+s2+s3+s4+s5)/5.0;
     printf("\n roll number is %d,\n name is %s,\n s1 is %f,\n s2 is %f,\n s3 is %f,\n s4 is %f,\n s5 is %f"
     ,rno,nm,s1,s2,s3,s4,s5);
     printf("\n percentage is %f",percent);
     if(percent>80)
     {
                   printf("\n grade of the student is A");
                   }
                   getch();
                   }
percentage of a student with his marks and show its grade using if statement in C language


/*Program to enter principal and time period by user and find simple interest of that according to different rate of interest */

#include<stdio.h>
#include<conio.h>
void main()
{
     float prin,time, rt=2.5, si=0;
     printf("\n enter any principal and time period");
     scanf("%f%f",&prin,&time);
      if(time>5)
     {
               rt=7.5;
               si=prin*time*rt/100;
               }
     si=prin*time*rt/100.0;
     printf("\n principal is %f,time period is %f",prin,time);
     printf("\n rate is %f",rt);
     printf("\n simple interest is %f",si);
    
               getch();
               }
simple interest with varying time period using if statement in C programming language


/*Program to enter item name,code,price by the user and calculate total amount */


#include<stdio.h>
#include<conio.h>
void main()
{
     int code;
     char nm[40];
     float pr,d=0,dI,tamt=0;
     printf("\n enter item name and item code");
     scanf("%s%d",nm,&code);
     printf("\n enter price of the item");
     scanf("%f",&pr);
     if(pr>200.0)
     {
                  d=8.0;
                  dI=pr*8.0/100;
                  tamt=pr-dI;
                  }
     tamt=pr-dI;
     printf("\n item name is %s,\n item code is %d,\n price is %f",nm,code,pr);
     printf("\n discount is %f",d);
     printf("\n total amount is %f",tamt);
     getch();
     }
discount applied to customer on total amount using if statement in C programming language




PRECAUTIONS

1. Use accurate header files.
2. Always close the brackets of “if” if taken into existence.
3. Use proper algorithm to define algebraic equation.
4. Don’t forget to declare printf() statement for the final result.
5. Use getch() for displaying result. 

No comments:

Post a Comment