Category: CPlusPlus Programs

c++ logo

  • C++ Program to Print Multiplication Table of any Number

    In this tutorial, we will write a C++ program to generate a multiplication table for any number and also for the given range.

    In order to understand the program, you should go through the following topic in C++.


    C++ Program to Print Multiplication Table of any Given Number

    The program will print the table of the number entered by the user up to 10. The program uses for loop to iterate and print the number.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n;
    
        cout << "Enter the number: ";
        cin >> n;
    
        for (int i = 1; i <= 10; ++i)
            cout << n << " * " << i << " = " << n*i << endl;
        
        return 0;
    }

    Output:

    Enter the number: 4
    4 * 1 = 4
    4 * 2 = 8
    4 * 3 = 12
    4 * 4 = 16
    4 * 5 = 20
    4 * 6 = 24
    4 * 7 = 28
    4 * 8 = 32
    4 * 9 = 36
    4 * 10 = 40


    C++ Program to Print Multiplication Table of any Number upto a given range

    This program will work as same as the above, the extra feature added here is the range. The above program displays the table of any number up to 10 but here it will print the table up to the user’s choice.

    The program takes user input for the number whose table needs to be displayed and also the range up to which number it should display.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num, range;
    
        cout << "Enter the number: ";
        cin >> num;
        
        cout << "Enter range: ";
        cin >> range;
    
        for (int i = 1; i <= range; ++i)
            cout << num << " * " << i << " = " << num*i << endl;
        
        return 0;
    }

    Output:

    Enter the number: 6
    Enter range: 15
    6 * 1 = 6
    6 * 2 = 12
    6 * 3 = 18
    6 * 4 = 24
    6 * 5 = 30
    6 * 6 = 36
    6 * 7 = 42
    6 * 8 = 48
    6 * 9 = 54
    6 * 10 = 60
    6 * 11 = 66
    6 * 12 = 72
    6 * 13 = 78
    6 * 14 = 84
    6 * 15 = 90


  • C++ Program to Calculate Grade of Student

    In this tutorial, we will write a C++ program to find the grades of the student. Before that, you should have knowledge of the following topics in C++.

    The program takes the user input for a number of subjects and the marks obtained on each of the subjects. The marks of the subject is taken using for loop. Then the program finds the sum of the marks and finally the average of the marks.

    Then using an else-if ladder in C++, we will find the category of the marks it belongs to. and display the grade obtained by the student.


    C++ Program to Calculate Grade of Student

    C++ Program to Calculate Grade of Student using if else statement.

    #include<iostream>
    using namespace std;
    
    int main()
    {
       int num, i;
       float marks[10], avg = 0, sum = 0;
       
       cout << "Enter number of subject: ";
       cin >> num;
       
       cout << "Enter marks for " << num << " subjects:\n";
       for (i = 0; i < num; i++)
       {
          cin >> marks[i];
       }
    
       //find the sum and average
       for (i = 0; i < num; i++)
       {
          sum += marks[i];
       }
       avg = sum / num;
       cout << "Average obtained: " << avg;
       
       cout << "\nGrade obtained: ";
       if (avg > 90)
       {
          cout << "A grade";
       }
       else if (avg < 90 && avg >= 75)
       {
          cout << "B grade";
       }
       else if (avg < 75 && avg >= 50)
       {
          cout << "C grade";
       }
       else if (avg < 50 && avg >= 30)
       {
          cout << "D grade";
       }
       else
       {
          cout << "Fail";
       }
    
       return 0;
    }

    Output:

    Enter number of subject: 5
    Enter marks for 5 subjects:
    75
    84
    29
    63
    77
    Average obtained: 78.2
    Grade obtained: B grade

    Although, the range of average for each grade is up to the programmer. You can change the average range and grade according to the program you want to create.


  • C++ Program to Calculate the Sum of Digits of a Number

    We will write the sum of digits program in C++. Before that, you should have knowledge of the topics in C++ given below.

    The program takes a user input for the integer whose sum of the digits needs to be found. Then using while loop, we will iterate the number and extract each digits and add it to the following one.

    Lastly, we will display the sum of digits of that number. Let us go through the program.


    C++ Program to Find Sum of Digits of a Number

    In this program, we will use a while loop to iterate through the number. You can also do that using for loop.

    C++ program to display the sum of the digits using a while loop.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int num, rem, sum = 0;
    
       cout << "Enter the Number: ";
       cin >> num;
    
       while (num > 0)
       {
          rem = num % 10;
          sum += rem;
          num /= 10;
       }
    
       cout << "Sum of Digits: " << sum;
    
       return 0;
    }

    Output:

    Enter the Number: 123
    Sum of Digits: 6


    C++ Program to Calculate the Sum of Digits of a Number using for loop

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int number, rem, sum = 0;
    
       cout << "Enter the Number: ";
       cin >> number;
    
       for (sum = 0; number > 0; number /= 10)
       {
          rem = number % 10;
          sum += rem;
       }
    
       cout << "Sum of Digits: " << sum;
    
       return 0;
    }

    Output: After successful execution of the above program, it will create the same output as the above one.


  • Arithmetic Mean Program in C++

    We will write a C++ Program to Calculate Arithmetic Mean. Before that, you should have knowledge of the topics in C++ given below.

    Arithmetic mean is the sum of a collection of numbers divided by the number of numbers in the collection. For example:

    arithmetic mean = (n1 + n2 +n3 + ... + nn)/n

    The program takes a number as an input from the user whose arithmetic mean is to be calculated. Then iterating that number through a for loop, we will calculate the sum of the number and then finally calculate the mean of the result of the sum of the number.

    Lastly, display the arithmetic mean of the entered number in C++.


    Arithmetic Mean Program in C++

    There is a use of an array to store the numbers entered by the user.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int num, i, arr[50], sum = 0, armean;
    
       cout << "How many number you want to enter?: ";
       cin >> num;
    
       cout << "Enter " << num << " Number:\n";
       for (i = 0; i < num; i++)
       {
          cin >> arr[i];
          sum = sum + arr[i];
       }
    
       armean = sum / num;
    
       cout << "Arithmetic Mean: " << armean;
    
       return 0;
    }

    Output:

    How many number you want to enter?: 4
    Enter 4 Number:
    2
    3
    4
    1
    Arithmetic Mean: 2

    Also, you can change the data type according to your need. It could be float, double, etc.


  • C++ Program to Check Palindrome Number

    In this tutorial, we will write a C++ program to check whether a number is Palindrome or not. Before that, you may go through the following topic in C++.

    A number is said to be a Palindrome number if it remains the same when its digits are reversed or are the same as forward. It can be applied to the word, phrase, or other sequences of symbols.

    For example: 14141, 777, 272 are palindrome numbers as they remain the same even if they are reversed. If we take the example of ‘mam’ or ‘madam’, these are also palindrome words.


    Program to Check Palindrome Number or Not in C++

    C++ program to check for palindrome numbers without using fucntion. Here the program asks the user to give the input and then checks the number by iterating it through a while loop.

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

    Output:

    Enter the Number: 232
    232 is a Palindrome Number


  • C++ Program to Check Whether a Character is Vowel or Consonant

    In this tutorial, we will write a C++ Program to Check Vowel or Consonant. To understand the example, you need to have knowledge of the following topic in C++.


    There are total 26 alphabets together out of which 5 are the vowels (a, e, i, o, u) and the rest are the consonant.

    The program below will take character input from the user to check. The program makes sure the vowel entered are the uppercase and lowercase letters then check using if..else statement.

    Let us go through the program.


    C++ Program to Check Whether a Character is Vowel or Consonant

    #include <iostream>
    using namespace std;
    
    int main()
    {
       char ch;
    
       cout << "Enter an alphabet: ";
       cin >> ch;
    
       if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
          cout << ch << " is a Vowel";
       else if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
          cout << ch << " is a Vowel";
       else
          cout << ch << " is a Consonant";
    
       return 0;
    }

    Output:

    //Run 1
    Enter an alphabet: e
    e is a Vowel

    //Run 2
    Enter an alphabet: j
    j is a Consonant


  • C++ Program to check Character is Alphabet or Not

    We will write a C++ program to check whether a character is an alphabet or not. Before that, you should have knowledge of the following in C++.


    Program to Check Alphabet or Not in C++

    First of all the program ask the user to enter a character that needed to be checked and then starts the process of checking.

    Using if-else statement if checks whether entered character is greater than or equals to a and less than or equal to z or not. Along with that, using or operator (||) another condition is checked for the uppercase alphabet that is whether entered character is greater than or equals to A and less than or equal to Z or not.

    If either of the condition is true then if the part is executed otherwise else part is executed. The program is checking for both lowercase alphabet and uppercase alphabet.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       char ch;
    
       cout << "Enter a Character: ";
       cin >> ch;
    
       if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
          cout << ch << " is an Alphabet";
       else
          cout << ch << " is NOT an Alphabet";
    
       return 0;
    }

    Output:

    //Run 1
    Enter a Character: A
    A is an Alphabet

    //Run 2
    Enter a Character: 5
    5 is NOT an Alphabet


    Using ASCII Value

    We will do the same except we will compare the character with ASCII value.

    • The ASCII values for lowercase characters ranges between 67 to 122. (‘a’ = 97 and ‘z’ = 122).
    • The ASCII values for uppercase characters ranges between 65 to 92. (‘A’ = 65 and ‘Z’ = 90).

    C++ program to check whether the entered character is alphabet or not using ASCII value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       char ch;
    
       cout << "Enter a Character: ";
       cin >> ch;
    
       if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
          cout << ch << " is an Alphabet";
       else
          cout << ch << " is NOT an Alphabet";
    
       return 0;
    }

    After executing this program, it will produce the same result as the first program mentioned above.


  • C++ Program to take Input from User

    In this simple tutorial, we will learn to get input from the user in C++. We will learn to take integer, character, string with spaces as an input. If you want to learn about the data types in C++ then go through the following tutorial.

    Take Integer and Character as an Input from User

    The program is simple, to take an input we use cin input stream present in C++. The program asks the user to enter an integer and displays that value on the screen. Then it also asks the user to enter a character and the value is again printed on the screen.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      	// integer
       int val;
       cout << "Enter the Number: ";
       cin >> val;
    
       cout << "Integer Value " << val;
    
      	// Character
       char ch;
       cout << "\n\nEnter a Character: ";
       cin >> ch;
       cout << "Character value: " << ch;
    
       return 0;
    }

    Output:

    Enter the Number: 25
    Integer Value 25

    Enter a Character: A
    Character value: A


    Get String Input with Spaces

    We can get a string with spaces from the user with the use of gets() function or getline() function. Use them in the following way in the program.

    gets(str);
    
    getline(cin, str);

    1. Take String input with Spaces using gets()

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
       char str[200];
    
       cout << "Enter the String: ";
       gets(str);
    
       cout << "String entered: " << str;
    
       return 0;
    }

    Enter the String: This is simple2code.com
    String entered: This is simple2code.com

    2. Take String Input with Spaces using getline()

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string str;
    
       cout << "Enter the String: ";
       getline(cin, str);
    
       cout << "String entered: " << str;
    
       return 0;
    }

    Enter the String: I am simple2code.com
    String entered: I am simple2code.com


  • C++ Program to Convert Octal to Hexadecimal

    In this tutorial, we will write a program to convert an octal number into hexadecimal in C++. Before that, you must have knowledge of the following topics in C++.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Octal to Hexadecimal Conversion in C++.


    C++ Program to Convert Octal to Hexadecimal

    #include <iostream>
    #include <string.h>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       char octalNumber[20];
       int arr1[20], arr2[20], rev[20];
       int h, i, j, k, l, x, fr, flag, rem, n1, n3;
       float rem1, n2, n4, dno;
    
       x = fr = flag = rem = 0;
       rem1 = 0.0;
    
       cout << "Enter an Octal number: ";
       cin >> octalNumber;
    
       for (i = 0, j = 0, k = 0; i < strlen(octalNumber); i++)
       {
          if (octalNumber[i] == '.')
             flag = 1;
          else if (flag == 0)
             arr1[j++] = octalNumber[i] - 48;
          else if (flag == 1)
             arr2[k++] = octalNumber[i] - 48;
       }
    
       x = j;
       fr = k;
    
       for (j = 0, i = x - 1; j < x; j++, i--)
       {
          rem = rem + (arr1[j] *pow(8, i));
       }
    
       for (k = 0, i = 1; k < fr; k++, i++)
       {
          rem1 = rem1 + (arr2[k] / pow(8, i));
       }
    
       rem1 = rem + rem1;
       dno = rem1;
       n1 = (int) dno;
       n2 = dno - n1;
    
       i = 0;
       while (n1 != 0)
       {
          rem = n1 % 16;
          rev[i] = rem;
          n1 = n1 / 16;
          i++;
       }
    
       j = 0;
       while (n2 != 0.0)
       {
          n2 = n2 * 16;
          n3 = (int) n2;
          n4 = n2 - n3;
          n2 = n4;
          arr1[j] = n3;
          j++;
    
          if (j == 4)
             break;
       }
    
       l = i;
       cout << "Equivalent hexadecimal value: ";
       for (i = l - 1; i >= 0; i--)
       {
          if (rev[i] == 10)
             cout << "A";
          else if (rev[i] == 11)
             cout << "B";
          else if (rev[i] == 12)
             cout << "C";
          else if (rev[i] == 13)
             cout << "D";
          else if (rev[i] == 14)
             cout << "E";
          else if (rev[i] == 15)
             cout << "F";
          else
             cout << rev[i];
       }
    }

    Output:

    Enter an Octal number: 377
    Equivalent hexadecimal value: FF

    The above program calculates the natural part of the octal number. If you also want the program the decimal octal number to decimal hexadecimal number then add the following code after the end of the last for loop on the above program.

    h = j;
    cout << ".";
    for (k = 0; k < h; k++)
    {
       if (arr1[k] == 10)
          cout << "A";
       else if (arr1[k] == 11)
          cout << "B";
       else if (arr1[k] == 12)
          cout << "C";
       else if (arr1[k] == 13)
          cout << "D";
       else if (arr1[k] == 14)
          cout << "E";
       else if (arr1[k] == 15)
          cout << "F";
       else
          cout << arr1[k];
    }

    Then you will get output something like this,

    Enter an Octal number: 137.6
    Equivalent hexadecimal value: 5F.C

    You may go through the vice-versa program:


  • C++ Program to Convert Hexadecimal to Octal

    In this tutorial, we will write a program to convert a hexadecimal number into octal in C++. Before that, you must have knowledge of the following topics in C++.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Hexadecimal to Octal Conversion in C++.


    C++ Program to Convert Hexadecimal to Octal

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       int deci = 0, oct[30], rem, i = 0, length = 0;
       char hexDec[10];
    
       cout << "Enter a Hexadecimal Number: ";
       cin >> hexDec;
    
       //find the length of the number entered
       while (hexDec[i] != '\0')
       {
          length++;
          i++;
       }
    
       length--;
    
       i = 0;
       while (length >= 0)
       {
          rem = hexDec[length];
    
          if (rem >= 48 && rem <= 57)
             rem -= 48;
          else if (rem >= 65 && rem <= 70)
             rem -= 55;
          else if (rem >= 97 && rem <= 102)
             rem -= 87;
          else
          {
             cout << "The hexadecimal number entered is invalid.";
             return 0;  //exit the program
          }
    
          deci += (rem* pow(16, i));
          length--;
          i++;
       }
    
       i = 0;
       while (deci != 0)
       {
          oct[i] = deci % 8;
          i++;
          deci /= 8;
       }
    
       //display
       cout << "Equivalent Octal Value: ";
       for (i = i - 1; i >= 0; i--)
          cout << oct[i];
    
       return 0;
    }

    Output:

    //Run 1
    Enter a Hexadecimal Number: 5F
    Equivalent Octal Value: 137

    //Run 2
    Enter a Hexadecimal Number: 2HD
    The hexadecimal number entered is invalid.

    You may go through the vice-versa program: