C++ Programs Questions
C++ Programs Questions
Source Code:
#include <iostream>
int main()
int num;
if (num % 2 == 0)
return 0;
Question 2:
Create a C++ program that checks if a student has passed or failed an exam. Pass the student if
their score is greater than or equal to 40.
Source Code:
#include <iostream>
int main()
int score;
else
return 0;
Question 3:
Source Code
#include <iostream>
int main()
else
cout << year << " is not a leap year." << endl;
return 0;
Question 4:
Source Code
#include <iostream>
int main()
{ int num;
if (num > 0)
else {
return 0;
Question 5:
Source Code
#include <iostream>
int main()
int num;
cout << "The absolute value is: " << num << endl;
return 0;
Question 6:
Source Code
#include <iostream>
int main() {
largest = num2;
largest = num3;
cout << "The largest number is: " << largest << endl;
return 0;
Question 7:
Source Code
1. #include <iostream>
2. using namespace std;
3.
4. int main() {
5. int num;
6. cout << "Enter a number: ";
7. cin >> num;
8.
9. if (num > 0) {
__________________________________________________________
Questions with "if-else" Statements:
Question 1:
Write a C++ program to determine if a given number is positive or negative, using if-else
statements.
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int num;
5. cout << "Enter a number: ";
6. cin >> num;
7. if (num > 0) {
8. cout << num << " is positive." << endl;
9. } else {
10. cout << num << " is negative." << endl;
11. }
12. return 0;
13. }
Question 2:
Create a C++ program that checks if a student has passed or failed an exam using if-else.
Pass the student if their score is greater than or equal to 40.
Source Code
3. int main() {
4. int score;
5. cout << "Enter the student's score: ";
6. cin >> score;
12. return 0;
13. }
Question 3:
Write a C++ program that determines if a given year is a leap year using if-else.
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int year;
5. cout << "Enter a year: ";
6. cin >> year;
Question 4:
Create a C++ program to check if a number is positive, negative, or zero using if-else.
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int num;
5. cout << "Enter a number: ";
6. cin >> num;
7. if (num > 0) {
8. cout << num << " is positive." << endl;
9. } else if (num < 0) {
10. cout << num << " is negative." << endl;
11. } else {
12. cout << num << " is zero." << endl;
13. }
14. return 0;
15. }
Question 5:
Write a C++ program that calculates the absolute value of a number using if-else.
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int num;
5. cout << "Enter a number: ";
6. cin >> num;
10. cout << "The absolute value is: " << num << endl;
11. return 0;
12. }
Question 6:
Create a C++ program to determine the largest of three numbers using if-else.
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int num1, num2, num3;
5. cout << "Enter three numbers: ";
6. cin >> num1 >> num2 >> num3;
14. cout << "The largest number is: " << largest << endl;
Question 7:
Source Code
1. #include <iostream>
2. using namespace std;
3. int main() {
4. char ch;
5. cout << "Enter a character: ";
6. cin >> ch;
13. return 0;
14. }
___________________________________________________________________________________
Write a C++ program to determine the grade of a student based on their score using else-if
statements.
#include <iostream>
int score;
} else {
return 0;
Question 2:
Create a C++ program that categorizes a number as positive, negative, or zero using else-if
statements.
#include <iostream>
int num;
if (num > 0) {
} else {
return 0;
Question 3:
#include <iostream>
int main() {
int year;
cout << year << " is an exceptional common year." << endl;
} else {
return 0;
Question 4:
Create a C++ program to determine the largest of three numbers using else-if statements.
#include <iostream>
int main() {
int largest;
largest = num1;
largest = num2;
} else {
largest = num3;
cout << "The largest number is: " << largest << endl;
return 0;
Question 5:
Write a C++ program that classifies a given character as uppercase, lowercase, or neither
using else-if statements.
#include <iostream>
int main() {
char ch;
cout << ch << " is neither uppercase nor lowercase." << endl;
return 0;
Question 6:
Create a C++ program that determines the season based on the month using else-if
statements.
#include <iostream>
int main() {
int month;
} else {
} else {
return 0;
Question 7:
Write a C++ program to determine the type of a triangle based on its sides using else-if
statements.
#include <iostream>
int main() {
int a, b, c;
if (a == b && b == c) {
} else if (a == b || b == c || a == c) {
} else {
_____________________________________________________________________________________
Create a C++ program to determine the largest of three numbers using nested if
statements.
#include <iostream>
int main() {
cout << "The largest number is: " << num1 << endl;
} else {
cout << "The largest number is: " << num3 << endl;
} else {
cout << "The largest number is: " << num2 << endl;
cout << "The largest number is: " << num3 << endl;
return 0;
Question 2:
Write a C++ program to determine if a student has passed or failed an exam based on
multiple conditions using nested if statements.
#include <iostream>
int main() {
int score;
bool absent;
cout << "Was the student absent? (1 for yes, 0 for no): ";
if (absent) {
} else {
cout << "Student has passed but must improve." << endl;
} else {
return 0;
Question 3:
Create a C++ program to check if a given year is a leap year using nested if statements.
#include <iostream>
int main() {
int year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
} else {
} else {
} else {
cout << year << " is not a leap year." << endl;
return 0;
Question 4:
Write a C++ program to determine if a number is positive, negative, or zero using nested if
statements.
#include <iostream>
int main() {
int num;
if (num > 0) {
} else {
return 0;
Question 5:
Create a C++ program to determine the season based on the month using nested if
statements.
#include <iostream>
int main() {
int month;
} else {
return 0;
Question 6:
#include <iostream>
int main() {
char ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
} else {
return 0;
Question 7:
Create a C++ program that determines the type of a triangle based on its sides using nested
if statements.
#include <iostream>
int main() {
int a, b, c;
if (a == b && b == c) {
} else {
if (a == b || b == c || a == c) {
} else {
Write a C++ program to determine the grade of a student based on their score using else-if
statements.
#include <iostream>
int main() {
int score;
} else {
return 0;
Question 2:
Create a C++ program that categorizes a number as positive, negative, or zero using else-if
statements.
#include <iostream>
int main() {
int num;
if (num > 0) {
} else {
return 0;
Question 3:
#include <iostream>
int main() {
int year;
cout << year << " is an exceptional common year." << endl;
} else {
return 0;
Question 4:
Create a C++ program to determine the largest of three numbers using else-if statements.
#include <iostream>
int largest;
largest = num1;
largest = num2;
} else {
largest = num3;
cout << "The largest number is: " << largest << endl;
return 0;
Question 5:
Write a C++ program that classifies a given character as uppercase, lowercase, or neither
using else-if statements.
#include <iostream>
char ch;
} else {
cout << ch << " is neither uppercase nor lowercase." << endl;
return 0;
Question 6:
Create a C++ program that determines the season based on the month using else-if
statements.
#include <iostream>
int main() {
int month;
} else {
} else {
return 0;
Question 7:
Write a C++ program to determine the type of a triangle based on its sides using else-if
statements.
#include <iostream>
int main() {
if (a == b && b == c) {
} else if (a == b || b == c || a == c) {
} else {
return 0;
Write a C++ program to calculate the sum of all even numbers from 1 to 50 using a "for"
loop.
#include <iostream>
int main() {
int sum = 0;
cout << "Sum of even numbers from 1 to 50: " << sum << endl;
return 0;
Question 2:
Create a C++ program to display the multiplication table for a given number (e.g., 5) using
a "for" loop.
#include <iostream>
int main() {
int num;
cout << num << " x " << i << " = " << (num * i) << endl;
return 0;
Question 3:
Write a C++ program to calculate the factorial of a number using a "for" loop.
#include <iostream>
int n;
cin >> n;
int factorial = 1;
factorial *= i;
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
Question 4:
Create a C++ program to print the Fibonacci series up to a specified number of terms
using a "for" loop.
#include <iostream>
int main() {
int n;
cout << "Enter the number of terms in the Fibonacci series: ";
cin >> n;
c = a + b;
a = b;
b = c;
return 0;
Question 5:
Write a C++ program to find the prime numbers between 1 and 100 using a "for" loop.
#include <iostream>
int main() {
if (num % i == 0) {
isPrime = false;
break;
if (isPrime) {
return 0;
Question 6:
Create a C++ program to calculate the power of a number using a "for" loop.
#include <iostream>
#include <cmath>
int main() {
result *= base;
return 0;
Question 7:
Write a C++ program to count the number of digits in a given integer using a "for" loop.
#include <iostream>
int main() {
count++;
return 0;
_____________________________________________________________________________________
#include <iostream>
int main() {
int sum = 0;
int num = 1;
sum += num;
num += 2;
cout << "Sum of odd numbers from 1 to 100: " << sum << endl;
return 0;
Question 2:
Write a C++ program to display the first 10 natural numbers using a "while" loop.
#include <iostream>
int main() {
int num = 1;
num++;
return 0;
Question 3:
Create a C++ program to find the square of a number entered by the user using a "while"
loop.
#include <iostream>
int main() {
cout << "Square of " << num << " is: " << square << endl;
return 0;
Question 4:
Write a C++ program to calculate the average of a set of numbers entered by the user using
a "while" loop.
#include <iostream>
int main() {
double average;
int i = 0;
int value;
total += value;
i++;
cout << "Average of " << count << " values is: " << average << endl;
return 0;
Question 5:
Create a C++ program to check if a number is a palindrome or not using a "while" loop.
int main() {
originalNum = num;
while (num != 0) {
num /= 10;
if (originalNum == reversedNum) {
} else {
return 0;
Question 6:
#include <iostream>
int main() {
num1 -= num2;
} else {
num2 -= num1;
return 0;
Question 7:
Create a C++ program to calculate the sum of natural numbers up to a specified number
using a "while" loop.
#include <iostream>
int n, sum = 0;
cin >> n;
int i = 1;
while (i <= n) {
sum += i;
i++;
cout << "Sum of natural numbers up to " << n << " is: " << sum << endl;
return 0;
_____________________________________________________________________________________
Write a C++ program to find the sum of natural numbers from 1 to 100 using a "do-while"
loop.
#include <iostream>
int main() {
int n = 1;
do {
sum += n;
n++;
cout << "Sum of natural numbers from 1 to 100: " << sum << endl;
return 0;
Question 2:
Create a C++ program to calculate the factorial of a number entered by the user using a
"do-while" loop.
#include <iostream>
int main() {
int n;
cin >> n;
int factorial = 1;
int i = 1;
do {
i++;
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
Question 3:
Write a C++ program to find the first 10 multiples of a number entered by the user using a
"do-while" loop.
#include <iostream>
int main() {
int num;
int i = 1;
do {
i++;
return 0;
Question 4:
#include <iostream>
int main() {
originalNum = num;
do {
num /= 10;
if (originalNum == reversedNum) {
} else {
return 0;
Question 5:
Write a C++ program to display the first 10 natural numbers using a "do-while" loop.
#include <iostream>
int main() {
int n = 1;
do {
n++;
return 0;
Question 6:
Create a C++ program to find the square of a number entered by the user using a "do-
while" loop.
#include <iostream>
cout << "Square of " << num << " is: " << square << endl;
return 0;
Question 7:
Write a C++ program to calculate the sum of even numbers from 2 to 100 using a "do-
while" loop.
#include <iostream>
int main() {
int n = 2;
int sum = 0;
do {
sum += n;
n += 2;
return 0;
_____________________________________________________________________________________
#include <iostream>
int main() {
int rows;
return 0;
Question 2:
#include <iostream>
int main() {
int rows;
return 0;
Question 3:
#include <iostream>
int n;
cin >> n;
char ch = 'A';
ch++;
return 0;
Question 4:
#include <iostream>
int main() {
return 0;
Question 5:
Create a C++ program to display a pattern of asterisks in a diamond shape using nested
loops.
#include <iostream>
int main() {
int n, spaces;
cin >> n;
spaces--;
spaces = 1;
spaces++;
return 0;
Question 6:
Write a C++ program to display a pattern of numbers in a pyramid shape using nested
loops.
#include <iostream>
int main() {
int n, num = 1;
cin >> n;
num++;
return 0;
Question 7:
Create a C++ program to display a pattern of asterisks in a hollow square shape using
nested loops.
#include <iostream>
int main() {
int n;
cin >> n;
if (i == 1 || i == n || j == 1 || j == n) {
} else {
return 0;
_____________________________________________________________________________________