Vous êtes sur la page 1sur 27

List of Basic C++ Programs

Following is the simplest C++ program which will print the string "Hello
Compiler, I am C++" on the output screen as shown here:
Solution:

/* C++ Programming Examples */

#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();
}

Getting Input from user

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();
}

Program 2 Guess a Number


/* C++ Programming Examples */

#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();
}

Program 4 Good Employee


/* C++ Programming Examples */

#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();
}

Program 5 Addition of numbers


/* C++ Program - Add Two Numbers */

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

/* C++ Program - Check Even or Odd */

#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 */

/* 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();
}

Program 8 Alphabet or not


/* C++ Program - Check Alphabet or Not */

#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();
}

Program 9 Vowel Or Not


/* C++ Program - Check Vowel or Not */

#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();
}

Program 11 - Check Original Equal Reverse or Not


Following C++ program ask the user to enter a number, to find its reverse and then check
whether reverse is equal to its original or not, then display the result on the screen:

/* C++ Program - Check Original Equal Reverse or Not */

#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();
}

Program 12- Addition, Subtraction, Multiplication, and Division in C++


/* C++ Program - Addition, Subtraction, Multiplication, Division */

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

/* C++ Program - Make Simple Calculator */

#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();
}

Program 14 - Add Digits of Number


/* C++ Program - Add Digits of Number */

#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();
}

Program 15 - Calculate Average and Percentage Marks


/* C++ Program - Calculate Average and Percentage Marks */

#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();
}

Program 16 - Calculate Arithmetic Mean


/* C++ Program - Calculate Arithmetic Mean */

#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();
}

Program 17 - Calculate Grade of Student in C++

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();
}

Program 18 - Print Table of Number

/* C++ Program - Print Table of Number */

#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();
}

Program 19 - Print Prime Number


/* C++ Program - Print Prime Numbers */

#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();
}

Program 20 - Add n Numbers in C++

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.

/* C++ Program - Add n Numbers */

#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();
}

Program 21 - Interchange Numbers


/* C++ Program - Interchange Numbers */

#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();
}

Program 22 - Reverse Number

/* C++ Program - Reverse Number */

#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();
}

swap two numbers without using a


temporary variable

Method 1 (Using Arithmetic Operators)


The idea is to get sum in one of the two given numbers. The numbers can then be
swapped using the sum and subtraction from sum.

#include <stdio.h>
int main()
{
int x = 10, y = 5;

// Code to swap 'x' and 'y'


x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5

printf("After Swapping: x = %d, y = %d", x, y);

return 0;
}
Output:

After Swapping: x = 5, y = 10

Multiplication and division can also be used for swapping.


#include <stdio.h>
int main()
{
int x = 10, y = 5;

// Code to swap 'x' and 'y'


x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5

printf("After Swapping: x = %d, y = %d", x, y);

return 0;
}

Output:

After Swapping: x = 5, y = 10

Method 2 (Using Bitwise XOR)


The bitwise XOR operator can be used to swap two variables. The XOR of two numbers
x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For
example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7
(0111) and 5 (0101) is (0010).

#include <stdio.h>
int main()
{
int x = 10, y = 5;

// Code to swap 'x' (1010) and 'y' (0101)


x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)

printf("After Swapping: x = %d, y = %d", x, y);


return 0;
}

Output:

After Swapping: x = 5, y = 10

Problems with above methods


1) The multiplication and division based approach doesn work if one of the numbers is
0 as the product becomes 0 irrespective of the other number.
2) Both Arithmetic solutions may cause arithmetic overflow. If x and y are too large,
addition and multiplication may go out of integer range.
3) When we use pointers to variable and make a function swap, all of the above
methods fail when both pointers point to the same variable. Lets take a look what will
happen in this case if both are pointing to the same variable.

Program 24 - Count Positive, Negative and Zero in C++


To count the number of positive number, negative number, and zero from the
given set of numbers entered by the user in C++ programming, you have to ask
to the user to enter some set of numbers (10 numbers here). Now to find
occurrence of positive, negative and zero from the given set of numbers, just
check all the numbers using for loop whether the number is 0, less than zero or
greater than 0 to count the occurrence of positive, negative and zero as shown
here in the following program.

/* C++ Program - Count Occurrence of Numbers */

#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();
}

Program 25 - Find Largest of Two Number


/* C++ Program - Find Largest of Two Numbers */

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

/* C++ Program - Find Factorial of Number */

#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();
}

Program 28- Find HCF and LCM


/* C++ Program - Find HCF and LCM of Two Numbers */

#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();
}

Program 30- Calculate Area and Circumference of Circle


/* C++ Program - Calculate Area and Circumference of Circle */

#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();
}

Program 31 - Fahrenheit to Centigrade Conversion in C++


/* C++ Program - Fahrenheit to Centigrade Conversion */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float fah, cel;
cout<<"Enter temperature in Fahrenheit : ";
cin>>fah;
cel=(fah-32) / 1.8;
cout<<"Temperature in Celsius = "<<cel;
getch();
}

Program 32- Centigrade to Fahrenheit Conversion


/* C++ Program - Centigrade to Fahrenheit Conversion */

#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();
}

Program 33- Print Fibonacci Series

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.

/* C++ Program - Print Fibonacci Series */

#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();
}

Program 34 - Print ASCII Values of Characters


/* C++ Program - Print ASCII Values of Characters */

#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

To check for palindrome i.e., whether entered number is palindrome or not


in C++ programming, you have to first ask from the user to enter a number.

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.

/* C++ Program - Check Palindrome or Not */

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

To check whether any positive number is an Armstrong number or not, following


is the example:

Since 153 is equal to 1*1*1 + 5*5*5 + 3*3*3. So 153 is an Armstrong


number
Since 12 is not equal to 1*1*1 + 2*2*2. So 12 is not an Armstrong number

/* C++ Program - Check Armstrong or Not */

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

/* C++ Program - Generate Armstrong Numbers */

#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();
}

Program 37 - Find ncR and nPr


/* C++ Program - Find ncR and nPr */

#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;
}

Vous aimerez peut-être aussi