C++ program to check whether a number is prime or not

//C++ program to check whether a number is prime or not

   #include <iostream>
   #include<conio.h>
   using namespace std;
    int prime(int a)
    {
       int count=0;
      for(int i=2;i<a;i++)
         {
            if(a%i==0)
             {
                 count++;
             }
         }
         if(count==0)
          {
            cout<<"\nThe number "<<a<<" is prime"<<endl;
          }
          else
          {
             cout<<"\nThe number "<<a<<" is not prime"<<endl;
          }
          return a;
    }

 int main()
   {
      int x,i=0;
  //loop to take input 5 time from user
      while(i<5)
      {
      cout<<"Enter a number : ";
      cin>>x;
      prime(x);
      i++;
      }
   }

C++ program to search a number in array

#include<iostream>
using namespace std;

int main()
{
   int array[10],findNum,index,i,j,k=0;
   cout<<"Enter 10 numbers in array : \n";
   for(i=0;i<10;i++)
   {
      cout<<"Enter number "<<i+1<<":  ";
      cin>>array[i];
   }
   cout<<"\n\nEnter number to find in array ";
   cin>>findNum;
   for(j=0;j<10;j++)
   {
      if(findNum==array[j])
      {
       cout<<"\nThe number found in array is "<<array[j]<<endl;
       index=j;
       cout<<"The index of number found in array is "<<index<<endl;
      }
      else
      {
         ++k;
      }
      if(k==10)
      {
         cout<<"\nThe number "<<findNum<<" not found in array "<<endl;
      }
   }
}

C++ program to find maximum and minimum number in array

#include<iostream>
using namespace std;

int main()
{
   int array[10],max,min,i,j,k;
   cout<<"Enter 10 numbers in array : \n";
   for(i=0;i<10;i++)
   {
      cout<<"Enter number "<<i+1<<":  ";
      cin>>array[i];
   }
   max=array[0];
   min=array[0];
   //loop to find maximum No in array
   for(j=1;j<10;j++)
   {
      if(max<array[j])
      {
         max=array[j];
      }

   }
   //loop to find minimum No in array
   for(k=1;k<10;k++)
   {
     if(min>array[k])
      {
         min=array[k];
      }
   }
   cout<<"The maximum number in array is "<<max<<endl;
   cout<<"The minimum number in array is "<<min<<endl;

  return 0;
}

C++ temperature conversion program

#include<iostream>
using namespace std;

 int main()
 {
    int choice;
    float F,C,K;
    cout<<"\nEnter 1 to enter temperature in Centigrade : ";
    cout<<"\nEnter 2 to enter temperature in Fahrenheit : ";
    cout<<"\nEnter 3 to enter temperature in Kelvin : ";
    cout<<"\n\nEnter your choice : ";
    cin>>choice;
    switch(choice)
    {
    case 1:
      {
         cout<<"\nEnter temperature in Centigrade : ";
         cin>>C;
         //  formula to convert centigrade to fahrenheit
         F=(1.8*C)+32;
         //  formula to convert centigrade to kelvin
         K=C+273.15;
         cout<<"\nThe Centigrade temperature in Fahrenheit is = "<<F<<endl;
         cout<<"The Centigrade temperature in Kelvin is = "<<K<<endl;
         break;
      }
    case 2:
      {
         cout<<"\nEnter temperature in Fahrenheit : ";
         cin>>F;
         //  formula to convert fahrenheit to centigrade
         C=0.555*(F-32);
         //  formula to convert fahrenheit to kelvin
         K=0.555*(F-32)+273.15;
         cout<<"\nThe Fahrenheit temperature in Centigrade is = "<<C<<endl;
         cout<<"The Fahrenheit temperature in Kelvin is = "<<K<<endl;
         break;

      }
    case 3:
      {
         cout<<"\nEnter temperature in Kelvin : ";
         cin>>K;
         //  formula to convert kelvin to centigrade
         C=K-273.15;
         //  formula to convert kelvin to fahrenheit
         F=(K-273.15)*1.8+32;
         cout<<"\nThe Kelvin temperature in Centigrade is = "<<C<<endl;
         cout<<"The Kelvin temperature in  Fahrenheit is = "<<F<<endl;
         break;
      }
    default:
      {
         cout<<"You Entered an incorrect choice "<<endl;
      }

    }
    return 0;
 }

