Chapter 2

Example 1 :
#include<stdio.h>
#include<conio.h>
void main()
{
int avg=346;
float per=69.2;
printf("\nAverage : %d\nPercentage : %f",avg,per);
}
Example 2 :
#include<iostream.h>
#include<conio.h>
void main()
{
int sum=0,ch,i,j,num,no;
char ans='y';
clrscr();
cout<<"\nEnter a number : ";
cin>>no;
num=no;
do
{
sum=sum+(no%10)*(no%10)*(no%10);
no=no/10;
}
while(no!=0);
if(sum==num)
cout<<"\nThe number is an Armstrong number ";
else
cout<<"\nThe number is not an Armstrong number ";
getch();
}

Example 3 : Program: The break statement
#include <iostream.h>
void main()

{
int i , j , n = 1 ;
for(i=1 ; i<5 ;)
{
for (j=1 ; ; j++)
{
cout<<n<<" ";
n++ ;
if (j >= 5)
break;
}
i++;
}
}
Example 4 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,j=6,k=0;
char x='A';
clrscr();
while((j/2)>=0)
{
for(i=0;i<=(j/2);i++)
cout<<" "<<char(x+i);
i=i-2;
for(;i>=0;i--)
cout<<" "<<char(x+i);
j=j-2;
k=k+1;
cout<<"\n";
for(i=0;i<=k;i++)
cout<<" ";
}
getch();
}

Example 5 : Program: Checking of entered character

#include <iostream.h>
void main()
{
char c ;
cout<<"Enter any character :\n";
cin>>c ;
if ((c >= 'a') && (c <= 'z'))
cout<<"The character is lower case.\n";
else if ((c >= 'A') && (c <= 'Z'))
cout<<"The character is upper case.\n";
else if ((c >= '0') && (c <= '9'))
cout<<"The character is numeric.\n";
else
cout<<"The character is a symbol.\n";
}
Example 6 :
# include <iostream.h>
#include<conio.h>
void main ( )
{
char c ;
clrscr ( ) ;
cout<<"Enter any character :";
cin>>c;
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '9':cout<<"It is a digit ";
break ;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':

case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':cout<<"It is an alphabet ";
break ;
default :cout<<"It is a special character";
}
}

Example 7 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
cout<<i<<" "<<j<<" "<<k<<endl;
getch();
}

Example 8 :

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,n;
char a[7];
clrscr();
cout<<"Enter the number : ";
cin>>a;
i=0;
while(a[i]!='###BOT_TEXT###')
{
if(a[i]==',')
{
int j=i;
while(a[j]!='###BOT_TEXT###')
{
a[j]=a[j+1];
j++;
}
}
else
i++;
}
cout<<"\nThe number after removing ',' is : "<<a;
getch();
}

Example 9 : Program : The continue statement
#include <iostream.h>
void main()
{
int x , y ;
for (x=1 ; x<3 ; x++)
{
for (y=1 ; y<3 ; y++)
{
if (x == y)
continue ;
cout<<x<<y;

}
}
}

Example 10 :
#include<iostream.h>
#include<conio.h>
void main()
{
int x, p = 0, count = 0;
clrscr();
cout<<"Enter a number : ";
cin>>x;
L1 : if (x%2==0)
x=x/2;
else
x=x*3+1;
p=x;
count=count+1;
if(p!=1)
goto L1;
else if (p==1)
cout<<"\nFunction converged to 1";
cout<<"\nNumber of iterations : "<<count;
getch();
}

Example 11 : Program : The do-while loop
#include <iostream.h>
void main()
{
int c = 0 ;
do
cout<<++c<<" ";
while (c < 11 ) ;
}

Example 12 : Program: The do-while loop
#include <iostream.h>
#include<conio.h>
void main()
{
int x ;
do
{
cout<<"Enter any integer value :\n";
cin>>x ;
cout<<"Number : "<<x ;
cout<<"Square : "<<x*x ;
cout<<"Cube : "<<x*x*x ;
}
while (x != 0) ;
getch () ;
}

Example 13 : Program: The do-while loop
#include <iostream.h>
#include <conio.h>
void main()
{
int num , temp ;
cout<<"Enter any integer value :\n";
cin>>num;
do
{
temp = num % 10 ;
cout<<temp ;
num = num / 10 ;
}
while (num != 0) ;
getch () ;
}

Example 14 : Program : Factorial
#include <iostream.h>
void main()
{
int i , number , factorial ;
cout<<"Enter a number :\n";
cin>>number;
for (i=1,factorial=1 ; i<=number ; i++)
factorial = factorial * i ;
cout<<"The factorial of "<<number<<" : "<<factorial;
}

Example 15 :
#include<stdio.h>
#include<conio.h>
void main()
{
int height=6;
clrscr();
printf("\nHeight is %d feet",height);
printf("\nHeight is %2d feet",height);
printf("\nHeight is %4d feet",height);
printf("\nHeight is %6d feet",height);
printf("\nHeight is %-6d feet",height);
getch();
}

Example 16 :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n%f%f%f",5.0,13.5,133.9);
printf("\n%f%f%f",305.5,1200.9,3005.3);
getch();

}
Example 17 :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n%10.1f%10.1f%10.1f",5.0,13.5,133.9);
printf("\n%10.1f%10.1f%10.1f",305.5,1200.9,3005.3);
getch();
}

Example 18 : Program : The for loop
#include <iostream.h>
void main()
{
int i , sum = 0 ;
for (i=1 ; i<21 ; i++)
sum = sum + i ;
cout<<"The sum of first twenty numbers is "<<sum;
}

Example 19 : Program: The for loop
#include <iostream.h>
void main()
{
int i = 0 ;
cout<<"Table of 15 -\n";
for (i=1 ; i<=10 ; i++)
cout<<"15 * "<<i<<" : "<<15*i;
}

Example 20 : Program : The for loop
#include <iostream.h>
void main()
{
int i = 0 ;
cout<<"Table of 15 -\n";
for (i=1 ; i<=10 ; ++i)
cout<<"15 * "<<i<<" : "<<15*i;
}

Example 21 : Program : The for loop
#include <iostream.h>
void main()
{
int i , sum = 0 ;
for (i=20 ; i>0 ; i--)
sum = sum + i ;
cout<<"The sum of first twenty numbers is "<<sum;
}

Example 22 : Program : The for loop
#include <iostream.h>
void main()
{
int i ;
i=1;
for (; i<21 ; i=i+1)
cout<<i<<" ";
}

Example 23 : Program : The for loop
#include <iostream.h>
main()
{
int i ;
i=0;
for (; i++<10 ;)
cout<<i<<" ";
}
Example 24 : Program : The for loop
#include <iostream.h>
void main()
{
int i ;
i=0;
for (; ++i<=10 ;)
cout<<i<<" ";
}

Example 25 : Program: The for loop
#include <iostream.h>
void main()
{
int i , sum = 0 ;
for (i=1 ; i<21 ;)
{
sum = sum + i ;
i=i+1;
}
cout<<"The sum of first twenty numbers is "<<sum ;
}

Example 26 : Program: The for loop as a block
#include <iostream.h>
void main()
{
int num , square , cube ;
cout<<"The squares and cubes of first ten numbers :\n";
cout<<"==========================================\n\n";
cout<<"Number\tSquare\tCube\n";
for (num=1 ; num<11 ; num++)
{
square = num * num ;
cube = num * square ;
cout<<num<<"\t"<<square<<"\t"<<cube;
}
}

Example 27 : Program: The for loop as a block
#include <iostream.h>
#include<conio.h>
void main()
{
int x , y , z = 0 ;
clrscr() ;
cout<<"The Fibonacci Series :\n";
cout<<"====================\n";
cout<<"0\n1\n";
for (x=0,y=1 ; y<1000 ; x=z)
{
z=y;
y=x+y;
cout<<y<<endl ;
}
}

Example 28 : Program: The for loop as a block

#include <iostream.h>
void main()
{
int i , j ;
for (i=1 ; i<=3 ; i++)
/* Outer For Loop */
for (j=1 ; j<=2 ; j++)
/* Inner For Loop */
cout<<"Hello!"<<endl;
}

Example 29 : Program : The for loop as a block
#include <iostream.h>
void main()
{
int i , j , num ;
cout<<"Enter the number of rows to be printed (from 1 to 10): \n";
cin>>num;
for (i=1 ; i<=num ; i++)
{
for (j=1 ; j<=i ; j++)
cout<<"*\t";
cout<<"\n";
}
}

