Basic programs of C++

Write a program in C++ to print a massage on output screen.

#include<iostream>
using namespace std;
int main()
{
     cout<<"Hello world";
     return 0;
}

Output:

Hello world

Write a program in C++ to print an integer value.

#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:

Enter the 1st number
10
Enter the 2nd number
20
Sum of two number 30

Write a program in C++ to subtraction of two number.

#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:

Enter the 1st number
10
Enter the 2nd number
20
Subtraction of two number -10

Write a program in C++ to multiplication of two number.

#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:

Enter the 1st number
10
Enter the 2nd number
20
Multiplication of two number 200

Write a program in C++ to Division of two number.

#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:

Enter the 1st number
20
Enter the 2nd number
10
Sum of two number 2

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:

Enter the first number    12
Enter the second number    13
First number is    13
Second number is    12

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:

Enter the first number    12
Enter the second number    13
First number is    13
Second number is    12

Write a program in C++ to find the greatest number in two numbers with the help of if/else.

#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:

Enter the first number    12
Enter the second number    13
Greatest number is    13


Related Article