Saturday, 11 June 2016

Cpp Program of Passing 2D array to Function

Passing multidimensional array to function is almost same as that of passing single dimensional array. In this tutorial i will show you how to pass multidimensional array to function and also in future tutorials i will show you how to pass multidimensional array to function using pointers. So let start.


Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

void filling(int arr[5][5]);    //Function Prototype
void printing(int arr[5][5]);

int main()
{
int arr[5][5]={};  // Null Array

filling(arr);  //Function Call
printing(arr);

}


void filling(int arr[5][5])
{
//Filling 2D array
cout<<"Filling Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}

}


void printing(int arr[5][5])
{
//Printing 2D array
cout<<"\nPrinting Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

}



Output:



Cpp Program of Filling and Printing 2D Array

Filling and printing of Multidimensional array is bit tricky than that of one dimensional array. In this tutorial i am going to show you how to fill and print multidimensional array. As you can see in earlier tutorials that in single dimensional array we just used only single for loop, but in 2D array we will use for loop inside for loop or use can say nested loop. The reason is that in 2D array the data is arranged in rows and columns so for controlling row we use outer for loop and for controlling columns we use nested for loop. So let see how it works.


Program:
// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include <iostream>
using namespace std;

int main()
{
int arr[5][5]={};  // Null Array

//Filling 2D array

cout<<"Filling Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}


//Printing 2D array

cout<<"\nPrinting Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

} // End of Program



Output:


Wednesday, 6 January 2016

C++ Snake Game


Here is a source code of Snake Game coded in C++ originally by me. In this game i have just used simple functions, loops, Arrays and rand() function. The border of the game is made by using for loop. And the food is generated using rand function. The kbhit() function is used to sense the key pressed and according to that it moves the snake and eat the food. Hope you will like it.

Source Code:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#‎include‬<iostream>
#include<string>
#include<conio.h>
#include<windows.h>
using namespace std;

const int width = 40;//FOR WIDTH OF FRAME
const int hight = 20;//FOR HIGHT OF FRAME
int foodx, foody;
int x;//FOR CONTROLLING X AXIS
int y;//FOR CONTROLLING Y AXIS
int score = 0;//FOR ADDING SCORE
int tailx[200] = {0};
int taily[200] = {0};
int tail = 0;
enum direction{ LEFT, RIGHT, UP, DOWN };
direction dir;
bool gameover;//FOR STARTING GAME


void start()
{

  gameover = false;
  x = width / 2;//SET THE VALUE OF X=20
  y = hight / 2;//SET THE VALUE OF Y=10
  foodx = rand() % width;//RANDOMLY GENERATE FOOD OF X AXIS
  foody = rand() % hight;//RANDOMLY GENERATE FOOD OF Y AXIS
}


void frame()
{

  system("cls");

  for (int i = 0; i < width + 2; i++)
  cout << "#";//FOR PRINTING UPPER WALL MEAN CEALING
  cout << endl;
 
  for (int i = 0; i < hight; i++)//FOR PRINTING SIDE WALLS
  {
    for (int j = 0; j < width; j++)
    {
      if (j == 0)//PRINT LEFT SIDE WALL
      {
        cout << "#";
      }
    
     if (i == y&&j == x)//FOR MAKING THE SNAKE HEAD IN CENTRE
     cout << "O";

     else if (i == foody&&j == foodx)//FOR GENERATING RANDOM FOOD IN FRAME
     cout << "*";

    else
    {
    bool print = false;

    for (int k = 0; k < tail; k++)
    {
      if (tailx[k] == j&&taily[k] == i)
      {
        cout << "0";
        print = true;
     }
    }

     if (!print) 
     {
      cout << " ";
     }
   }

    if (j == width - 1)//FOR PRINTING RIGHT SIDE WALL
    {
      cout << "#";
    }
 }

 cout << endl;
}

for (int i = 0; i < width + 2; i++)//FOR PRINTING LOWER WALL MEAN FLOOR
{
  cout << "#";

}
 cout << endl;
 cout << "SCORE: " << score << endl;
}


void direct()
{

  if (_kbhit())//WHEN USER ENTER THE KEYBOARD
  {
   switch (_getch())//IT TAKES CHARACTER
   {
    case'a':
    dir = LEFT;
    break;
    case's':
    dir = DOWN;
    break;
    case'w':
    dir = UP;
    break;
    case'd':
    dir = RIGHT;
    break;
   }
  }
}


