Tuesday, 24 February 2015

Conditional and Decision making in C++

If Condition:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
cout<<”Input first value:”;
cin>>i;
cout<<”Input second value:”;
cin>>j;
if(i>j)
cout<<”First value is greater”;
if(j>i)
cout<<”Second value is greater”;
getch();
}

Else if:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
cout<<”Input first value:”;
cin>>i;
cout<<”Input second value:”;
cin>>j;
if(i>j)
cout<<”First value is greater”;
else
cout<<”Second value is greater”;
getch();
}

Switch case statements:

#include<iostream.h>
#include<conio.h>
void main()
{
char ecode;
clrscr();
cout<<”Enter the ecode[a/m/p]:”;
cin>>ecode;
switch(ecode)
{
case ‘a’:
cout<<”Apprentice staff”;
break;
case ‘m’:
cout<<”Management staff”;
break;
case ‘p’:
cout<<”Permanent staff”:
break;
default:
cout<<”Invalid employee code”;
break;
}
getch();
}

For Loop:

#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
cout<<”Number Square”<<endl;
for(i=1;i<=5;i++)
{
cout<<i<<”\t”<<i*i<<endl;
}
getch();
}

While loop:

#include<iostream.h>
#include<conio.h>
void main()
{
int radius;
float area;
char wish=’y’;
while(wish= =’y’)
{
cout<<”Enter the radius of the circle:”;
cin>>radius;
area=3.14*radius*radius;
cout<<”Area is:”<<area<<endl;
cout<<”Wish to calculate area of another circle[y/n]:”;
cin>>wish;
}
getch();
}

Continue:

#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
cout<<”The following number are divisible by 5:”;
for(i=1;i<=25;i++)
{
if(i%5!=0)
continue;
cout<<i<<”\t”;
}
getch();

}

No comments:

Post a Comment