Example 30 : Program : The goto statement
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int num ;
char ans ;
again : clrscr () ;
printf ("Enter a number :\n") ;

cin>>num;
if (num % 2 == 0)
cout<<"The number "<<num<<" is EVEN\n";
else
cout<<"The number "<<num<<" is ODD\n";
cout<<"Do you want to continue (Y or N) ? : " ;
cin>>ans;
if ((ans == 'y') || (ans == 'Y'))
goto again ;
else
cout<<"Thank You !!\n";
}

Example 31 : Program : Illustration of if
#include<iostream.h>
void main()
{
int no;
cout<<"\nEnter a number : ";
cin>>no;
if(no<100)
cout<<"\nThe number is less than 100!!!";
}

Example 32 : Program: Test for divisibility
#include <iostream.h>
void main()
{ // reports if one input number is not divisible by another:
int n, d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n%d)
cout << n << " is not divisible by " << d << endl;
}

Example 33 : Program: Multiple statements within if
#include<iostream.h>
void main()
{
int marks;
cout<<"\nEnter Marks : ";
cin>>marks;
if(marks<40)
{
int lessmarks=40-marks;
cout<<"\nSorry!!!!!you are failed";
cout<<"\nYou should have got "<<lessmarks<<" more marks";
}
}

Example 34 : Program : The If-else statement - find minimum number
#include <iostream.h>
void main()
{
int x , y , min ;
cout<<"Enter any two numbers : ";
cin>>x>>y ;
if (x<y)
cout<<"The minimum number is "<<x ;
else
cout<<"The minimum number is "<<y ;
}

Example 35 : Program: The If-else statement - Even or Odd number
#include <iostream.h>
void main()
{
int a , temp ;
cout<<"Enter a number : " ;
cin>>a ;
temp = a % 2 ;
if (temp == 0)

cout<<"The number is Even." ;
else
cout<<"The number is Odd.";
}

Example 36 : Program : The If-else statement - find minimum and maximum no.
#include <iostream.h>
#include<conio.h>
void main()
{
int a , b , c , min , max ;
cout<<"Enter three numbers : " ;
cin>>a>>b>>c ;
if (a < b)
min = a ;
else
min = b ;
if (c < min)
min = c ;
cout<<"The minimum number is "<<min ;
getch () ;
if (a > b)
max = a ;
else
max = b ;
if (c > max)
max = c ;
cout<<"The maximum number is "<<max ;
getch () ;
}

Example 37 : Program: The If-else statement-find whether divisible by 3

#include <iostream.h>
void main()
{
int x , temp ;
cout<<"Enter a number : " ;
cin>>x ;
temp = x % 3 ;
if (temp == 0) ;
else
cout<<"The number is not divisible by 3" ;
}

Example 38 : Program: The If-else statement as a block
#include <iostream.h>
void main()
{
int x , y ;
cout<<"Enter a number : ";
cin>>x ;
if (x <= 6)
{
int y ;
y = x << 2 ;
cout<<"After two left shifts : "<<y;
}
else
{
int y ;
y = x >> 1 ;
cout<<"After a single right shift : "<<y ;
}
}

Example 39 : Program: To find largest number of given 5 numbers

#include <iostream.h>
void main ()
{
int n1 , n2 , n3 , n4 , n5 , max ;
cout<<"Enter any five integer values :\n";
cin>>n1>>n2>>n3>>n4>>n5;
if (n1 > n2)
max = n1 ;
else
max = n2 ;
if (n3 > max)
max = n3 ;
if (n4 > max)
max = n4 ;
if (n5 > max)
max = n5 ;
cout<<"The maximum number of the given five numbers is "<<max;
}

Example 40 : Program: To display a Message
#include<stdio.h>
#include<conio.h>
void main ()
{
printf ("Welcome to the World of C.") ;
getch();
}

