Category: CPlusPlus Programs

c++ logo

  • C++ Program to Delete an Element from an Array

    In this tutorial, we will learn and write code on deleting an element from an array in C++. To understand the coding, you should have knowledge of the following topics in C++ programming:


    Delete Element from Array in C++

    The program takes user input for the size of an array and for the elements of an array. Then the program will ask the user to enter a number from the array to be deleted.

    Lastly, after the deletion of an element, the new array is displayed.

    C++ Program to Delete an Element from an Array

    //C++ Program to Delete an Element in an array
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr[30], size, i, delElem, count = 0;
    
      cout << "Enter the size of an array (Max size: 30): ";
      cin >> size;
    
      cout << "Enter array elements:\n";
      for (i = 0; i < size; i++)
        cin >> arr[i];
    
      cout << "\nEnter element to be delete: ";
      cin >> delElem;
    
      for (i = 0; i < size; i++)
      {
        if (arr[i] == delElem)
        {
          for (int j = i; j < (size - 1); j++)
          {
            arr[j] = arr[j + 1];
          }
    
          count++;
          break;
        }
      }
    
      if (count == 0)
      {
        cout << "\nElement not found..!!\n";
      }
      else
      {
        cout << "\nElement deleted successfully..!!\n";
    
        //Display new array
        cout << "New Array after Deletion:\n";
        for (i = 0; i < (size - 1); i++)
          cout << arr[i] << " ";
      }
    
    
      return 0;
    }

    Output:

    delete array element c++

  • C++ Program to Add Two Matrices

    In this tutorial, we will write a C++ program to add two matrices using multi-dimensional arrays. To understand the coding, you should have knowledge of the following topics in C++ programming:

    The program takes the user input for the number of rows and columns then the elements for both the matrices.

    Lastly, add both the matrices and store them in a separate array and display the result. Let us go through the program.


    C++ Program to Add Two Matrices

    During addition or subtraction of matrix, it is important that both of the matrices have the same dimensions such as 2×3 matrix can be added to 2×3 matrix.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr1[50][50], arr2[50][50], sum[50][50];
      int row, col, i, j;
    
      cout << "Enter the no. of rows: ";
      cin >> row;
      cout << "Enter the no. of columns: ";
      cin >> col;
    
      // 1st matrix user input
      cout << endl << "Enter 1st Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr1[i][j];
    
      // 2nd matrix user input
      cout << endl << "Enter 2nd Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr2[i][j];
    
      // matrix adition
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          sum[i][j] = arr1[i][j] + arr2[i][j];
    
      // Displaying the result
      cout << endl << "Result of sum of the matrices: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
        {
          cout << sum[i][j] << "  ";
          if (j == col - 1)
            cout << endl;
        }
    
      return 0;
    }

    Output:

    Enter the no. of rows: 2
    Enter the no. of columns: 3

    Enter 1st Matrix elements:
    2
    2
    2
    2
    2
    2

    Enter 2nd Matrix elements:
    3
    3
    3
    3
    3
    3

    Result of sum of the matrices:
    5 5 5
    5 5 5


  • C++ Program to Make a Simple Calculator Using Switch Case Statement

    In this C++ program tutorial, we will write a code on how to make a simple calculator using switch statement in C++ or in other words create a simple calculator to add, subtract, multiply and divide using switch and break statement.

    In order to understand the program you need to know about the following topics in C++:


    C++ Program to Make a Simple Calculator Using Switch Case Statement

    # include <iostream>
    using namespace std;
    
    int main() 
    {
        char op;
        float num1, num2;
    
        cout << "Choose the operator to perform: +, -, *, / : ";
        cin >> op;
    
        cout << "\nEnter the First Number: ";
        cin >> num1;
        cout << "Enter the Second Number: ";
        cin >> num2;
    
        switch(op) 
        {
            case '+':
                cout << num1 << " + " << num2 << " = " << num1 + num2;
                break;
    
            case '-':
                cout << num1 << " - " << num2 << " = " << num1 - num2;
                break;
    
            case '*':
                cout << num1 << " * " << num2 << " = " << num1 * num2;
                break;
    
            case '/':
                cout << num1 << " / " << num2 << " = " << num1 / num2;
                break;
    
            default:
                // If chose other than the given options
                cout << "Error! Operation not supported";
                break;
        }
    
        return 0;
    }

    Output:

    Choose the operator to perform: +, -, *, / : +
    Enter the First Number: 20
    Enter the Second Number: 30
    20 + 30 = 50

    Choose the operator to perform: +, -, *, / : *
    Enter the First Number: 5
    Enter the Second Number: 12
    5 * 12 = 60

    Choose the operator to perform: +, -, *, / : &
    Enter the First Number: 12
    Enter the Second Number: 52
    Error! Operation not supported


  • C++ Program to Calculate Average Percentage Marks

    In this tutorial, you will learn to write a code on how to calculate the average percentage marks in C++. You may go through the following topics in C++:

    We will do calculate the total marks, average, and then the percentage of the subjects entered by the user. The program also takes the user input for the marks obtained in the subjects.

    How to find the average and Percentage of marks?

    Total Marks = Sum of the marks of all subjects
    Average Marks = Total Marks/No. of subjects
    Percentage = ((marks obtained)/(total marks) *100)


    C++ Program to find Total, Average and Percentage Marks

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int sub, i;
      float marks, total = 0.0 f, avg, perc;
    
      cout << "Enter the number of subjects: ";
      cin >> sub;
    
      cout << "Enter marks on each subject:\n";
      for (i = 0; i < sub; i++)
      {
        cin >> marks;
        total += marks;
      }
    
      // Average Calculation
      avg = total / sub;
    
      // Each marks is out of 100
      perc = (total / (sub *100)) *100;
    
      cout << "Total Marks Obtained: " << total;
      cout << "\nAverage Marks: " << avg;
      cout << "\nPercentage: " << perc;
    
      return 0;
    }

    Output:

    Enter the number of subjects: 5
    Enter marks on each subject:
    84
    67
    62
    80
    76
    Total Marks Obtained: 369
    Average Marks: 73.8
    Percentage: 73.8

    Here, the total marks are taken as 100 on each subject, you may take the total marks from the users too.


  • C++ Program to Calculate Product and Sum of all Elements in an Array

    In this tutorial, we will write a C++ program to find the sum and product of all elements of an array. You may go through the following topics in C++ used in the program below.

    Explanation: The program takes input for the size of an array and the elements of an array. Then the array is iterated using for loop and the sum and the product of an element are calculated. Lastly, the result is displayed.

    However, the program solves two separate questions at a time:

    • C++ program to calculate the sum of all elements in an array
    • C++ program to calculate the product of all elements in an array

    C++ Program to Calculate Product and Sum of all Elements in an Array

    Source code:

    #include<iostream>
    using namespace std;
    
    int main ()
    {
        int arr[10], size, i, sum = 0, product = 1;
        
        //user input
        cout << "Enter the size of an array: ";
        cin >> size;
        
        cout << "Enter " << size << " elements in an array: " << endl;
        for (i = 0; i < size; i++)
            cin >> arr[i];
        
        //calculation
        for (i = 0; i < size; i++)
        {
            sum += arr[i];
            product *= arr[i];
        }
        
        //Display
        cout << "\nSum of array elements: " << sum;
        cout << "\nProduct of array elements: " << product;
        return 0;
    }

    Output:

    Enter the size of an array: 5
    Enter 5 elements in an array:
    2
    3
    4
    5
    6
    Sum of array elements: 20
    Product of array elements: 720