basic programs of c++
DESCRIPTION
Basic Programs of C++ includes loops and conditional statement and basicsTRANSCRIPT
![Page 1: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/1.jpg)
// 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
![Page 2: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/2.jpg)
Output :
2
![Page 3: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/3.jpg)
// 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
![Page 4: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/4.jpg)
Output :
4
![Page 5: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/5.jpg)
// 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
![Page 6: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/6.jpg)
Output :
6
![Page 7: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/7.jpg)
// 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
![Page 8: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/8.jpg)
Output :
8
![Page 9: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/9.jpg)
// 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
![Page 10: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/10.jpg)
Output :
10
![Page 11: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/11.jpg)
// 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
![Page 12: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/12.jpg)
Output :
12
![Page 13: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/13.jpg)
// 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
![Page 14: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/14.jpg)
Output :
14
![Page 15: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/15.jpg)
// 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
![Page 16: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/16.jpg)
Output :
16
![Page 17: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/17.jpg)
// 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
![Page 18: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/18.jpg)
Output :
18
![Page 19: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/19.jpg)
// 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
![Page 20: Basic Programs of C++](https://reader036.vdocuments.net/reader036/viewer/2022083001/557ed0c7d8b42ae27f8b4def/html5/thumbnails/20.jpg)
Output :
20