void move()
{

  int previousx = tailx[0];
  int previousy = taily[0];
  int prev2x = 0;
  int prev2y = 0;
  tailx[0] = x;//remind first tail value of x axis
  taily[0] = y;//remind second tail value of y-ais
 for (int i = 1; i < tail; i++)
 {//for reminding previous tail position
  prev2x = tailx[i];
  prev2y = taily[i];
  tailx[i] = previousx;
  taily[i] = previousy;
  previousx = prev2x;
  previousy = prev2y;

 }

 switch (dir)
 {
  case LEFT://decrease value of x when a is pressed
  x--;
  break;
  case RIGHT://increase value of x when a is pressed
  x++;
  break;
  case UP:
  y--;//decrease value of y when w is pressed
  break;
  case DOWN:
  y++;//increase value of y when s is pressed
  break;
  default:
  break;
 }

 if (x>width - 1 || x<0 || y > hight - 1 || y<0)//when snake hit the wall game end
 gameover = true;

 for (int i = 0; i < tail; i++)
 {
   if (tailx[i] == x&&taily[i] == y)
      gameover = true;
 }

 if (x == foodx&&y == foody)//WHEN FOODX=x AND FOODY=y then generate new food
 {
  foodx = rand() % width;
  foody = rand() % hight;
  tail++;
  score += 10;//when food is eaten then score added by ten
 }

}

int main()
{

  string a;
  cout << "\n\n\n\t\t\t\t THE SNAKE GAME\n\n";
  cout << "CONTROLS:\n\nw=UP\ns=DOWN\na=RIGHT\nd=LEFT\n\n\n";
  cout << "PLEASE ENTER YOUR NAME: ";
  cin >> a;

  start();

 while (!gameover)//game condition is true
 {

  frame();
  direct();
  move();
  Sleep(100);
 }

 system("cls");

  cout << "\n\n\nOPPS YOU ARE OUT" << "\tTRY AGAIN";
  cout << endl;
  cout << a << " YOUR SCORE IS: " << score << endl;

}

 

Passing Array to a Function in Different ways


There are three ways to pass an array to a function. Each way and its program examples are give below:

Here i am sharing a simple program that will initialize and array and then will send that array to a function which will make square of elements of that array.

These three method include passing array to function using pointers. Passing array to function simply with name, And passing array to function with size.

 

 Method 1:

#include <iostream>
using namespace std;

void function(int arr[]);


int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr);
 
  // New Values of Array
  for(int i=0; i<10; i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int arr[])
{
      for(int i=0; i<10; i++)
       arr[i]= arr[i] * arr[i];
}



Method 2:

#include <iostream>
using namespace std;

void function(int *arr);


int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr);
 
  // New Values of Array
  for(int i=0; i<10; i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int *arr)
{
      for(int i=0; i<10; i++)
       arr[i]= arr[i] * arr[i];
}


 

Method 3:

#include <iostream>
using namespace std;

void function(int *arr, int size);

int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr, 10);
 
  // New Values of Array
  for(int i=0; i<=9;i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int *arr, int size)
{
      for(int i=0; i<size; i++)
       arr[i]= arr[i] * arr[i];
}






 

Output:

 


C++ Program to find Transpose of Matrix




In this c++ program we are going to take transpose of a matrix. Transpose of matrix include transforming rows into columns and columns into rows.

In this program we will take an array of five rows and five columns and another array named transpose in which we will store the result of transformation.

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
  int arr[5][5], transpose[5][5];
  int rows=0, columns=0;
 
  cout<<"Enter total number of Rows: ";
  cin>>rows;

  cout<<"Enter total number of Columns: ";
  cin>>columns;
 
 
  for(int i=0;i<rows;i++)
  {
    for(int j=0;j<columns;j++)
    {
       cin>>arr[i][j];
    }
  }
 
 cout<<"Printing Matrix" <<endl;

  for(int i=0;i<rows;i++)
  {
   for(int j=0;j<columns;j++)
   {
     cout<<arr[i][j]<<"     ";
   }
    cout<<endl;
  }

