January 29, 2013

Decision Making- If statement in C++ (OOPs)

decision making if statement in C++ (OOPs) programming language banner

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 calculate simple interest having varying time period using IF statement*/


#include<iostream.h>
#include<conio.h>
main()
{
      float princ,time,rate=2.5,si;
      cout<<"\n Enter Principle and Time period of loan";
      cin>>princ>>time;
      if(time>=5)
      {
                 rate=5.5;
                 si=(princ*rate*time)/100.0;
                 }
      si=(princ*rate*time)/100.0;
      cout<<"\n Simple interst of the loan is: "<<si<<" with Rate of interest of: "<<rate;
      getch();
      }
output of program having if condition for finding simple interest in C++ (OOPs) programming

/* Program to enter student details and allow grade according to its performance*/

#include<iostream.h>
#include<conio.h>
main()
{
      int roll;
      int s1,s2,s3,s4,s5;
      float per;
      char name[20];
      cout<<"\n Enter student's Name and Roll number";
      cin>>name>>roll;
      cout<<"\n Enter student marks in Physics, Chemistry, Maths, English, Physical";
      cin>>s1>>s2>>s3>>s4>>s5;
      per=(s1+s2+s3+s4+s5)/5.0;
      cout<<"\n Name: "<<name;
      cout<<"\n Roll Number: "<<roll;
      cout<<"\n Percentage: "<<per;
      if(per>=80.0)
      {
                   cout<<"\n Student is Above Average";
                   }
      getch();
      }
output of program to enter details of student and allow grades using IF statement in C++ (OOPs)

/* Program to enter Item to be purchased details and print its complete bill with appropriate discount according to the total price*/

#include<iostream.h>
#include<conio.h>
main()
{
      int itcd;
      float qty,pr,amt,tamt,dp,d=5.25;
      char itnm[10];
      cout<<"\n Enter Item name and its code";
      cin>>itnm>>itcd;
      cout<<"\n Enter Item's quantity(in Kg) and price";
      cin>>qty>>pr;
      amt=qty*pr;
      if(amt>=500.0)
      {
                    d=8.5;
                    dp=amt*d/100.0;
                    tamt=amt-dp;
                    }
      dp=amt*d/100.0;
      tamt=amt-dp;
      cout<<"\n Code\tName\tQty.\tPrice\tAmount\tDis%\tT.Amount";
      cout<<"\n "<<itcd<<"\t"<<itnm<<"\t"<<qty<<"\t"<<pr<<"\t"<<amt<<"\t"<<d<<"\t"<<tamt;
      cout<<"\n Total amount to be paid: "<<"Rs."<<tamt;
      getch();
      }
output of program to enter item details and print its bill according to discount offered using IF statement in C++ (OOPs)


No comments:

Post a Comment