Home > C++ > Simple C++ Programming Examples

You can define a programming language as the formal language in which you can specify a set of instructions. These instructions collectively perform some task and generate an output. In general programming languages are the collection of instructions for the computer. There is a huge of list of programming languages exist. The complete list is available at Wikipedia:

Some well-known programming languages include C, C++, C#, Java, Python, JavaScript, PHP etc. The syntax for writing programs in these languages differ, but the basic idea behind every program is same. For example, if you understand the working of loops in any one of these languages. Then you can use loops in any of these languages. Because their basic working remains same. i.e. loops will always be used to repeat the specific set of instructions. No matter they are used in C++ or java or in php.  Similar is the case with every other data structure available in these languages.

Simple C++ Programming Examples

c++ code example - Simple C++ Programming Examples

Here are below given some simple c++ programming examples to understand the basic functionality of different data structures.

Display a string or message to the user

C++ program to Display a string or message on the console.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     cout << "This is a simple c++ programming example \n";
     return 0;
}

Input From User

Take input from the user and display it on the console.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x;
     cout << "Please Enter Value for x :";
     cin >> x;
     cout << "\nValue for x = " << x<<endl;
     return 0;
}

This above example is using an integer variable. Similarly, we can use other data types as well. Like float char etc. However, taking input for strings is a little bit different. Have a look at this example program.

#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main()
{
     string a;
     cout << "Please Enter Value for a :";
     getline(cin, a);
     cout << "\nValue for a = " << a<<endl;
     return 0;
}

The below-given program takes two integers from the user and displays their sum on the console to the user.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
    int x,y,z;
    cout << "Please Enter Value for x :";
    cin >> x;
    cout << "Please Enter Value for y :";
    cin >> y;
    z = x + y;
    //any one of the below can be used. These two work same.
    cout << "\nSum for " << x << " & " << y << " = " << x+y <<endl;
    cout << "\nSum for " << x << " & " << y << " = " << z << endl;
    return 0;
}

If you are curious to know how much bytes a data type takes in your memory you can use the following program.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     cout << "Size of an integer is : " << sizeof(int) << " bytes" << endl;
     cout << "Size of a single character is : " << sizeof(char) << " bytes" << endl;
     cout << "Size of floating point value is : " << sizeof(float) << " bytes" << endl;
     cout << "Size of double data type is : " << sizeof(double) << " bytes" << endl;
     return 0;
}

Swapping The Numbers

Swapping two numbers is also a very simple task.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x, y, z;
     cout << "Please Enter Value for x : ";
     cin >> x;
     cout << "Please Enter Value for y : ";
     cin >> y;
     z = x; // making a copy of value of x. To assign this value to y.
     x = y; // Assigning value of y to x;
     y = z;//Now assigning the previous value of x to y.
     cout << "New Value of x = "<<x;
     cout << "\nNew Value of y = "<<y<<endl;
     return 0;
}

That’s really easy right? Now what if you are asked to swap the values of two variables without using the third variable. Below given simple program does that for us.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x, y, z;
     cout << "Please Enter Value for x : ";
     cin >> x;
     cout << "Please Enter Value for y : ";
     cin >> y;

     x = x + y;
     y = x - y;
     x = x - y;
     cout << "New Value of x = "<<x;
     cout << "\nNew Value of y = "<<y<<endl;
     return 0;
}

You can do it in other ways as well. But I’ll leave that for you. Now I think that’s enough with the input and output let’s move to some other cool stuff like Conditional statements.

You may have come across some questions in which you have to find the largest number of the given two or three numbers. Let’s have a look at that program. Here are given the simple C++ programming examples of finding the largest number.

Using if-else statements.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x, y, z;
     cout << "Please Enter Value for x : ";
     cin >> x;
     cout << "Please Enter Value for y : ";
     cin >> y;
     cout << "Please Enter Value for z : ";
     cin >> z;
     if (x > y && x > z)
     {
           cout << x<<"is largrer\n";
     }
     else if(y > z && y > x)
     {
           cout << y << "is largrer\n";
     }
     else 
     {
           cout << z << "is largrer\n"; 
     }
     return 0;
}

Note: – In this above program, we didn’t check if the numbers are equal. There is the possibility that user may enter the equal numbers.  For that, we need to add the following line of code before executing the conditions in our above program.