C++ program to find GCD and LCM of two numbers

/*GCD of two numbers is the greatest common divisor that
divides both the numbers completely e.g GCD of 15 and 25 is 5.
LCM of two numbers is the product of two numbers divide by its GCD
e.g LCM of 15 and 25 is 15x25/5(gcd)=75 */

#include <iostream>
using namespace std;

int main()
{
   int a,b,gcd,lcm;
   cout<<"Enter two numbers to find its GCD and LCM \n";
   cout<<"Enter first Number ";
   cin>>a;
   cout<<"Enter second Number ";
   cin>>b;

   for(int i=1;i<=a;i++)
   {
      //condition to find highest divisor of both numbers
      if((a%i==0)&&(b%i==0))
      {
         gcd=i;
      }

   }

     cout<<"\nThe GCD Of two numbers "<<a<<" and "<<b<<" is "<<gcd<<endl;
      //formula to find LCM
      lcm=(a*b)/gcd;
     cout<<"The LCM Of two numbers "<<a<<" and "<<b<<" is "<<lcm<<endl;

    return 0;
}


C++ Unique Number program

//C++ unique number program
/*unique No are those No in which no digit is
repeated e.g 123,321 and 12345 are unique Nos*/


#include <iostream>
using namespace std;

int main()
{
//initialize entire array of size 10 to zero
   int arry[10]={0};
   int num,rem,j=0,num1;
   cout<<"Enter number : ";
   cin>>num;
   num1=num;
   while (num!=0)
   {
      rem=num%10;
       //to check all digits of No
      switch(rem){
         case 0:
        ++arry[rem];
           break;
      case 1:
      ++arry[rem];
          break;
      case 2:
         ++arry[rem];
         break;
       case 3:
         ++arry[rem];
         break;
       case 4:
         ++arry[rem];
         break;
       case 5:
         ++arry[rem];
         break;
       case 6:
         ++arry[rem];
        break;
      case 7:
        ++arry[rem];
          break;
      case 8:

         ++arry[rem];
          break;
     case 9:

         ++arry[rem];
         break;

     default:
      cout<<"Enter a valid number "<<endl;
        }

    num=num/10;
   }

  for (int i=0;i<10;i++)
      {
      if (arry[i]>1)
         {

             j++;
         }
      }
    if (j==0)
    {
       cout<<"The number "<<num1<<" is unique "<<endl;
    }
    else
      cout<<"The number "<<num1<<" is not unique "<<endl;

   return 0;
}

C++ program to add subtract and multiply two complex numbers

//C++ Program to add subtract and Multiply Complex Numbers
#include<iostream>
using namespace std;
#include<math.h>

//Declare structure
   struct complex
   {
     int real,img;
   };

   int main()
   {
      complex a,b,c;
      int choice;
   cout<<"Enter First Complex Number:"<<endl;
   cout<<"Enter Real Part of First Complex No=";
   cin>>a.real;
   cout<<"Enter imaginary Part of First Complex No.=";
   cin>>a.img;
   cout<<endl;
   cout<<"Enter Second Complex Number:"<<endl;
   cout<<"Enter Real Part of Second Complex No =";
   cin>>b.real;
   cout<<"Enter imaginary Part of Second Complex No.= ";

   cin>>b.img;

   cout<<endl;
   cout<<"\tEnter 1 to add two complex number \n";
   cout<<"\tEnter 2 to subtract two complex numbers\n";
   cout<<"\tEnter 3 to multiply two complex numbers\n";
   cout<<"Enter your choice ";
   cin>>choice;
   switch(choice)
    {
     case 1:
       {
      //add real with real
      c.real=a.real+b.real;
      //add imaginary with imaginary
      c.img=a.img+b.img;
      cout<<"********************************************************"<<endl;

      //to check whether the imaginary part is positive or negative
      if (c.img<0)
      cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<c.img<<"i"<<endl;

      else
      cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<"+"<<c.img<<"i"<<endl;
      break;
         }
  case 2:
      {
      c.real=a.real-b.real;
      c.img=a.img-b.img;

      if (c.img<0)
      {
        cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
      }
      else
       cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl;
       break;
     }
  case 3:
    {
   //multipication of complex Nos start here

      c.real=(a.real*b.real+(a.img*b.img)*-1);
      c.img=a.img*b.real+b.img*a.real;

      if (c.img<0)
         {
         cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
         }
      else
       cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl<<endl;
            break;
      }
    }
   return 0;
 }

