Basic Programs of C++
Basic Programs of C++
3 min read
By Anit Kumar
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello world";
return 0;
}
Output:
Hello world
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter integer\n";
cin>>a;
cout<<"Integer is "<<a;
return 0;
}
Output:
Enter integer
10
Integer is 10
Write a program in C++ to addition of two number.
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter the 1st number\n";
cin>>a;
cout<<"Enter the 2nd number\n";
cin>>b;
sum=a+b;
cout<<"Sum of two number "<<sum;
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
int a,b,sub;
cout<<"Enter the 1st number\n";
cin>>a;
cout<<"Enter the 2nd number\n";
cin>>b;
sub=a-b;
cout<<"Subtraction of two number "<<sub;
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
int a,b,mul;
cout<<"Enter the 1st number\n";
cin>>a;
cout<<"Enter the 2nd number\n";
cin>>b;
mul=a*b;
cout<<"Multiplication of two number "<<mul;
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
int a,b,div;
cout<<"Enter the 1st number\n";
cin>>a;
cout<<"Enter the 2nd number\n";
cin>>b;
div=a/b;
cout<<"Division of two number "<<div;
return 0;
}
Output:
Write a program in C++ to swap two number with the help of third
variable.
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter the first number\t";
cin>>a;
cout<<"\nEnter the second number\t";
cin>>b;
c=a;
a=b;
b=c;
cout<<"First number is\t"<<a;
cout<<"\nSecond number is\t<<b;
return 0;
}
Output:
Write a program in C++ to swap two number without the help of third
variable.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter the first number\t";
cin>>a;
cout<<"\nEnter the second number\t";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"First number is\t"<<a;
cout<<"\nSecond number is\t<<b;
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter the first number\t";
cin>>a;
cout<<"Enter the second number\t";
cin>>b;
if(a>b)
cout<<"Greatest number is\t"<<a;
else
cout<<"Greatest number is\t"<<b;
return 0;
}
Output: