basic programs of c++
Post on 15-Jun-2015
169 Views
Preview:
DESCRIPTION
TRANSCRIPT
// 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();
}
1
Output :
2
// 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();
}
3
Output :
4
// 3. Program to implement increment and decrement operators
#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();
}
5
Output :
6
// 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();
}
7
Output :
8
// 5. Program to implement if-else if ladder statement
#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();
}
9
Output :
10
// 6. Program to implement switch case statement
#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();
}
11
Output :
12
// 7. Program to implement while loop and it computes a power of b
#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();
}
13
Output :
14
// 8. Program to implement do while and it computes the factorial of a number
#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();
}
15
Output :
16
// 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();
}
17
Output :
18
// 10. Program to display multiples of a number using for loop
#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();
}
19
Output :
20
top related