C++ if Statement
/* C++ Selection Statements - C++ if Statement */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
getch();
}
C++ if-else Statement
/* C++ Selection Statements - C++ if-else Statement */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
else
{
cout<<"You entered an odd number";
}
getch();
}
C++ switch Statement
/* C++ Selection Statement - C++ switch Statement */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int dow;
cout<<"Enter number of week's day (1-7): ";
cin>>dow;
switch(dow)
{
case 1 : cout<<"\nSunday";
break;
case 2 : cout<<"\nMonday";
break;
case 3 : cout<<"\nTuesday";
break;
case 4 : cout<<"\nWednesday";
break;
case 5 : cout<<"\nThursday";
break;
case 6 : cout<<"\nFriday";
break;
case 7 : cout<<"\nSaturday";
break;
default : cout<<"\nWrong number of day";
break;
}
getch();
}
C++ Loops
/* C++ Loops Example */
Let's start with for loop program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
for(l=0; l<10; l++)
{
num++;
cout<<num<<"\n";
}
getch();
}
Program demonstrating while loop in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
while(l<10)
{
num++;
cout<<num<<"\n";
l++;
}
getch();
}
Program, demonstrating do-while loop in C++
/* C++ Loops Example */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
do
{
num++;
cout<<num<<"\n";
l++;
}while(l<10);
getch();
}