January 29, 2013

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

decision making-if else statement in C++ (OOPs) banner

This statement is also a conditional statement. It starts with keyword ‘if’ followed by an expression followed by “ first statement”  and “ second statement” is given after ‘else’ clause.

Format:
if(expression)
statement1;
else
statement2;


if the expression is true then statement1 will be executed.
If the expression is false then statement2 will be executed.

Similarly, if there are more statement before ‘else’ then a block is to be formed and similarly if there are many statements after the ‘else’ clause then it’s separate block is formed.
Such statement is called “Compound if else” statement.


/* Program to enter two values and find which is greater*/


#include<iostream.h>
#include<conio.h>
main()
{
      int n1,n2;
      cout<<"\n Enter any two number you want to check";
      cin>>n1>>n2;
      if(n1>n2)
      {
               cout<<"\n "<<n1<<" is greater than "<<n2;
               }
      else
      {
          cout<<"\n "<<n2<<" is greater than "<<n1;
          }
      getch();
      }
output of program to show which number is greater using C++ (OOPs)

/* Program to tell a number whether its even or odd*/

#include<iostream.h>
#include<conio.h>
main()
{
      int n;
      cout<<"\n Enter the number you want to check";
      cin>>n;
      if(n%2==0)
      {
                cout<<"\n "<<n<<" is even";
                }
      else
      {
          cout<<"\n "<<n<<" is odd";
          }
      getch();
      }
output of program to show number is even of odd using if else statement in C++ (OOPs)

/* Program to check the number is positive or negative */

#include<iostream.h>
#include<conio.h>
main()
{
      float n;
      cout<<"\n Enter any real value";
      cin>>n;
      if(n>=0)
      {
              cout<<"\n "<<n<<" is positive";
              }
      else
      {
          cout<<"\n "<<n<<" is negative";
          }
      getch();
      }
output of program to check the number is positive or negative using if else statement in C++ (OOPs)

/*Program to enter item details and print its bill with complete details according to the discount offered to the customer */

#include<iostream.h>
#include<conio.h>
main()
{
      int itcd;
      float pr,qty,d,dp,amt,tamt;
      char itnm[10];
      cout<<"\n Enter Item's Name and Code";
      cin>>itnm>>itcd;
      cout<<"\n Enter Item's Quantity(in Kg) and Price";
      cin>>qty>>pr;
      amt=pr*qty;
      if(amt>=500)
      {
           d=12.5;
           dp=amt*d/100;
           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;
           }
      else
      {
          d=7.5;
          dp=amt*d/100;
          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;
          }
      getch();
      }
output of program to enter item details and print the total amount with discount using if else statement in C++ (OOPs)

No comments:

Post a Comment