C++ program to find average of ten numbers

//program to find average of ten numbers enter by user
#include <iostream>
using namespace std;

 int main()
   {
      int num,sum=0,count=0,avg;
      cout<<"Enter ten numbers to find its average"<<endl;
     for(int i=0;i<10;i++)
      {
         cout<<"Enter number "<<i+1<<" : ";
         cin>>num;
         sum=sum+num;
         count++;
      }
      avg=sum/count;
      cout<<"\nAverage of ten Numbers is = "<<avg<<endl;
      return 0;
   }

C++ program to convert seconds into hours minutes and seconds

//program to convert seconds into hours minutes and seconds
#include <iostream>
using namespace std;

 int main()
   {
       int sec,min,hr,sec1;
       cout<<"Enter seconds : ";
       cin>>sec;
       hr=sec/3600;
       sec=sec%3600;
       min=sec/60;
       sec1=sec%60;

       cout<<"\nNumber of hours = "<<hr<<endl;
       cout<<"Number of minutes = "<<min<<endl;
       cout<<"Number of seconds = "<<sec1<<endl;

      return 0;
   }


C++ program to print multipication table of a number

#include <iostream>
using namespace std;

 int main()
   {
       int num,i=1;
      cout<<"Enter Number to Make its table: ";
      cin>>num;
      while(i<=10)
      {
       cout<<num<<" * "<<i<<" = "<<num*i<<endl;
        i++;

      }
      return 0;
   }

C++ perfect number program

/*program to check whether a number is perfect
or not. Perfect numbers are those whose factors
sum is equal to the number itself e.g 6 and 28 are perfect
factorsSum   1+2+3=6    1+2+4+7+14=28  */

#include <iostream>
using namespace std;

 //function to find perfect number
void perfectNum(int x)
{
   int factorSum=0;
   for(int i=1;i<x;i++)
   {
      if(x%i==0)
      {
      //nested if to print factors 1+2+4....
       if(i==1)
       {
          cout<<"\n"<<i;
          factorSum=factorSum+i;
       }
       else
       {
          cout<<"+"<<i;
          factorSum=factorSum+i;
       }
      }

   }
   if(factorSum==x)
   {
      cout<<"="<<x;
      cout<<"\n\nThe number "<<x<<" is perfect\n";
   }
   else
   {
      cout<<" not = "<<x;
      cout<<"\n\nThe number "<<x<<" is not perfect"<<endl;
   }
}

int main()
 {
    int num;;
    cout<<"Enter a number ";
    cin>>num;
    perfectNum(num);

 return 0;
}

C++ program to find Permutation and Combination

// Program to find permutation(nPr) and combination(nCr)
#include <iostream>
using namespace std;
//recursive function to find factorial
 int fact(int a)
 {
    if(a>1)
    {
       return a*fact(a-1);
    }
    else
    {
       return 1;
    }
 }

int main()
 {
int num1,num2,num3,n,r,choice;
cout<<"Enter the value of n ";
cin>>n;
cout<<"Enter the value of r ";
cin>>r;
if(n<r)
   {
      cout<<"The value of n must be greater than r "<<endl;
      cout<<"Enter again ";
      cin>>n>>r;
   }
cout<<"\tEnter 1 to find permutation "<<endl;
cout<<"\tEnter 2 to find Combination\n "<<endl;
cout<<"Enter Your choice ";
cin>>choice;
switch(choice)
{
   case 1:
      {
         num1=fact(n);
         num2=fact(n-r);
         num3=num1/num2;
         cout<<"nPr = "<<num3<<endl;
         break;
      }
   case 2:
      {
         num1=fact(n);
         num2=fact(n-r)*fact(r);
         num3=num1/num2;
         cout<<" nCr = "<<num3<<endl;
         break;
      }
   default:
      {
         cout<<"You Entered an incorrect choice :"<<endl;
         break;
      }
}


return 0;
}

C++ program to make simple calculator

#include <iostream>
using namespace std;

int main()
{
    int num1,num2,numStore,choice;
    cout<<"Enter first number : ";
    cin>>num1;
    cout<<"Enter second number : ";
    cin>>num2;
    cout<<"\t\tEnter 1 for addition\n";
    cout<<"\t\tEnter 2 for subtraction\n";
    cout<<"\t\tEnter 3 for multipication\n";
    cout<<"\t\tEnter 4 for Division\n";
    cout<<"Enter Your Choice : ";
    cin>>choice;
    switch(choice)
    {
    case 1:
      {
         numStore=num1+num2;
         cout<<"The sum of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
         break;
      }
      case 2:
      {
         numStore=num1-num2;
         cout<<"The difference of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
         break;
      }
      case 3:
      {
       numStore=num1*num2;
      cout<<"The product of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
         break;
      }
      case 4:
      {
         if(num2==0)
         {
            cout<<"Division by Zero is not Possible";
            break;
         }

      numStore=num1/num2;
      cout<<"The result of division of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
         break;
      }
    }
     cout << endl;
   return 0;
}

C++ program to find factors of number entered by User

//C++ program to find factors of a number
#include <iostream>
using namespace std;
int main()
{
    int number,i ;

    cout << "Enter the number to find its factors : ";
    cin >> number;
    cout << "The factors of " << number << " are : ";

   for(i=1;i<number;i++)
   {
      if(number%i==0)
      {
         cout<<i<<" ";
      }
   }
     cout << endl;
 return 0;
}

C++ program to find Factorial of a number

 //C++ program to find factorial of number using function
#include <iostream>
using namespace std;
void fact(int a)
{
   int num=1;
   for(int i=1;i<=a;i++)
   {
      num=num*i;
   }
   cout<<"\nThe factorial of the number "<<a<<" is = "<<num;
}


int main()
 {
 int num;
 cout<<"Enter a number to find its Factorial ";
 cin>>num;
 fact(num);

 return 0;
}

C++ program to find Factorial of number using Recursion

//C++ program to find factorial of number using recursive function
#include <iostream>
using namespace std;
int fact(int a)
{
   if(a>1)
   {
      return a*fact(a-1);
   }
   else
   {
      return 1;
   }
}


int main()
 {
int num,num1;
cout<<"Enter a number to find its Factorial ";
cin>>num;
num1=fact(num);
cout<<"The factorial of the number "<<num<<" is = "<<num1;

return 0;
}

C++ program to find sum of Natural numbers

/*this program find sum of
 natural numbers upto the range entered by user*/
#include <iostream>
using namespace std;

int main()
 {
 int sumStore=0,i,Srange,Erange;
 cout<<"Enter start range ";
 cin>>Srange;
 cout<<"Enter End range ";
 cin>>Erange;
 for(i=Srange;i<=Erange;i++)
   {
      sumStore=sumStore+i;
   }

 cout<<"\nSum of Natural numbers from "<<Srange<<" to "<<Erange<<" is= "<<sumStore;
 cout<<endl;
 return 0;
}

C++ program to Reverse a number

#include <iostream>
using namespace std;

int main()
 {
 int sumStore=0,num,rem,num1;
 cout<<"Enter a number to Reverse it ";
 cin>>num;
 num1=num;
 while(num>0)
   {
      rem=num%10;
      //to store number
      sumStore=(sumStore*10)+rem;
      num=num/10;

   }
   cout<<"\nThe Reverse of "<<num1<<" is "<<sumStore<<endl;

 return 0;
}