Example 41 :
#include<iostream.h>
#include<conio.h>
main()
{
int n=0;

clrscr();
cout<<"\nEnter the Month Number(1-12) : ";
cin>>n;
switch(n)
{
case 1:
cout<<"\nMonth is Jan";
break;
case 2:
cout<<"\n Month is Feb";
break;
case 3:
cout<<"\nMonth is March";
break;
case 4:
cout<<"\nMonth is April";
break;
case 5:
cout<<"\nMonth is May";
break;
case 6:
cout<<"\nMonth is June";
break;
case 7:
cout<<"\nMonth is July";
break;
case 8:
cout<<"\nMonth is Aug";
break;
case 9:
cout<<"\n Month is September";
break;
case 10:
cout<<"\n Month is Oct";
break;

case 11:
cout<<"\nMonth is November";
break;
case 12:
cout<<"\n month is Dec";
break;
default:
cout<<"\n Wrong input!!!";
}
getch();
return(0);
}

Example 42 : Program : The nested If statement
#include <iostream.h>
main()
{
int a ;
cout<<"Enter a number : " ;
cin>>a ;
if (a%3 == 0)
if (a%5 == 0)
cout<<"The number is divisible by 3 and 5" ;
}

Example 43 : Program: Minimum of three numbers-Using nested if else statements
#include <iostream.h>
void main()
{
int n1, n2, n3;
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
if (n1 < n2)

if (n1 < n3)
cout << "Smallest number is : " << n1 << endl;
else
cout << "Smallest number is : " << n3 << endl;
else // n1 >= n2
if (n2 < n3)
cout << "Smallest number is " << n2 << endl;
else
cout << "Smallest number is " << n3 << endl;
}

Example 44 : Program to print the grade for an input test score: using the else if
construct
#include <iostream.h>
void main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
if (score > 100)
cout << "Error: that score is out of range.\n";
else
if (score >= 90)
cout << "Your grade is an A.\n";
else
if (score >= 80)
cout << "Your grade is a B.\n";
else
if (score >= 70)
cout << "Your grade is a C.\n";
else
if (score >= 60)
cout << "Your grade is a D.\n";
else
if (score >= 0)
cout << "Your grade is an F.\n";
else

cout << "Error: that score is out of
range.\n";
}

Example 45 : Program to prints the grade for an input test score: Using the else if
construct
#include <iostream.h>
void main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
if (score > 100)
cout << "Error: that score is out of range.\n";
if((score >= 90)&&(score<=100))
cout << "Your grade is an A.\n";
if((score >= 80)&&(score <= 90))
cout << "Your grade is a B.\n";
if((score >= 70)&&(score <= 80))
cout << "Your grade is a C.\n";
if((score >= 60)&&(score <= 70))
cout << "Your grade is a D.\n";
if((score >= 0)&&(score <= 60))
cout << "Your grade is an F.\n";
}

Example 46 : Program: Permutation and Combination
#include <iostream.h>
void main()
{
int i , n , r , P , C ;

int factorial , temp , result ;
cout<<"Enter a number,n and r :\n";
cin>>n>>r;
for (i=1,factorial=1 ; i<=n ; i++) /* Calculate n! */
factorial = factorial * i ;
for (i=1,temp=1 ; i<=r ; i++)
/* Calculate r! */
temp = temp * i ;
for (i=1,result=1 ; i<=(n-r) ; i++)
/* Calculate (n-r)!*/
result = result * i ;
P = factorial / temp ;
C = P / result ;
cout<<"Permutation : "<<P;
cout<<"Combination : "<<C;
}
Example 47 :
#include<iostream.h>
#include<conio.h>
void main()
{
int m=0,x,b;
clrscr();
do
{
x=0;b=1;
while(x<=m)
{
if(m==0||x==0)
cout<<" "<<b;
else
{
b=b*(m-x+1)/x;
cout<<" "<<b;
}
x=x+1;
}
cout<<"\n\n";
m=m+1;
}
while(m<=5);
getch();
}

Example 48 :
#include<iostream.h>
#include<conio.h>
void main()
{
int y;
float i,x;
clrscr();
cout<<"i\ty\tx"<<endl;
cout<<"--------------------"<<endl;
for(y=1;y<=2;y++)
for(x=5.5;x<=12.5;x=x+0.5)
{
i=2+(y+0.5*x);
cout<<i<<"\t"<<y<<"\t"<<x<<endl;
}
getch();
}

Example 49 : Program: To calculate x^n
#include <iostream.h>
#include <math.h>
void main()
{
int num , power , result ;
cout<<"Enter the number and the power :\n";
cin>>num>>power;
result = pow (num,power) ;
cout<<"Result : "<<result;
}

