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 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();
}
/* 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();
}
/* 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();
}
/*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();
}
No comments:
Post a Comment