C++ Palindrome number program

#include <iostream>
using namespace std;

int main()
 {
int sumStore=0,num,rem,palindrome,num1;
cout<<"Enter a number ";
cin>>num;
palindrome=num;
num1=num;
//loop to check every digit of number Entered
while(num!=0)
   {
      rem=num%10;
      //to store number
      sumStore=(sumStore*10)+rem;
      num=num/10;

   }
   //to check Entered number is palindorme or not
   if(sumStore==palindrome)
   {
      cout<<"The Number "<<num1<<" is Palindrome"<<endl;
   }
   else
   {
      cout<<"The Number "<<num1<<" is not Palindrome"<<endl;

   }

return 0;
}

C++ program to find sum of Odd numbers

#include <iostream>
using namespace std;

int main()
 {
 int sumStore=0,i,Srange,Erange;
 cout<<"Enter start range ";
 cin>>Srange;
 cout<<"Enter End range ";
 cin>>Erange;
 for(i=Srange;i<=Erange;i++)
   {
      if(i%2!=0)
      {
         sumStore=sumStore+i;
      }
   }
   cout<<endl;
 cout<<"Sum of odd numbers from "<<Srange<<" to "<<Erange<<" is = "<<sumStore<<endl;
 return 0;
}

C++ program to convert Decimal to Binary

#include <iostream>
using namespace std;

int main()
 {
 int sumStore=0,base=1,dec,num,rem;

 cout<<"Enter a decimal number : ";
 cin>>num;
 dec=num;

  while(num!=0)
    {
      rem=num%2;
      sumStore=sumStore+(rem*base);
      base=base*10;
      num=num/2;

    }

 cout<<"\nThe binary Equivalent of Decimal No "<<dec<<" is = "<<sumStore<<endl;


 return 0;
}

C++ program to convert Binary to Decimal

#include <iostream>
using namespace std;

int main() {
int sumStore=0,base=1,bin,num,num1,rem;

cout<<"Enter a binary number : ";
cin>>num;
bin=num;
num1=num;

  //Loop to check the number is binary or not
while(num!=0)
   {

if((num%10!=0 )&& (num%10!=1))
     {
      cout<<"The number "<<bin<<" is not binary\n";
      return 0;
     }

      num=num/10;

    }
    //if number is binary then this loop will execute
   while(num1!=0)
    {
      rem=num1%10;
      sumStore=sumStore+(rem*base);
      base=base*2;
      num1=num1/10;

    }
   cout<<"\nThe decimal Equivalent of binary No "<<bin<<" is = "<<sumStore<<endl;


return 0;
}

C++ program to find Sum of digits of a number Entered by User

#include <iostream>
using namespace std;

int main(void) {
int sum=0,count=0,i,a,rem,num;

cout<<"Enter a number to sum its digits : ";
cin>>num;
a=num;
for(i=0;num!=0;i++)
   {
      rem=num%10;
      sum=sum+rem;
      num=num/10;
      count++;

   }
   cout<<"\nThe sum of "<<count<<" digited number "<<a<<" is = "<<sum<<endl;
return 0;
}

C++ program to swap two numbers

#include<iostream>
using namespace std;


void swap(int & i,int & j);


int main()
   {
   int num1, num2;
   cout<<"Enter first Number :";
   cin>>num1;
   cout<<"Enter Second Number :";
   cin>>num2;
   cout<<endl<<endl;

    cout << "Before swapping:"<<endl;
    cout<<"First Number is = " << num1<<endl;

 cout<<"Second number is = " << num2<<endl<<endl;

    swap(num1,num2);

    cout << "After swapping"<<endl;
    cout<<" First number is =  " << num1 <<endl;

 cout<<" Second number is = " << num2 <<endl<<endl;


     return 0;

}
void swap(int& i, int& j)
{
    int t = i;
    i = j;
 j = t;
}