if (x == y && y == z) {
/* Write a proper message explaining that entered numbers 
are equal and exit form program. either by returning or using exit function.*/
}

There is another way to find out the largest number of the given numbers.

Using ternary operator

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x, y, z,a;
     cout << "Please Enter Value for x : ";
     cin >> x;
     cout << "Please Enter Value for y : ";
     cin >> y;
     cout << "Please Enter Value for z : ";
     cin >> z;
     a = (x > y && x > z) ? x : (y > z && y > x) ? y : z;
     cout << a << "is the largest number.\n";
     return 0;
}

What is a prime number? In mathematics, we can say a number is prime if it is divisible by only two numbers. One and the number itself. Or we can say that a prime will have only two factors.

  • One
  • Number itself.

The program below checks if a number is prime or not and displays the result.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x;
     bool noIsPrime = true;
     cout << "Please Enter a positive Integer Value: ";
     cin >> x;
     for (int i = 2; i <= x / 2; i++)
     {
           if (x % i == 0)
           {
                 noIsPrime = false;
                 break;
           }
     }
     if (noIsPrime )
     {
          cout << x <<" is a prime number\n";
     }
     else
     {
          cout <<x << " is not a prime number\n";
     }
     return 0;
}

The above example uses for loop thus explaining the working of the loops.

C++ code example for finding the Factorial of a Number

Let’s find the factorial of a number.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int x, factorial = 1;
     bool isPrime = true;
     cout << "Please Enter a positive number to find factorial : ";
     cin >> x;
     if (x == 0 || x == 1) 
     {
           factorial = 1;
     }
     else 
     {
           for (int i = 1; i <= x; i++)
          {
                factorial *= i;
          }
     }
     cout << "Factorial of "<<x<<" = "<<factorial<<endl;
     return 0;
}

We can also find the Factorial by using the recursive function.

#include "stdafx.h"
#include "iostream"
using namespace std;
int factorial(int a);
int main()
{
     int x;
     bool isPrime = true;
     cout << "Please Enter a positive number to find factorial : ";
     cin >> x;
     cout << "Factorial of "<<x<<" = "<< factorial(x) <<endl;
     return 0;
}
int factorial(int x) 
{
     if (x == 0 || x == 1) 
     {
          return 1;
     }
     else 
     {
          return x * factorial(x - 1);
     }
}

Multiplication of matrices

The below given program uses arrays and loops to show multiplication of the matrices.

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
     int a[10][10], b[10][10], mult[10][10] = { 0 }, rows1, cols1, rows2, cols2, k;
     do 
     {
          cout << "Enter number of rows for 1st matrix: ";
          cin >> rows1;
          cout << "Enter number of columns for 1st matrix: ";
          cin >> cols1;
          cout << "Enter number of rows for 2nd matrix: ";
          cin >> rows2;
          cout << "Enter number of columns for 2nd matrix: ";
          cin >> cols2;
     } while (cols1 != rows2);
      /* We can multiply two matrices if number of columns of matrix one 
                  are equal to number of rows of matrix 2 */

     cout << endl << "Enter values for 1st matrix :" << endl;
     for (int i = 0; i < rows1; i++)
     {
          for (int j = 0; j < cols1; j++)
          {
                cout << "Enter element at a[" << i + 1 <<"]["<< j + 1 << "] : ";
                cin >> a[i][j];
          }
     }
     cout << endl << "Enter values for 2nd matrix :" << endl;
     for (int i = 0; i < rows2; i++)
     {
          for (int j = 0; j < cols2; j++)
          {
               cout << "Enter element at b[" << i + 1 << "][" << j + 1 << "] : ";
               cin >> b[i][j];
          }
     }
     for (int i = 0; i < rows1; i++)
     {
          for (int j = 0; j < cols2; j++)
          {
               for (int k = 0; k < cols1; ++k)
               {
                    mult[i][j] += a[i][k] * b[k][j];
               }
          }
     }
     cout << endl << "Output Matrix: " << endl;
     for (int i = 0; i < rows1; i++)
     {
          for (int j = 0; j < cols2; j++)
          {
               cout << " " << mult[i][j];
               if (j == cols2 - 1)
               {
                    cout << endl;
               }
          }
     }
     return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *