Prime number up to N number using c++

 Let's see the prime number program in C++. In this C++ program, we will take an input from the user and Print series of prime numbers up to N number.

#include <iostream>  
using namespace std;  

int main()  
{  
  int n, i, j; 
  cout << "Enter the a Number:  ";  
  cin >> n;  
  cout << "Series of prime numbers up to "<<n<<"is "<<endl ;
  for(i = 2; i <= n; i++)  
  {  
      for(j=2;j<=i;j++)  
      {  
           if (i%j==0)  
             break;  
      }  
   }  
   if (i==j)  
      cout << "  "<<i;  
  return 1;  

Output:

Enter the a Number:   30

Series of prime number up to 30 is 2 3 5 7 11 13 17 19 23 29


C++ Program to print Number Triangle

 Like alphabet triangle, we can write the C++ program to print the number triangle. The number triangle can be printed in different ways.

Let's see the C++ example to print number triangle.

#include <iostream>  
using namespace std;  
int main()  
{  
    int i,j,k,l,n;    
    cout<<"Enter the Range=";    
    cin>>n;    
    for(i=1;i<=n;i++)    
    {    
       for(j=1;j<=n-i;j++)    
      {    
         cout<<" ";    
      }    
      for(k=1;k<=i;k++)    
     {    
        cout<<k;    
     }    
    for(l=i-1;l>=1;l--)    
    {    
       cout<<l;    
    }    
   cout<<"\n";    
  }    
return 0;  
}  

 
Enter the Range=5
     1
    121
   12321
  1234321
 123454321  
Enter the Range=6
      1
     121
    2321
   1234321 
  123454321
 12345654321  

C++ Program to Print Alphabet Triangle

 There are different triangles that can be printed. Triangles can be generated by alphabets or numbers. In this C++ program, we are going to print alphabet triangles.

Let's see the C++ example to print alphabet triangle.

#include <iostream>  
using namespace std;  
int main()  
{  
 char ch='A';    
    int i, j, k, m;    
    for(i=1;i<=5;i++)    
    {    
        for(j=5;j>=i;j--)    
            cout<<" ";    
        for(k=1;k<=i;k++)    
            cout<<ch++;    
            ch--;    
        for(m=1;m<i;m++)    
            cout<<--ch;    
        cout<<"\n";    
        ch='A';    
    }    
return 0;  
}

Output:

   
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA


C++ Program to convert Decimal to Binary

We can convert any decimal number (base-10 (0 to 9)) into binary number (base-2 (0 or 1)) by C++ program.

Decimal Number

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 223, 585, 192, 0, 7 etc.

Binary Number

Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.

Let's see the some binary numbers for the decimal number.

DecimalBinary
10
210
311
4100
5101
6110
7111
81000
91001
101010

Decimal to Binary Conversion Algorithm

Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array

Step 2: Divide the number by 2 through / (division operator)

Step 3: Repeat the step 2 until the number is greater than zero

Let's see the C++ example to convert decimal to binary.

#include <iostream>  
using namespace std;  
int main()  
{  
  int a[10], n, i;    
  cout<<"Enter the number to convert: ";    
  cin>>n;    
  for(i=0; n>0; i++)    
 {    
   a[i]=n%2;    
   n= n/2;  
  }    
 cout<<"Binary of the given number= ";    
 for(i=i-1 ;i>=0 ;i--)    
 {    
   cout<<a[i];    
 }    
}

Output:

Enter the number to convert: 9
Binary of the given number= 1001


Matrix multiplication in C++

We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row number, column number, first matrix elements and second matrix elements. Then we are performing multiplication on the matrices entered by the user.

In matrix multiplication first matrix one row element is multiplied by second matrix all column elements.

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure given below:

Let's see the program of matrix multiplication in C++

#include <iostream>  
using namespace std;  
int main()  
{  
   int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    
   cout<<"enter the number of row=";    
   cin>>r;    
   cout<<"enter the number of column=";    
   cin>>c;    
   cout<<"enter the first matrix element=\n";    
   for(i=0;i<r;i++)    
  {    
    for(j=0;j<c;j++)    
   {    
     cin>>a[i][j];  
   }    
 }    
 cout<<"enter the second matrix element=\n";    
 for(i=0;i<r;i++)    
 {    
   for(j=0;j<c;j++)    
   {    
     cin>>b[i][j];    
   }    
 }    
 cout<<"multiply of the matrix=\n";    
 for(i=0;i<r;i++)    
 {    
   for(j=0;j<c;j++)    
   {    
      mul[i][j]=0;    
      for(k=0;k<c;k++)    
     {    
         mul[i][j]+=a[i][k]*b[k][j];    
     }    
  }    
}    
//for printing result    
for(i=0;i<r;i++)    
{    
  for(j=0;j<c;j++)    
  {    
     cout<<mul[i][j]<<" ";    
   }    
   cout<<"\n";    
  }    
 return 0;  
}


Output:
enter the number of row=3  
enter the number of column=3  
enter the first matrix element= 
1 2 3
1 2 3  
1 2 3       
enter the second matrix element= 
1 1 1  
2 1 2   
3 2 1    
 multiply of the matrix=  
14 9 8      
14 9 8  
14 9 8

    

C++ Program to swap two numbers without third variable

We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable:
  1. By + and -
  2. By * and /

Program 1: Using * and /

Let's see a simple C++ example to swap two numbers without using third variable.

#include <iostream>  
using namespace std;  
int main()  
{  
int a=5, b=10;      
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;      
a=a*b; //a=50 (5*10)    
b=a/b; //b=5 (50/10)    
a=a/b; //a=10 (50/5)    
cout<<"After swap a= "<<a<<" b= "<<b<<endl;      
return 0;  



Output:


Before swap a= 5 b= 10     
After swap a= 10 b= 5

Program 2: Using + and -

Let's see another example to swap two numbers using + and -.

#include <iostream>  
using namespace std;  
int main()  
{  
int a=5, b=10;      
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;      
a=a+b; //a=15 (5+10)    
b=a-b; //b=5 (15-10)    
a=a-b; //a=10 (15-5)    
cout<<"After swap a= "<<a<<" b= "<<b<<endl;      
return 0;  
}  
Output:


Before swap a= 5 b= 10  
After swap a= 10 b= 5

reverse number using c++

   We can reverse a number in C++ using loop and arithmetic operators. In this program, we are getting number as input from the user and reversing that number.

 Let's see a simple C++ example to reverse a given number.

#include <iostream>  
using namespace std;  
int main()  
{  
int n, reverse=0, rem;    
cout<<"Enter a number: ";    
cin>>n;    
  while(n!=0)    
  {    
     rem=n%10;      
     reverse=reverse*10+rem;    
     n/=10;    
  }    
 cout<<"Reversed Number: "<<reverse<<endl;     
return 0;  
}


Output:
Enter a number: 234  
Reversed Number: 432

Sum of digits program in C++

We can write the sum of digits program in C++ language by the help of loop and mathematical operation only.

Sum of digits algorithm

To get sum of each digit by C++ program, use the following algorithm:
  • Step 1: Get number by user
  • Step 2: Get the modulus/remainder of the number
  • Step 3: sum the remainder of the number
  • Step 4: Divide the number by 10
  • Step 5: Repeat the step 2 while number is greater than 0.
Let's see the sum of digits program in C++.

#include <iostream>  
using namespace std;  
int main()  
{  
    int n,sum=0,m;    
    cout<<"Enter a number: ";    
    cin>>n;    
    while(n>0)    
   {    
     m=n%10;    
     sum=sum+m;    
     n=n/10;    
   }    
   cout<<"Sum is= "<<sum<<endl;    
   return 0;  
}


Output:

Enter a number: 23  
Sum is= 5


Enter a number: 624       
Sum is= 12


Armstrong Number in C++

Before going to write the C++ program to check whether the number is Armstrong or not, let's understand what is Armstrong number.

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Let's try to understand why 371 is an Armstrong number.

371 = (3*3*3)+(7*7*7)+(1*1*1)    
where:    
(3*3*3)=27    
(7*7*7)=343    
(1*1*1)=1    
So:    
27+343+1=371 

Let's see the C++ program to check Armstrong Number.

#include <iostream>  
using namespace std;  
int main()  
{  
int n,r,sum=0,temp;    
cout<<"Enter the Number=  ";    
cin>>n;    
temp=n;    
while(n>0)    
{    
r=n%10;    
sum=sum+(r*r*r);    
n=n/10;    
}    
if(temp==sum)    
cout<<"Armstrong Number."<<endl;    
else    
cout<<"Not Armstrong Number."<<endl;   
return 0;  
}  
Output:
Enter the Number= 371
Armstrong Number.
Enter the Number= 342   
Not Armstrong Number.

Factorial program in C++

Factorial Program in C++: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

4! = 4*3*2*1 = 24  
6! = 6*5*4*3*2*1 = 720

Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in C++ language. Let's see the 2 ways to write the factorial program.
  • Factorial Program using loop
  • Factorial Program using recursion





Factorial Program using Loop

Let's see the factorial Program in C++ using loop.

#include <iostream>  
using namespace std;  
int main()  
{  
   int i,fact=1,number;    
  cout<<"Enter any Number: ";    
 cin>>number;    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  cout<<"Factorial of " <<number<<" is: "<<fact<<endl;  
  return 0;  


Output:
Enter any Number: 5  
Factorial of 5 is: 120  

Factorial Program using Recursion

Let's see the factorial program in C++ using recursion.
#include<iostream>    
using namespace std;      
int main()    
{    
int factorial(int);    
int fact,value;    
cout<<"Enter any number: ";    
cin>>value;    
fact=factorial(value);    
cout<<"Factorial of a number is: "<<fact<<endl;    
return 0;    
}    
int factorial(int n)    
{    
if(n<0)    
return(-1); /*Wrong value*/      
if(n==0)    
return(1);  /*Terminating condition*/    
else    
{    
return(n*factorial(n-1));        
}    
}
Output:
Enter any number: 6   
Factorial of a number is: 720

Palindrome number algorithm

palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.

Palindrome number algorithm

  • Get the number from user
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print palindrome number
  • Else print not palindrome number
Let's see the palindrome program in C++. In this program, we will get an input from the user and check whether number is palindrome or not.



#include <iostream>   
using  namespace  std;  
int  main()  
{  
   int  n,r,sum=0,temp;    
  cout<< "Enter the Number=" ;    
  cin>>n;    
 temp=n;    
  while (n>0)    
{    
 r=n%10;    
 sum=(sum*10)+r;    
 n=n/10;    
}    
if (temp==sum)    
cout<< "Number is Palindrome." ;    
else     
cout<< "Number is not Palindrome." ;   
   return  0;  


Output:
Enter the Number=121   
 Number is Palindrome. 

Enter the number=113  
Number is not Palindrome.