// Finding Transpose of Matrix
   
  for(int i=0;i<rows;i++)
  {
     for(int j=0;j<columns;j++)
     {
        transpose[j][i]=arr[i][j];
     }
        cout<<endl;
  }

//Displaying The Final Result

cout<<"Transpose of Entered Matrix is: " <<endl;   
  for(int i=0;i<columns;i++)
  {
     for(int j=0;j<rows;j++)
     {
        cout<<transpose[i][j]<<"    ";
     }
      cout<<endl;
  }

   return 0;
  
}

Code:

 

C++ Program of Multiplication of Matrix Using 2D Array


In this program we are going to multiply two matrices. For this purpose we will declare two arrays, we will take input in these arrays and then we will multiply them and store their result in a new array.

 

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], multi[10][10];
    int row1=0, column1=0, row2=0, column2=0;
  
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;

/* If columns of first matrix in not equal to rows of second matrix then error will occur, asking user to re-enter size of matrix. */
 
    while (column1!=row2)
    {
  
    cout << "Error Occured! column of first matrix not equal to row of second.";
      
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;
  
    }

/* Filling of first matrix. */

    cout<< "Enter elements of 1st matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column1; j++)
    {
        cout << "Enter element a" << i+1 << j+1 << " : ";
        cin >> a[i][j];
    }

/* Filling of second matrix. */
    cout <<"Enter elements of 2nd matrix: " << endl;
    for(int i=0; i<row2; ++i)
    for(int j=0; j<column2; ++j)
    {
        cout << "Enter element b" << i+1 << j+1 << " : ";
        cin >> b[i][j];
    }

/* Initializing array of multi.*/
    for(int i=0; i<row1; ++i)
    for(int j=0; j<column2; ++j)
    {
       multi[i][j]=0;
    }

/* Multiplying matrix a and b and storing in array of multi. */
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    for(int k=0; k<column1; k++)
    {
        multi[i][j]=multi[i][j] + a[i][k] * b[k][j];
    }

/* Displaying the result. */
    cout <<"Output Matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    {
        cout << " " << multi[i][j];
        if(j==column2-1)
            cout << endl;
    }
    return 0;
}

Output:


Tuesday, 5 January 2016

C++ Program of Sorting Elements of Array

In this C++ program we will sort an unsorted array. First of all we will take input from user using for loop. And then we will sort that array using if condition in nested for loop.

Program:



// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
    int a[10], i=0 ,j=0, temp=0;
  

    for(i=0; i<10; i++)
    {
       cout<<"Enter " <<i <<" Element of Array: ";
        cin>>a[i];
     }

       
    for(i=1; i<10 ;i++)
    {
        for(j=0; j<(10-i); j++)
       
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    }
   

    cout<<"Array after bubble sort is:";

    for(i=0 ;i<10; i++)
    {
     cout<<" "<<a[i];
    }
       
    return 0;

}  //end of main function

Output:

C++ Program to Find Largest Element of Array

In this program we are going to find the largest number in the array. All we are doing in this program is that we are taking one array element as check and we are comparing it with other elements of array and storing the result in that check element of array. In my program we are taking a[0] as a check element and we are storing the result in it.

 

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
    int a[10];
   
    for(int i=0; i<10; i++)
    {
        cout<<"Enter " <<i <<" Element of array: ";
        cin>>a[i];
    }
   
    for(int j=0; j<10; j++)
    {
        if(a[0]<a[j])        /* Change sign '<' to sign '>' if you want to find smallest element of array*/
        a[0]=a[j];
    }
   
    cout<<"The Largest Elemet of Array is: " <<a[0];
   
}

Output:

Note: 
By Changing sign '<' to sign '>' you can find the smallest element of array.
 

C++ Program of Filling and Printing an Array

In this program we are declaring an array of 10. And then we are using For Loop to take input from user of each element of array. And then we are printing each element of the array using another For Loop.

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
    int a[10];
   
    for(int i=0; i<10; i++)
    {
        cout<<"Enter " <<i <<" Element of array: ";
        cin>>a[i];
    }
   
    for(int j=0; j<10; j++)
    {
        cout<<j <<" Element of array is: " <<a[j] <<endl;
    }

}

Output:

C++ Program to print Table of number using Function