Example 50 : Program: To calculate x^n without using the pow() function
#include <iostream.h>
void main()
{

int i , number , power , result ;
cout<<"Enter the base number :\n";
cin>>number;
cout<<"Enter the power :\n";
cin>>power;
result = number ;
for (i=1 ; i<power ; i++)
result = result * number ;
cout<<"Result : "<<result;
}

Example 51 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i,num;
clrscr();
cout<<"\nEnter a number : ";
cin>>num;
i=2;
while(i<=num-1)
{
if(num%i==0)
{
cout<<"\nNot a prime no.";
break;
}
i++;
}
if(i==num)
cout<<"\nPrime number";
getch();
}

Example 52 : Program : Roots of given Quadratic equation
#include <iostream.h>
#include <math.h>
void main()
{
float a , b , c , d , e ;
float temp , r1 , r2 ;
int select ;
char ans ;
do
{
cout<<"Enter the coefficients of the Quadratic equation :\n";
cin>>a>>b>>c;
temp = (b * b) - (4 * a * c) ;
if (temp > 0)
select = 0 ;
else
if (temp == 0)
select = 1 ;
else
select = 2 ;
switch (select)
{
case 0 :
temp = sqrt (temp) ;
r1 = (-b + temp) / (2 * a) ;
r2 = (-b - temp) / (2 * a) ;
cout<<"The roots are Real and Unequal.\n";
cout<<"The roots are : "<<r1<<" "<<r2;
break ;
case 1 :
r1 = -b / (2 * a) ;
r2 = r1 ;
cout<<"The roots are Real and Equal.\n";
cout<<"The roots are : "<<r1<<" "<<r2;
break ;
case 2 :
temp = -temp ;
temp = sqrt (temp) ;
temp = temp / (2 * a) ;
r1 = -b / (2 * a) ;
cout<<"The roots are Real and Imaginary.\n";
cout<<"The roots are : "<<r1<<"+j"<<temp<<" ,
"<<r1<<"-j"<<temp;
break ;

}
cout<<"Do you want to continue (Y or N) ? :\n";
cin>>ans;
}
while ((ans == 'y') || (ans == 'Y')) ;
}