C++ program to print diamond pattern

//C++ program to print diamond pattern
#include<iostream>
using namespace std;

int main()
{
   int rows,i,j;
   cout<<"Enter Number of rows ";
   cin>>rows;
    for(i=1;i<=rows;i++)
    {
       for(j=rows-i;j>=1;j--)
       {
     cout<<" ";
       }
       for(j=1;j<=i;j++)
       {
    cout<<"* ";

       }
       cout<<endl;
    }

   for(i=rows-1;i>=1;i--)
    {
       for(j=rows-i;j>=1;j--)
       {
     cout<<" ";
       }
       for(j=1;j<=i;j++)
       {
    cout<<"* ";

       }
  cout<<endl;}
return 0;
}

C++ program to print Inverted triangle

#include<iostream>
using namespace std;

int main()
{
   int rows,i,j;
   cout<<"Enter Number of rows ";
   cin>>rows;
   cout<<endl;

 for(i=rows-1;i>=1;i--)
    {
       for(j=rows-i;j>=1;j--)
       {
     cout<<" ";
       }
       for(j=1;j<=i;j++)
       {
    cout<<"* ";

       }
    cout<<endl;}
 return 0;
}

C++ program to print Triangle pattern of asterisks

#include<iostream>
using namespace std;

int main()
{
   int rows;
   cout<<"Enter Number of rows ";
   cin>>rows;

for (int j=0;j<rows;++j)
{
for(int k=rows-j;k>0;--k)
{
cout<<" ";
}
for (int m=0;m<j;++m)
{

{
cout<<"*"<<" ";
}

}
cout<<"   "<<endl<<"   ";
}
return 0;
}

C++ program to print Fabonacci series upto n terms

#include <iostream>
using namespace std;

int main()
{
   int i=0,j=1,sum=0,limit;

   cout<<"Enter limit for series :";
   cin>>limit;
   cout<<"The fabanacci series "<<"upto "<<limit<<" term is "<<i<<" ";

   for (int k=1;k<limit;k++)
   {

      sum=i+j;
      cout<<sum<<" ";
      i=i+j;
      j=i-j;

   }
   cout<<endl;
return 0;

}

C++ Tower of Hanoi program

#include <iostream>
using namespace std;
#include<math.h>

int move;
void movetower(int n,char from,char to,char temp);
void movedisk(char from,char to);
int main()
{
    int disks,moves;
    cout<<"\t\tWelcome to Tower of Hanoi Program : "<<endl;
    cout<<"\t\tEnter number of disks ";
    cin>>disks;
    cout<<endl;
    movetower(disks,'1','2','3');
    // to count total number of moves of disk
    moves=(pow(2,disks))-1;

    cout<<"\n\t\tTotal moves is = "<<moves<<endl;
    return 0;
}
void movetower(int n,char from,char to,char temp)
{
   if (n==1)
{


      movedisk(from,to);
}
      else
      {
         movetower(n-1,from,temp,to);
         movedisk(from,to);
         movetower(n-1,temp,to,from);
      }


}
void movedisk(char from,char to)
{

   cout<<"\t\tMove top disk from "<<from<<" to "<<to<<endl;

}

C++ program to find Prime Numbers between two Numbers

//C++ program to Find prime Numbers b/w two intervals

   #include <iostream>
   #include<conio.h>
   using namespace std;
    int prime(int a)
    {
       int count=0;
      for(int i=2;i<a;i++)
         {
            if(a%i==0)
             {
                 count++;
             }
         }
         if(count==0)
          {
            cout<<a<<" ";
          }
          return a;
    }

 int main()
   {
      int a,b;
      cout<<"Enter two numbers to find prime Number b/w it "<<endl;
      cout<<"Enter first No : ";
      cin>>a;
      cout<<"Enter second No : ";
      cin>>b;
      cout<<"\nThe prime number b/w "<<a<<" and "<<b<<" are "<<endl<<endl;
      for(int i=a+1;i<b;i++)
      {
        prime(i);
      }
      getch();
      return 0;
   }