In this program we are going to print table of any number using function. Actually we will take input of an integer from a user and then we will pass that number to a function. And after passing number to function we will find table of that number using for loop.

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com


#include<iostream>
using namespace std;

int table(int n);

int main()
{

  int n=0;

  cout<<"Enter Number: ";
  cin>>n;

  table(n);

}

int table(int n)
{
    int t=1;
   
  cout<<"Table of " <<n << " is: " <<endl;
 
  for(int i=1; i<=10; i++)
  {
     t=n*i;
   
     cout<<n <<" * " <<i <<" = " <<t <<endl;
   
  }
 
  return 0;
}

 

Output:

 

C++ Program to Reverse number using function

In this program we will declare an integer and then will take input from user for the integer. After this we will pass the integer in function where we will find its reverse number.

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

 
#include<iostream>
using namespace std;

int reverse(int n);

int main()
{

 int n=0, result=0;

 cout<<"Enter Number: ";
 cin>>n;

 result=reverse(n);
 cout<<"Reverse number is: " <<result <<endl;

}

int reverse(int n)
{

 int temp=0, rev=0;

 while(n!=0)
 {
     temp=n%10;
     rev=(rev*10)+temp;
     n=n/10;
 }

 return rev;

}

Output:

C++ Program to find Factorial of number using Function

In this program we will declare an integer and ask user to enter its value. Then we will pass that integer to function where we will find factorial using for loop.

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

 
#include<iostream>
using namespace std;

int fact(int n);          //function prototype

int main()
{

 int n=0, result=0;

 cout<<"Enter Number: ";
 cin>>n;

 result=fact(n);
 cout<<"Factorial of " <<n <<" is: " <<result <<endl;

}

int fact(int n)   //function definition
{

 int f=1;

 for(int i=1; i<=n; i++)
 {
  f=f*i;
 }

 return f;

}


Output:

C++ program to find roots of quadratic equation

In this program we will find roots of quadratic equation. For this purpose we will take input of three integers a, b and c. After this we will calculate discriminant. And then we will find two roots, and will store them in variable x1, and x2. And then we will find whether roots are imaginary or real.

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

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

int main()
{
    int a=0, b=0, c=0, x1=0, x2=0, d=0;
    int real_Part=0;
    int imaginary_Part=0;
   
   
    cout<<"Enter Value of a: ";
    cin>>a;
   
    cout<<"Enter Value of b: ";
    cin>>b;
   
    cout<<"Enter Value of c: ";
    cin>>c;
   
    d=b*b-4*a*c;
   
    if (d > 0)
    {
        x1 = (-b + sqrt(d)) / (2*a);
        x2 = (-b - sqrt(d)) / (2*a);
        cout << "Roots of quadratic are real and different!" << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;
       }
   
     else if (d == 0)
     {
        cout << "Roots of quadratic equation are real and same." << endl;
        x1 = (-b + sqrt(d)) / (2*a);
        cout << "x1 = x2 =" << x1 << endl;
     }
    
    
     else
     {
        real_Part = -b/(2*a);
        imaginary_Part =sqrt(-d)/(2*a);
        cout << "Roots of quadratic equation are complex and different."  << endl;
        cout << "x1 = " << real_Part << "+" << imaginary_Part << "i" << endl;
        cout << "x2 = " << real_Part << "-" << imaginary_Part << "i" << endl;
    }

}

Output:

Monday, 4 January 2016

C++ Program which tell whether a number is zero or not without using comparator operator

In this program we will find that input value from user is zero or not, without using comparator operator. For this purpose we will user if-else conditional statement. If user if enter zero then zero will go inside if, and we all know that if statement doesn't work on zero, so else condition will become true and it will be executed.

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

 
#include<iostream>
using namespace std;

int main()
{
    int n=0;
   
    cout<<"Enter Number: ";
    cin>>n;
   
    if(n)
    {
        cout<<"Number is non-zero";
    }
   
    else
    cout<<"Number is Zero";
}

Output:

C++ program which print ASCII of an alphabet

In this program we will print ASCII value of alphabet entered by user. For this purpose we will declare a char variable and then we will ask user for entering its value. After this we will convert that character to integer using (int) char_variable.

 Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

 
#include<iostream>
using namespace std;