Example 53 :
#include<iostream.h>
#include<conio.h>
void main()
{
int ch,i,j;
char ans='y';
do
{
clrscr();
cout<<"Menu";
cout<<"\n1.Whole Screen";
cout<<"\n2.Half Screen";
cout<<"\n3.Top Three Lines";
cout<<"\n4.Bottom Three Lines";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1: clrscr();
for(i=0;i<25;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
case 2: clrscr();
for(i=0;i<13;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;

case 3: clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
case 4: clrscr();
for(i=0;i<44;i++)
cout<<"\n";
for(i=0;i<3;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
default:cout<<"\nWrong choice!!!!";
}
cout<<"\nDo you want to continue?(y/n) : ";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}

Example 54 : Program : First 10 even numbers and their squares
#include <iostream.h>
void main()
{
int n ,count=1;
cout<<"First 10 even numbers and their squares:\n";
cout<<"\nNumber\tSquare\n";
for (n=2 ;count<=10; n=n+2)
{
cout<<n<<"\t"<<n*n<<endl;
count++;
}
}

Example 55 :
#include<iostream.h>
#include<conio.h>
void main()
{
int ch,i,j,in=1;
char ans='y';
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i&&i<=3;j++)
cout<<"\t";
//to leave space before stars for upper triangle
for(j=1;j<=i-1&&i>3;j++) //to leave space before stars for lower triangle
cout<<"\t";
for(j=1;j<=in;j++)
//to display stars
cout<<"*\t";
if(i+1<=3)
in=in+2;
else
in=in-2;
cout<<"\n\n";

//to decide no of stars in each row

}
getch();
}

Example 56 : Program: Sum of digits of an integer
#include <iostream.h>
main()
{
int num , temp , sum = 0 ;
cout<<"Enter an integer value :\n";
cin>>num;
do

{
temp = num % 10 ;
sum = sum + temp ;
num = num / 10 ;
}
while (num != 0) ;
cout<<"The sum of individual digits is "<<sum;
}

Example 57 : Program: The switch statement
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main()
{
int rn;
cout<<"Enter Room number : ";
cin>>rn;
switch (rn)
{
case 1 :
printf ("Hi! This is Room 1.\n") ;
case 2 :
printf ("Hi! This is Room 2.\n") ;
case 3 :
printf ("Hi! This is Room 3.\n") ;
case 4 :
printf ("Hi! This is Room 4.\n") ;
default :
printf ("Wrong Way !!\n") ;
}
getch () ;
}

Example 58 : Program: The switch statement

#include <iostream.h>
#include <conio.h>
void main()
{
int rn;
cout<<"Enter Room number : ";
cin>>rn;
switch (rn)
{
case 1 :
cout<<"Hi! This is Room 1.\n";
break ;
case 2 :
cout<<"Hi! This is Room 2.\n";
break ;
case 3 :
cout<<"Hi! This is Room 3.\n";
break ;
case 4 :
cout<<"Hi! This is Room 4.\n";
break ;
default :
cout<<"Wrong Way !!\n";
}
getch () ;
}

Example 59 : Program: The switch statement
#include <iostream.h>
void main()
{
int choice , x , y ;
cout<<"Enter any two numbers : ";
cin>>x>>y;
cout<<"\tMenu\n";

cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Division\n";
cout<<"4. Multiplication\n";
cout<<"5. Modulus\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Addition of "<<x<<" and "<<y<<" : "<<x+y;
break ;
case 2 :
cout<<"Subtraction of "<<x<<" and "<<y<<" : "<<x-y;
break ;
case 3 :
cout<<"Division of "<<x<<" and "<<y<<" : "<<float(x)/float(y);
break ;
case 4 :
cout<<"Multiplication of "<<x<<" and "<<y<<" : "<<x*y;
break ;
case 5 :
cout<<"Modulus of "<<x<<" and "<<y<<" : "<<x%y;
break ;
default :
cout<<"Wrong choice !!";
}
}
Example 60 : Program : The switch statement
#include <iostream.h>
#include <conio.h>
void main()
{
int p ;
cout<<"Enter any value between 1 to 5 :\n";
cin>>p;

switch (p)
{
case 4 :
cout<<"Welcome to the world of C++!!\n";
break ;
case 5 :
cout<<"This is section two.\n";
break ;
case 3 :
cout<<"This happens to be section three.\n";
break ;
case 1 :
cout<<"Oops! This is section four.\n";
break ;
case 2 :
cout<<"This is the last chance u have...\n";
break ;
default:
cout<<"I am sorry! Wrong Way !!\n";
}
getch () ;
}

Example 61 : Program : The switch statement
#include <iostream.h>
void main()
{
char c ;
cout<<"Enter any one character from the word 'worry': ";
cin>>c;
switch (c)
{
case 'W':

case 'w':
cout<<"I am in case w\n";
break ;
case 'O':
case 'o':
cout<<"I am in case o\n";
break ;
case 'R':
case 'r':
cout<<"I am in case r\n";
break ;
case 'Y':
case 'y':
cout<<"I am in case y\n";
break ;
default:cout<<"U entered the wrong letter !!\n";
}
}

Example 62 : Program: The switch statement
#include <iostream.h>
void main()
{
char c = 'x' ;
switch (c)
{
case '4':
case 'x' :
cout<<"Yes u got it !!\n";
}
}

