1 
// 1. Program to implement arithmetic operators 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a,b,sum,sub,mul,div; 
cout<<"nenter the numbers : "; 
cin>>a>>b; 
sum=a+b; 
sub=a-b; 
mul=a*b; 
div=a/b; 
cout<<"nSum:"<<sum; 
cout<<"nSubtraction:"<<sub; 
cout<<"nMultipication:"<<mul; 
cout<<"nDivision:"<<div; 
getch(); 
}
2 
Output :
3 
// 2. Program to calculate simple interest 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int principle,rate,time,si; 
cout<<"nEnter the principle : "; 
cin>>principle; 
cout<<"nEnter the rate : "; 
cin>>rate; 
cout<<"nEnter the time :"; 
cout<<interest; 
si=(principle*rate*time)/100; 
cout<<"nSimple Interest :"<<si; 
getch(); 
}
4 
Output :
// 3. Program to implement increment and decrement operators 
5 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int x=5,y=5; 
cout<<x++; 
cout<<"n"; 
cout<<++x; 
cout<<"n"; 
cout<<y++; 
cout<<"n"; 
cout<<++y; 
getch(); 
}
6 
Output :
7 
// 4. Program to implement if-else statements 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int year; 
cout<<"nEnter the year:"; 
cin>>year; 
if(year%4==0) 
cout<<"nIt is a leap year"; 
else 
cout<<"Not a leap year"; 
getch(); 
}
8 
Output :
// 5. Program to implement if-else if ladder statement 
9 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
float sal; 
float tax; 
cout<<"nEnter the salary : "; 
cin>>sal; 
if(sal>=9000) 
tax=sal*0.4; 
else if((sal>=7500)&&(sal<8999)) 
tax=sal*0.3; 
else 
tax=sal*0.2; 
cout<<"Tax:"<<tax; 
getch(); 
}
10 
Output :
// 6. Program to implement switch case statement 
11 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int ch; 
cout<<"nEnter your choice : "; 
cin>>ch; 
switch(ch) 
{ 
case 1 : cout<<"nCase 1 : Good Morning"; 
break; 
case 2 : cout<<"nCase 2 : Good Afternoon"; 
break; 
case 3 : cout<<"nCase 3 : Good Evening"; 
break; 
case 4 : cout<<"nCase 4 : Good Night"; 
break; 
deafult: cout<<"nWrong choice"; 
} 
getch(); 
}
12 
Output :
// 7. Program to implement while loop and it computes a power of b 
13 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
float a; 
int b; 
float pow; 
cout<<"nEnter the values : "; 
cin>>a>>b; 
pow=1.0; 
while(b>=1) 
{ 
pow=pow*a; 
b--; 
} 
cout<<"Power : "<<pow; 
getch(); 
}
14 
Output :
// 8. Program to implement do while and it computes the factorial of a number 
15 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int num,fact=1; 
cout<<"nEnter the numnber : "; 
cin>>num; 
do 
{ 
fact=fact*num; 
num--; 
} 
while(num!=0); 
cout<<"nFactorial :"<<fact; 
getch(); 
}
16 
Output :
17 
// 9. Program to display an array 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a[5],i; 
cout<<"nEnter the elements in an array :n"; 
for(i=0;i<5;i++) 
{ 
cin>>a[i]; 
} 
cout<<"nDisplayed array : n"; 
for(i=0;i<5;i++) 
{ 
cout<<"n"<<a[i]; 
} 
getch(); 
}
18 
Output :
// 10. Program to display multiples of a number using for loop 
19 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int i,num; 
cout<<"nEnter the number :"; 
cin>>num; 
cout<<"The multiples are :"; 
for(i=1;i<=10;i++) 
{ 
cout<<"n"<<num*i; 
} 
getch(); 
}
20 
Output :

Basic Programs of C++

  • 1.
    1 // 1.Program to implement arithmetic operators #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,sum,sub,mul,div; cout<<"nenter the numbers : "; cin>>a>>b; sum=a+b; sub=a-b; mul=a*b; div=a/b; cout<<"nSum:"<<sum; cout<<"nSubtraction:"<<sub; cout<<"nMultipication:"<<mul; cout<<"nDivision:"<<div; getch(); }
  • 2.
  • 3.
    3 // 2.Program to calculate simple interest #include<iostream.h> #include<conio.h> void main() { clrscr(); int principle,rate,time,si; cout<<"nEnter the principle : "; cin>>principle; cout<<"nEnter the rate : "; cin>>rate; cout<<"nEnter the time :"; cout<<interest; si=(principle*rate*time)/100; cout<<"nSimple Interest :"<<si; getch(); }
  • 4.
  • 5.
    // 3. Programto implement increment and decrement operators 5 #include<iostream.h> #include<conio.h> void main() { clrscr(); int x=5,y=5; cout<<x++; cout<<"n"; cout<<++x; cout<<"n"; cout<<y++; cout<<"n"; cout<<++y; getch(); }
  • 6.
  • 7.
    7 // 4.Program to implement if-else statements #include<iostream.h> #include<conio.h> void main() { clrscr(); int year; cout<<"nEnter the year:"; cin>>year; if(year%4==0) cout<<"nIt is a leap year"; else cout<<"Not a leap year"; getch(); }
  • 8.
  • 9.
    // 5. Programto implement if-else if ladder statement 9 #include<iostream.h> #include<conio.h> void main() { clrscr(); float sal; float tax; cout<<"nEnter the salary : "; cin>>sal; if(sal>=9000) tax=sal*0.4; else if((sal>=7500)&&(sal<8999)) tax=sal*0.3; else tax=sal*0.2; cout<<"Tax:"<<tax; getch(); }
  • 10.
  • 11.
    // 6. Programto implement switch case statement 11 #include<iostream.h> #include<conio.h> void main() { clrscr(); int ch; cout<<"nEnter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<"nCase 1 : Good Morning"; break; case 2 : cout<<"nCase 2 : Good Afternoon"; break; case 3 : cout<<"nCase 3 : Good Evening"; break; case 4 : cout<<"nCase 4 : Good Night"; break; deafult: cout<<"nWrong choice"; } getch(); }
  • 12.
  • 13.
    // 7. Programto implement while loop and it computes a power of b 13 #include<iostream.h> #include<conio.h> void main() { clrscr(); float a; int b; float pow; cout<<"nEnter the values : "; cin>>a>>b; pow=1.0; while(b>=1) { pow=pow*a; b--; } cout<<"Power : "<<pow; getch(); }
  • 14.
  • 15.
    // 8. Programto implement do while and it computes the factorial of a number 15 #include<iostream.h> #include<conio.h> void main() { clrscr(); int num,fact=1; cout<<"nEnter the numnber : "; cin>>num; do { fact=fact*num; num--; } while(num!=0); cout<<"nFactorial :"<<fact; getch(); }
  • 16.
  • 17.
    17 // 9.Program to display an array #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5],i; cout<<"nEnter the elements in an array :n"; for(i=0;i<5;i++) { cin>>a[i]; } cout<<"nDisplayed array : n"; for(i=0;i<5;i++) { cout<<"n"<<a[i]; } getch(); }
  • 18.
  • 19.
    // 10. Programto display multiples of a number using for loop 19 #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,num; cout<<"nEnter the number :"; cin>>num; cout<<"The multiples are :"; for(i=1;i<=10;i++) { cout<<"n"<<num*i; } getch(); }
  • 20.