Académique Documents
Professionnel Documents
Culture Documents
Following is the simplest C++ program which will print the string "Hello
Compiler, I am C++" on the output screen as shown here:
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // clear the screen
cout<<"Hello Compiler, I am C++";
getch(); // holds output screen until user press a key
}
OR
/* C++ Programming Examples */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char str[20];
cout<<"Enter your name : ";
cin>>str;
cout<<"Hello, "<<str<<" Sir, You are genius";
getch();
}
Following C++ program prints the value of the variable num which holds an
integer value:
/* C++ Program - Print Integers */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num=10;
cout<<"The value of num is "<<num;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=10, num;
cout<<"Guess a Number : ";
cin>>num;
if(num>10 && num<100)
{
cout<<"What a mind!!";
}
else
{
cout<<"Opps..!!";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=10, b;
cout<<"Enter 10:";
cin>>b;
if(b==a)
{
cout<<"What a good Employee!!";
}
else
{
cout<<"Sorry!, you are not selected..!!";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, sum;
cout<<"Enter two number :";
cin>>a>>b;
sum=a+b;
cout<<"Sum of the two number is "<<sum;
getch();
}
Program 6
C++ Programming Code to Check Even or Odd
Following C++ program ask to the user to enter a number, to check and display
whether the entered number is an even number or an odd number:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number :";
cin>>num;
if(num%2==0)
{
cout<<"This is an even number";
}
else
{
cout<<"This is an odd number";
}
getch();
}
Program 7
C++ Programming Code to Check Prime or Not
/* C++ Program - Check Prime or Not */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,count=0;
cout<<"Enter a number:";
cin>>num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0)
{
cout<<"This is a prime number";
}
else
{
cout<<"This is not a prime number";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter a character: ";
cin>>ch;
if((ch>='a'&& ch<='z') || (ch>='A' && ch<='Z'))
{
cout<<ch<<" is an alphabet";
}
else
{
cout<<ch<<" is not an alphabet";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter an alphabet : ";
cin>>ch;
if(ch=='a' || ch=='A' || ch=='e' || ch=='E' ||
ch=='i' || ch=='I' || ch=='o' || ch=='O' ||
ch=='u' || ch=='U')
{
cout<<"This is a vowel";
}
else
{
cout<<"This is not a vowel";
}
getch();
}
Program 10 - Check Leap Year or Not in C++
/* C++ Program - Check Leap Year or Not */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int yr;
cout<<"Enter year :";
cin>>yr;
if((yr%4==0) && (yr%100!=0))
{
cout<<"This is a Leap Year";
}
else if(yr%100==0)
{
cout<<"This is not a Leap Year";
}
else if(yr%400==0)
{
cout<<"This is a Leap Year";
}
else
{
cout<<"This is not a Leap Year";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, orig, rev=0, rem;
cout<<"Enter a number : ";
cin>>num;
orig=num;
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(orig==rev)
{
cout<<"Reverse is equal to original";
}
else
{
cout<<"Reverse is not equal to original";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, res;
cout<<"Enter two number :";
cin>>a>>b;
res=a+b;
cout<<"\nAddition = "<<res;
res=a-b;
cout<<"\nSubtraction = "<<res;
res=a*b;
cout<<"\nMultiplication = "<<res;
res=a/b;
cout<<"\nDivision = "<<res;
getch();
}
Program 13 - Make Simple Calculator Program in C++
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
float a, b, res;
char choice, ch;
do
{
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiplication\n";
cout<<"4.Division\n";
cout<<"5.Exit\n\n";
cout<<"Enter Your Choice : ";
cin>>choice;
switch(choice)
{
case '1' : cout<<"Enter two number : ";
cin>>a>>b;
res=a+b;
cout<<"Result = "<<res;
break;
case '2' : cout<<"Enter two number : ";
cin>>a>>b;
res=a-b;
cout<<"Result = "<<res;
break;
case '3' : cout<<"Enter two number : ";
cin>>a>>b;
res=a*b;
cout<<"Result = "<<res;
break;
case '4' : cout<<"Enter two number : ";
cin>>a>>b;
res=a/b;
cout<<"Result = "<<res;
break;
case '5' : exit(0);
break;
default : cout<<"Wrong Choice..!!";
break;
}
cout<<"\n------------------------------------\n";
}while(choice!=5 && choice!=getchar());
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, rem=0, sum=0;
cout<<"Enter a Number :";
cin>>num;
int temp=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
cout<<"Sum of the digits of "<<temp<<" is "<<sum;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mark[5], i;
float sum=0;
cout<<"Enter marks obtained in Physics, Chemistry, Maths, CS,
English :";
for(i=0; i<5; i++)
{
cin>>mark[i];
sum=sum+mark[i];
}
float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"Average Marks = "<<avg;
cout<<"\nPercentage = "<<perc<<"%";
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], sum=0;
cout<<"How many number you want to enter ?\n";
cin>>n;
cout<<"Enter "<<n<<" Numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
sum=sum+arr[i];
}
int armean=sum/n;
cout<<"Arithmetic Mean = "<<armean;
getch();
}
To calculate grade of a student on the basis of his total marks in C++ programming, you
have to ask to the user to enter marks obtained in some subjects (5 subjects here).
To calculate grade of student, first calculate the mark's sum of all the subjects and
then calculate average marks. Now start checking for the grades to display the result as
shown here in the following program.
/* C++ Program - Calculate Grade of Student */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mark[5], i;
float sum=0,avg;
cout<<"Enter marks obtained in 5 subjects :";
for(i=0; i<5; i++)
{
cin>>mark[i];
sum=sum+mark[i];
}
avg=sum/5;
cout<<"Your Grade is ";
if(avg>80)
{
cout<<"A";
}
else if(avg>60 && avg<=80)
{
cout<<"B";
}
else if(avg>40 && avg<=60)
{
cout<<"C";
}
else
{
cout<<"D";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i, tab;
cout<<"Enter a number : ";
cin>>num;
cout<<"Table of "<<num<<" is \n\n";
for(i=1; i<=10; i++)
{
tab=num*i;
cout<<num<<" * "<<i<<" = "<<tab<<"\n";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int start, end, i, j, count=0;
// to print all the prime number between any range
// enter the two number (starting and ending)
cout<<"Enter starting number : ";
cin>>start;
cout<<"Enter ending number : ";
cin>>end;
cout<<"Prime Number Between "<<start<<" and "<<end<<" is
:\n";
for(i=start; i<=end; i++)
{
count=0;
for(j=2; j<i; j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0)
{
cout<<i<<" ";
}
}
getch();
}
To add n numbers in C++ programming, you have to ask to the user to enter the
value of n (i.e., how many numbers he/she want to enter to all the numbers), then
ask to enter n numbers to perform the addition of all the n numbers to display the
result on the screen as shown here in the following program.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, n, sum=0, num;
cout<<"How many number you want to enter and add them ?";
cin>>n;
cout<<"Enter "<<n<<" number :";
for(i=0;i<n;i++)
{
cin>>num;
sum=sum+num;
}
cout<<"Sum of all the "<<n<<" number is "<<sum;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, temp;
cout<<"Enter value of A and B : \n";
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
temp=a;
a=b;
b=temp;
cout<<"Number interchanged successfully..!!\n";
cout<<"A = "<<a<<"\n"<<"B = "<<b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, rev=0, rem;
cout<<"Enter a number : ";
cin>>num;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
cout<<"Reverse = "<<rev;
getch();
}
To reverse a number, first make a variable say rev and place 0 to rev initially,
and make one more variable say rem. Now place the modulus of the number
to rem and place rev*10+rem to rev, now divide the number with 10 and continue
until number will become 0.
Program 23 - Swap Two Numbers
/* C++ Program - Swap Two Numbers */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, swap;
cout<<"Enter two number : ";
cout<<"\nFirst Number : ";
cin>>num1;
cout<<"Second Number : ";
cin>>num2;
swap=num1;
num1=num2;
num2=swap;
cout<<"The value of first and second number after swapping is
\n";
cout<<"First Number = "<<num1<<"\n"<<"Second Number =
"<<num2;
getch();
}
#include <stdio.h>
int main()
{
int x = 10, y = 5;
return 0;
}
Output:
After Swapping: x = 5, y = 10
return 0;
}
Output:
After Swapping: x = 5, y = 10
#include <stdio.h>
int main()
{
int x = 10, y = 5;
Output:
After Swapping: x = 5, y = 10
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int countp=0, countn=0, countz=0, arr[10], i;
cout<<"Enter 10 numbers : ";
for(i=0; i<10; i++)
{
cin>>arr[i];
}
for(i=0; i<10; i++)
{
if(arr[i]<0)
{
countn++;
}
else if(arr[i]==0)
{
countz++;
}
else
{
countp++;
}
}
cout<<"Positive Numbers = "<<countp<<"\n";
cout<<"Negative Numbers = "<<countn<<"\n";
cout<<"Zero = "<<countz<<"\n";
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, big;
cout<<"Enter two number : ";
cin>>a>>b;
if(a>b)
{
big=a;
}
else
{
big=b;
}
cout<<"Biggest of the two number is "<<big;
getch();
}
Program 26 - Find Largest of Three Numbers
/* C++ Program - Find Largest of Three Numbers */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c, big;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
//let a is the biggest
big=a;
if(big<b)
{
if(b>c)
{
big=b;
}
else
{
big=c;
}
}
else if(big<c)
{
if(c>b)
{
big=c;
}
else
{
big=b;
}
}
else
{
big=a;
}
cout<<"Biggest number is "<<big;
getch();
}
Program 27- Find Factorial of Number in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i, fact=1;
cout<<"Enter a number : ";
cin>>num;
for(i=num; i>0; i--)
{
fact=fact*i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, x, y, t, hcf, lcm;
cout<<"Enter two number : ";
cin>>x>>y;
a=x;
b=y;
while(b!=0)
{
t=b;
b=a%b;
a=t;
}
hcf=a;
lcm=(x*y)/hcf;
cout<<"HCF = "<<hcf<<"\n"
cout<<"LCM = "<<lcm<<"\n";
getch();
}
Program 29- Find Calculate Area and Perimeter of Square and Rectangle
/* C++ Program - Calculate Area and Perimeter of Rectangle */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int len, bre, peri, area;
cout<<"Enter length and breadth of the rectangle :";
cin>>len>>bre;
area=len*bre;
peri=(2*len)+(2*bre);
cout<<"Area = "<<area<<"\tPerimeter="<<peri;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r, area, circum;
cout<<"Enter the radius of the circle :";
cin>>r;
area=3.14*r*r;
circum=2*3.14*r;
cout<<"Area of the circle = "<<area<<"\nCircumference of the circle
= "<<circum<<"\n";
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float cen, fah;
cout<<"Enter temperature in Celsius : ";
cin>>cen;
fah=(1.8 * cen) + 32;
cout<<"\nTemperature in Fahrenheit = "<<fah;
getch();
}
To print Fibonacci series in C++ programming, you have to ask from the user to
enter total number of terms that he/she want to print fibonacci series upto the
required number.
Now to print fibonacci series, first print the starting two number of the Fibonacci
series and make a while loop to start printing the next number of the Fibonacci
series. Use the three variable say a, b and c. Place b in a and c in b then
place a+b in c to print the value of c to make and print Fibonacci series as shown
here in the following program.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0, b=1, c=0, limit;
cout<<"Upto How many term ? ";
scanf("%d",&limit);
cout<<"Fabonacci Series : "<<a<<" "<<b<<" "; // first two
term
c=a+b;
limit=limit-2; // decrease the limit by 2. since two
numbers already printed
while(limit)
{
cout<<c<<" ";
a=b;
b=c;
c=a+b;
limit--;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int i;
for(i=1; i<255; i++)
{
ch=i;
cout<<i<<"-> "<<ch<<"\t";
}
getch();
}
Program 35 -Check Palindrome or Not
Now to check whether the entered number is a palindrome number (if reverse of
the number is equal to its original) or not a palindrome number (if reverse of the
number is not equal to its original), you have to first reverse the number and
check whether reverse is equal to original or not. If it is equal then the number is
palindrome, otherwise not palindrome number as shown here in the following
program.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, rem, orig, rev=0;
cout<<"Enter a number : ";
cin>>num;
orig=num;
while(num!=0)
{
rem=num%10;
rev=rev*10 + rem;
num=num/10;
}
if(rev==orig) // check if original number is equal to its
reverse
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
getch();
}
Program 36 - Check Armstrong or Not in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, nu, num=0, rem;
cout<<"Enter any positive number : ";
cin>>n;
nu=n;
while(nu!=0)
{
rem=nu%10;
num=num + rem*rem*rem;
nu=nu/10;
}
if(num==n)
{
cout<<"Armstrong Number";
}
else
{
cout<<"Not Armstrong Number";
}
getch();
}
Generate Armstrong Numbers
Since if you start from 1 then the first Armstrong number will be 153. So enter the
interval which contain 153 like enter starting number as 1, 2, 3, 4...... etc. and
enter the ending number like 154, 155, 156.......etc. It is just a clue that the first
Armstrong number is 153 so to check/print, you can follow it. Generate
Armstrong number from the following C++ program, so following C++ program
illustrates it:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, i, n, rem, temp, count=0;
//enter the interval (enter the two number)
cout<<"Enter Starting Number : ";
cin>>num1;
cout<<"Enter Ending Number : ";
cin>>num2;
for(i=num1+1; i<num2; i++)
{
temp=i;
n=0;
while(temp!=0)
{
rem=temp%10;
n = n + rem*rem*rem;
temp=temp/10;
}
if(i==n)
{
if(count==0)
{
cout<<"Armstrong numbers between the
given interval are : \n";
}
cout<<i<<" ";
count++;
}
}
if(count==0)
{
cout<<"Armstrong number not found between the given
interval";
}
getch();
}
#include<iostream.h>
#include<conio.h>
long int fact(int); //function declaration
void main()
{
clrscr();
int n, r;
long int ncr, npr;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Enter the value of r : ";
cin>>r;
npr=fact(n)/fact(n-r); // calling the function or function
calling
ncr=npr/fact(r); //function calling
cout<<"NPR value = "<<npr<<"\n";
cout<<"NCR value = "<<ncr<<"\n";
getch();
}
long int fact(int x) //defining the function or function
definition
{
int i, f=1;
for(i=2; i<=x; i++)
{
f=f*i;
}
return f;
}