Example 63 : Program: The switch statement
#include <iostream.h>
#include<conio.h>
void main()
{
int select , choice , x , y , i ;
clrscr();
cout<<"Enter any two integer values : ";
cin>>x>>y;
cout<<"\nVarious Operators : \n";
cout<<"=================\n";
cout<<"1. Arithmetic\n";
cout<<"2. Relational\n";
cout<<"3. Logical\n";
cout<<"Enter your choice :\n";
cin>>select;
switch (select)
{
case 1 :

cout<<"\tMenu-Arithmatic operations\n";
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Division\n";
cout<<"4. Multiplication\n";
cout<<"5. Modulus\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Addition of "<<x<<" and "<<y<<" : "<<x+y;
break ;
case 2 :
cout<<"Subtraction of "<<x<<" and "<<y<<" : "<<x-y;
break ;
case 3 :
cout<<"Division of "<<x<<" and "<<y<<" : "<<float(x)/float(y);
break ;
case 4 :
cout<<"Multiplication of "<<x<<" and "<<y<<" : "<<x*y;
break ;
case 5 :
cout<<"Modulus of "<<x<<" and "<<y<<" : "<<x%y;
break ;
default :
cout<<"Wrong choice !!";
}
break ;
case 2 :
cout<<"\tMenu-Relational opeations\n";
cout<<"1. Greater than\n";
cout<<"2. Less than\n";
cout<<"3. Equal to\n";
cout<<"4. Less than or equal to\n";
cout<<"5. Greater thn or equal to\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)

{
case 1 :
cout<<"Result of x greater than y : "<<(x>y);
break ;
case 2 :
cout<<"Result of x less than y : "<<(x<y);
break ;
case 3 :
cout<<"Result of x equal to y : "<<(x==y);
break ;
case 4 :
cout<<"Result of x less than or equal to y : "<<(x<=y);
break ;
case 5 :
cout<<"Result of x greater than or equal to y : "<<(x>=y);
break ;
}
break ;
case 3 :
cout<<"\tMenu-Logical operations\n";
cout<<"1. Logical AND\n";
cout<<"2. Logical OR\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Result of x AND y : "<<(x&&y);
break ;
case 2 :
cout<<"Result of x OR y : "<<(x||y);
break ;
}
break ;
}
getch () ;
}

Example 64 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,n;
clrscr();
cout<<"\nTable of 4 :\n";
for(i=1;i<=10;i++)
cout<<i<<"*4 = "<<i*4<<endl;
getch();
}

Example 65 :
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
cout<<"Christmas Tree:\n\n\n\n";
cout<<"\t\t\t\t\t /\\n";
cout<<"\t\t\t\t\t /\\n";
cout<<"\t\t\t\t\t/ \\n";
cout<<"\t\t\t\t
/\\n";
cout<<"\t\t\t\t
/________\\n";
cout<<"\t\t\t\t
| | \n";
cout<<"\t\t\t\t
| | \n";
cout<<"\t\t\t\t
| | \n";
getch();
}

Example 66 : Program: Evaluation of Triangular Number
#include <iostream.h>

void main()
{
int n , tri_num ;
cout<<"Enter the row number :\n";
cin>>n;
tri_num = n * (n+1) / 2 ;
cout<<"The value of the triangular number is "<<tri_num;
}

Example 67 : Program : The while loop
#include <iostream.h>
void main()
{
int i = 1 ;
while (i<=5)
cout<<i++<<endl ;
}

Example 68 : Program : The while loop
#include <iostream.h>
void main()
{
int i = 1 ;
while (i<=5)
{
cout<<i<<endl ;
i++ ;
}
}

Example 69 : Program : The while loop
#include <iostream.h>
void main()
{
int i = 1 , sum = 0 ;

while (i<11)
{
sum = sum + i ;
i++ ;
}
cout<<"The sum of first ten numbers is : "<<sum;
}

Example 70 : Program : The while loop
#include <iostream.h>
void main( )
{
float principle , rate_of_interest , no_of_years ;
int num = 0 ;
float simple_interest ;
cout<<"Simple Interest Calculation : ";
while (num<2)
{
cout<<"\nSet - "<<num+1;
cout<<"Enter the principle value : ";
cin>>principle;
cout<<"Enter the rate of interest : " ;
cin>>rate_of_interest;
cout<<"Enter the number of years : ";
cin>>no_of_years;
simple_interest = (principle * rate_of_interest * no_of_years) ;
simple_interest = simple_interest / 100 ;
cout<<"The simple interest is : "<<simple_interest;
num++ ;
}
}

Example 71 : Program : The while loop

#include <iostream.h>
#include<conio.h>
void main()
{
int i = 1 , j , n ;
cout<<"Enter the number of rows (from 1 to 10) : " ;
cin>>n;
while (i<=n)
{
j=1;
while (j<=i)
{
cout<<"*"<<"\t" ;
j++ ;
}
cout<<"\n" ;
i++ ;
}
getch () ;
}

Example 72 : Program: The while loop
#include <iostream.h>
void main()
{
int num_1 , num_2 ;
cout<<"Enter any two integer values :\n";
cin>>num_1>>num_2 ;
while ((num_1!=0) && (num_2!=0))
{
if (num_1 > num_2)
num_1 = num_1 - num_2 ;
else
num_2 = num_2 - num_1 ;
}
cout<<"The GCD is "<<num_1;
}