Table of Contents
hide
NB: These programs are compiled by dev C++ 5.11 compiler.
Example : A simple C++ program to display the (static) message.
#include<iostream>
using namespace std;
int main()
{
cout <<"Welcome U All";
}
//----------------------------- OR ----------------------------
#include<iostream>
using namespace std;
int main(void)
{
cout <<"Welcome U All";
}
Example : A simple C++ program to display the message in two separate lines.
#include<iostream>
using namespace std;
int main()
{
cout <<"Welcome U All"<<"\n"<<"In Codershelpline.com";
}
//----------------------------- OR ----------------------------
#include<iostream>
using namespace std;
int main()
{
cout <<"Welcome U All"<<endl<<"In Codershelpline.com";
}
Output:
Welcome U All
In Codershelpline.com
Example : A simple C++ program to display the dynamic or user given message.
#include<iostream>
using namespace std;
int main()
{
char uname [30];
//clrscr();
cout<<"Enter User Name: = ";
cin>>uname;
cout<<"User Name is : " <<uname;
return 0;
}
//----------------------------- OR ----------------------------
#include<iostream>
using namespace std;
int main()
{
string Str; // Not use String Str;
Str = "Raman Kumar";
cout << Str << endl;
Str = "Sarika Kumari";
cout << Str << endl;
return 0;
}
Output :
Raman Kumar
Sarika Kumari
Example : A simple C++ program to display the single accepted numerical value from the user.
#include<iostream>
using namespace std;
int main()
{
int num;
cout <<"Enter single integer value: ";
cin >> num;
cout <<"You entered " << num <<" "<<"value.";
return 0;
}
Output :
Enter single integer value: 25
You entered 25 value.
Example : A simple C++ program to display the multiple accepted numerical value.
#include<iostream>
using namespace std;
int main ()
{
int num1, num2;
//clrscr();
cout<<"Enter two integer numbers :";
cin>>num1>>num2;
cout<<"Entered numbers are : " ;
cout <<num1<<"\t"<<num2;
return 0;
}
Example : A simple C++ program to display the initialization and calculation of those numerical values.
#include<iostream>
using namespace std;
int main()
{
int a=5; // initialize variable a with value = 5
int b(2); // initialize variable b with value = 2
int result; // here initial value undetermined
result = a + b;
cout << result;
return 0;
}
Output :
7
Example : A simple C++ program to display the sum/addition of two values.
#include<iostream>
using namespace std;
int main ()
{
int a,b,sum;
cout<<"Enter first number\n";
cin>>a;
cout<<"Enter second number\n";
cin>>b;
sum=a+b;
cout<<"Sum of two number = "<<sum;
return 0;
}
Output :
Enter first number
20
Enter second number
30
Sum of two number = 50
Example : 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 :\n";
cin>>a;
cout<<"Enter the second number :\n";
cin>>b;
c=a;
a=b;
b=c;
cout<<"The first value after swapping is :" <<a;
cout<<"\nThe second value after swapping is : "<<b;
return 0;
}
Output :
Enter the first number :
62
Enter the second number :
35
The first value after swapping is :35
The second value after swapping is : 62
Example : 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,c;
cout<<"Enter the first number :\n";
cin>>a;
cout<<"Enter the second number :\n";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"The first value after swapping is :" <<a;
cout<<"\nThe second value after swapping is : "<<b;
return 0;
}
Example : A simple C++ program to display the calculation result using macro concept.
#include<iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r = 5.3;
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
Output :
33.3009
Example : A C++ program to check whether the given no. is odd or even.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter an Integer Value : ";
cin>>num;
if (num % 2 == 0)
cout << num << " is even no.";
else
cout << num << " is odd no.";
return 0;
}
Example : A C++ program to create simple calculator using switch case statement.
#include<iostream>
using namespace std;
int main()
{
char choice;
int num1, num2;
cout << "Press plus/minus/multiply/division symbol from keyword to use calculator : ";
cin >> choice;
cout << "Enter two values : ";
cin >> num1 >> num2;
switch(choice)
{
case '+':
cout << "Addition result is : ";
cout << num1+num2;
break;
case '-':
cout << "Subtraction result is : ";
cout << num1-num2;
break;
case '*':
cout << "Multiplication result is : ";
cout << num1*num2;
break;
case '/':
cout << "Division result is : ";
cout << num1/num2;
break;
default:
cout << "Error! given operator is not correct, re-enter again ";
break;
}
return 0;
}
Example : A C++ program to check whether the given value is palindrome or not.
#include<iostream>
using namespace std;
int main()
{
int num2, num1, digit, rev = 0;
cout << "Enter a number: ";
cin >> num1;
num2 = num1;
do
{
digit = num1 % 10;
rev = (rev * 10) + digit;
num1 = num1 / 10;
} while (num1 != 0);
cout << " The reverse of the given number is: " << rev << endl;
if (num2 == rev)
cout << " The given number is a palindrome.";
else
cout << " The given number is not a palindrome.";
return 0;
}
Example : A C++ program to check whether the given value is prime or not.
#include<iostream>
using namespace std;
int main()
{
int x=0,num, i;
cout << "Enter a positive integer value : ";
cin >> num;
for(i=2; i <= num/2; ++i)
{
if(num % i == 0)
{
x = 1;
break;
}
}
if (x==0)
cout << "Given no. is prime number";
else
cout << "Given no is not a prime number";
return 0;
}
0 Comments