int main()
{
    char c;
   
    cout<<"Enter Any Alphabet: ";
    cin>>c;
   
    cout<<"ASCII code of this Aplhabet is: " <<(int) c;
}

Output:

C++ Program to Print Table of any Number

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com


#include<iostream>
using namespace std;

int main()
{
    int num=0;
    cout << "Enter number to print its table: ";
    cin >> num;
    for (int i = 1; i <= 10; i++)
    {
        cout << num << " * " << i << " = " << num*i << endl;
    }
   
    return 0;

}

Output:

C++ Program to find Greatest number between three Numbers

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
    int n1=0, n2=0, n3=0;
    cout<<"Enter first number: ";
    cin>>n1;

    cout<<"Enter second number: ";
    cin>>n2;

    cout<<"Enter third number: ";
    cin>>n3;
   
    if(n1>n2&&n1>n3)
    {
     cout<<n1 <<" is greatest of all";
    }
   
    else if(n2>n1&&n2>n3)
    {
     cout<<n2 <<" is greatest of all";
    }
   
    else
    {
     cout<<n3 <<" is greatest of all";
    }

}

Output:

C++ Program take Hours, Minutes, Seconds and convert it in 24 Hours & Standard format

Program

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
   int hour=0 ,min=0, second=0;                        

   cout<<"Enter hours: ";
   cin>>hour;

   cout<<"Enter minutes: ";
   cin>>min;

   cout<<"Enter seconds: ";
   cin>>second;

   if(hour > 24)
   {
     cout<<"Invalid Entery";
   }

   else
   {
    cout<<"\n****24 Hours Format****" <<endl;
    cout<<"Hours : Mins : Seconds" <<endl;
    cout<<hour<<" : "<<min<<" : "<<second <<endl;
   }

 
  if(hour > 12)
 {
  hour=hour-12;
  cout<<"\n****12 Hours Format****" <<endl;
  cout<<"Hours : Mins : Seconds" <<endl;
  cout<<hour<<" : "<<min<<" : "<<second <<endl;
 }

  else
  {
   cout<<"****12 Hours Format****" <<endl;
   cout<<"Hours : Mins : Seconds" <<endl;
   cout<<hour<<" : "<<min<<" : "<<second <<endl;
  }

}

Output:

C++ Program to find Prime Numbers In a Give Range

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int start=0,end=0;
    int found=0,count=0;

    cout<<"Enter Staring Number of Range:  ";
    cin>>start;
    cout<<"Enter Ending Number of Range:  ";
    cin>>end;

    for(int i=start;i<=end;i++)
       {
           for(int j=2;j<=sqrt(i);j++)
               {
               if(i%j==0)
                  count++;
               }
               if(count==0&&i!=1)
               { found++;
                 cout<<"Prime Number = "<<i<<endl;
                 count=0;
               }
               count=0;
       }

 cout<<"Total Prime Number Between Range "<<start<<" to "<<end<<" = "<<found<<endl;

}


 

Output:

 

C++ Program of Calculator

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
    int n1=0, n2=0;
    char c;
   
    cout<<"Enter 1st number: ";
    cin>>n1;

    cout<<"Enter 2nd number: ";
    cin>>n2;   
   
    cout<<"Enter Operator: ";
    cin>>c;
   
    switch(c)
    {
        case '+':
        cout<<"Sum is: " <<n1+n2;
        break;
       
        case '-':
        cout<<"Difference is: " <<n1-n2;
        break;
       
        case '*':
        cout<<"Prduct is: " <<n1*n2;
        break;
       
        case '/':
        cout<<"Division is: " <<n1/n2;
        break;
       
        default:
        cout<<"Invalid Operator!" <<endl;
    }
   
}
   

Output:

C++ Program to find Perfect Numbers

Before looking at the coding of program. Lets discuses Perfect Numbers. Perfect numbers are those number which are equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (That sum is also known as its aliquot sum).


Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
  int n=0, sum=0;
 
  cout<<"Enter Number: ";
  cin>>n;
 
  for(int i=1; i<n; i++)
  {
      if(n%i==0)
      {
      sum=sum+i;
      }
  }
 
    if(sum==n)
  cout<<"Entered Number is a Perfect Number" <<endl;
 
  else
  cout<<"Enter Number is Not a Perfect Number" <<endl;
}

Output: