C++ Basic Interview Programs practice Quiz

C++ Program to Check Armstrong Number

This is a C++ Program to Check if a Number is an Armstrong Number.

Problem Description

The program takes a number and checks if it is an Armstrong number. An n-digit number such that the sum of each digit raised to the power n is the number itself, is an Armstrong number.

Problem Solution

1. The number to be checked is entered.
2. Each digit is cubed, and added.
3. If the eventual sum of the cubed digits is equal to the number entered, then it is an Armstrong number.
4. Else it is not.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is an Armstrong Number. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. int num, temp, rem, sum = 0;
  6. cout << “Enter number to be checked : “;
  7. cin >> num;
  8. temp = num;
  9. while (temp != 0)
  10. {
  11. rem = temp % 10;
  12. sum = sum + rem*rem*rem;
  13. temp = temp / 10;
  14. }
  15. if (sum == num)
  16. cout << “\n” << num << ” is an Armstrong number.”;
  17. else
  18. cout << “\n” << num << ” is not an Armstrong number.”;
  19. return 0;
  20. }

Program Explanation

1. The user is asked to enter the number to be checked and it is stored in the variable ‘num’.
2. The variable sum is initialized as 0.
3. The value of num is assigned to a temporary variable temp and it is checked.
4. Each digit is cubed and added to the variable sum using a while loop and modulus operator.
5. If the value of sum is equal to the value of the number entered, i.e num, then it is an Armstrong number.
6. Else the entered number is not an Armstrong number.
7. The result is then printed.

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

This is a C++ Program to Check if a Character is a Vowel, Consonant or Digit.

Problem Description

The program takes a character and checks if it is a vowel, consonant or a digit.

Problem Solution

1. A character is entered.
2. Using nested if else condition, the character is checked if it is a vowel, consonant or a digit.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Character is a Vowel, Consonant or Digit. The program output is shown below.

  1. #include<iostream>
  2. #include<ctype.h>
  3. using namespace std;
  4. int main ()
  5. {
  6. char ch;
  7. cout << “Enter a character : “;
  8. cin >> ch;
  9. if (isdigit(ch))
  10. cout << “\nThe entered character is a digit.”;
  11. else if (isalpha(ch))
  12. {
  13. if ((ch == ‘A’) || (ch == ‘E’)|| (ch == ‘I’)|| (ch == ‘O’)|| (ch == ‘U’)|| (ch == ‘a’)|| (ch == ‘e’)||
  14. (ch == ‘i’)|| (ch == ‘o’)|| (ch == ‘u’))
  15. cout << “\nThe entered character is a vowel.”;
  16. else
  17. cout << “\nThe entered character is a consonant.”;
  18. }
  19. else
  20. cout << “\nThe entered character is a special character.”;
  21. return 0;
  22. }

Program Explanation

1. The user is asked to enter a character and is stored in the variable ‘char’.
2. The character is checked if it is a vowel, consonant or digit using functions under the library ctype.h.
3. If it is neither of the three, then the entered character is a special character,
4. The result is then printed.

Runtime Test Cases

C++ Program to Print Pascal Triangle

This C++ Program which prints pascal’s triangle. The program takes number of rows as input and uses nested loops to print pascal’s triangle. The first inner loop creates the indentation space and the second inner loop computes the value of binomial coefficient, creates indentation space and prints the binomial coefficient for that particular column.

Here is source code of the C++ program which prints pascal’s triangle. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

$ g++ main.cpp
$ ./a.out
Enter the number of rows : 5
 
                  1
 
               1      1
 
            1      2      1
 
         1      3      3      1
 
      1      4      6      4      1
  1. /*
  2.  * C++ Program to Print Pascal’s Triangle
  3.  */
  4.  
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int rows;
  11. cout << “Enter the number of rows : “;
  12. cin >> rows;
  13. cout << endl;
  14.  
  15. for (int i = 0; i < rows; i++)
  16. {
  17. int val = 1;
  18. for (int j = 1; j < (rows – i); j++)
  19. {
  20. cout << ” “;
  21. }
  22. for (int k = 0; k <= i; k++)
  23. {
  24. cout << ” ” << val;
  25. val = val * (i – k) / (k + 1);
  26. }
  27. cout << endl << endl;
  28. }
  29. cout << endl;
  30. return 0;
  31. }

About Author


After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

Leave a Comment