Important 30 Basic Algorithm Questions in C++


Q 1. Write a program that takes two integers and returns their sum.

Input: 5, 10

Expected Output: 15

Q 2. Write a program to check if a number is even or odd.

Input: 7

Expected Output: Odd

Q 3. Write a program to find the maximum of two numbers.

Input: 15, 25

Expected Output: 25

Q 4. Write a program to find the sum of the first n natural numbers.

Input: 5

Expected Output: 15

Q 5. Write a program to calculate the factorial of a number.

Input: 4

Expected Output: 24

Q 6. Write a program to check whether a number is prime.

Input: 29

Expected Output: Prime

Q 7. Write a program to find the largest of three numbers.

Input: 12, 45, 23

Expected Output: 45

Q 8. Write a program to reverse a number.

Input: 1234

Expected Output: 4321

Q 9. Write a program to check if a number is a palindrome.

Input: 121

Expected Output: Palindrome

Q 10. Write a program to print the first n Fibonacci numbers.

Input: 5

Expected Output: 0 1 1 2 3

Q 11. Write a program to find the GCD (Greatest Common Divisor) of two numbers.

Input: 54, 24

Expected Output: 6

Q 12. Write a program to count the number of digits in a number.

Input: 12345

Expected Output: 5

Q 13. Write a program to check if a number is an Armstrong number.

Input: 153

Expected Output: Armstrong

Q 14. Write a program to find the LCM (Least Common Multiple) of two numbers.

Input: 12, 18

Expected Output: 36

Q 15. Write a program to calculate the sum of digits of a number.

Input: 123

Expected Output: 6

Q 16. Write a program to swap two numbers using a temporary variable.

Input: 5, 10

Expected Output: 10 5

Q 17. Write a program to swap two numbers without using a temporary variable.

Input: 3, 7

Expected Output: 7 3

Q 18. Write a program to find x raised to the power of y.

Input: 2, 3

Expected Output: 8

Q 19. Write a program to check if a given year is a leap year.

Input: 2020

Expected Output: Leap Year

Q 20. Write a program to find the square root of a number.

Input: 16

Expected Output: 4

Q 21. Write a program to check if a number is a perfect number. A perfect number is one that is equal to the sum of its divisors (excluding itself).

Input: 6

Expected Output: Perfect Number

Q 22. Write a program to generate a multiplication table for a given number.

Input: 5

Expected Output:

5 * 1 = 5

5 * 2 = 10

...

5 * 10 = 50

Q 23. Write a program to find the sum of all even numbers from 1 to n.

Input: 10

Expected Output: 30

Q 24. Write a program to count the number of vowels in a string.

Input: "Hello"

Expected Output: 2

Q 25. Write a program to reverse a string.

Input: "World"

Expected Output: "dlroW"

Q 26. Write a program to check if a string is a palindrome.

Input: "madam"

Expected Output: Palindrome

Q 27. Write a program to find the largest element in an array.

Input: {10, 20, 5, 35, 2}

Expected Output: 35

Q 28. Write a program to calculate the sum of all elements in an array.

Input: {10, 20, 30}

Expected Output: 60

Q 29. Write a program to find the second largest element in an array.

Input: {10, 20, 35, 5}

Expected Output: 20

Q 30. Write a program to find the length of a string without using built-in functions.

Input: "Programming"

Expected Output: 11

Social Share

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!