0% found this document useful (0 votes)
19 views

C++ Programs Questions

Uploaded by

Sajida Wajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C++ Programs Questions

Uploaded by

Sajida Wajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Programs Of C++

 (if ,if-else, else-if , nested-if, switch


statement)
 (for , while, do-while , nested-loop)
 Questions with "if" Statements:
Question 1:
Write a C++ program to check if a given number is even or odd.

Source Code:

#include <iostream>

using namespace std;

int main()

int num;

cout << "Enter a number: ";

cin >> num;

if (num % 2 == 0)

cout << num << " is even." << endl;

} else { cout << num << " is odd." << endl; }

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>

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
using namespace std;

int main()

int score;

cout << "Enter the student's score: ";

cin >> score;

if (score >= 40)

cout << "Student has passed." << endl;

else

cout << "Student has failed." << endl;

return 0;

Question 3:

Write a C++ program that determines if a given year is a leap year.

Source Code

#include <iostream>

using namespace std;

int main()

int year; cout << "Enter a year: ";

cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
{ cout << year << " is a leap year." << endl;

else

cout << year << " is not a leap year." << endl;

return 0;

Question 4:

Create a C++ program to check if a number is positive, negative, or zero.

Source Code

#include <iostream>

using namespace std;

int main()

{ int num;

cout << "Enter a number: ";

cin >> num;

if (num > 0)

cout << num << " is positive." << endl;

else if (num < 0)

cout << num << " is negative." << endl;

else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << num << " is zero." << endl;

return 0;

Question 5:

Write a C++ program that calculates the absolute value of a number.

Source Code

#include <iostream>

using namespace std;

int main()

int num;

cout << "Enter a number: ";

cin >> num;

if (num < 0) { num = -num;

cout << "The absolute value is: " << num << endl;

return 0;

Question 6:

Create a C++ program to determine the largest of three numbers.

Source Code

#include <iostream>

using namespace std;

int main() {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int largest = num1;

if (num2 > largest) {

largest = num2;

if (num3 > largest) {

largest = num3;

cout << "The largest number is: " << largest << endl;

return 0;

Question 7:

Write a C++ program to check if a character is a vowel or a consonant.

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) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
10. cout << num << " is positive." << endl;
11. } else {
12. cout << num << " is negative." << endl;
13. }
14.
15. return 0;
16. }

__________________________________________________________
 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

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
1. #include <iostream>
2. using namespace std;

3. int main() {
4. int score;
5. cout << "Enter the student's score: ";
6. cin >> score;

7. if (score >= 40) {


8. cout << "Student has passed." << endl;
9. } else {
10. cout << "Student has failed." << endl;
11. }

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;

7. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


8. cout << year << " is a leap year." << endl;
9. } else {
10. cout << year << " is not a leap year." << endl;
11. }

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
12. return 0;
13. }

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;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
7. if (num < 0) {
8. num = -num;
9. }

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;

7. int largest = num1;

8. if (num2 > largest) {


9. largest = num2;
10. }

11. if (num3 > largest) {


12. largest = num3;
13. }

14. cout << "The largest number is: " << largest << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
15. return 0;
16. }

Question 7:

Write a C++ program to check if a character is a vowel or a consonant using if-else.

Source Code

1. #include <iostream>
2. using namespace std;

3. int main() {
4. char ch;
5. cout << "Enter a character: ";
6. cin >> ch;

7. if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||


8. ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
9. cout << ch << " is a vowel." << endl;
10. } else {
11. cout << ch << " is a consonant." << endl;
12. }

13. return 0;
14. }

___________________________________________________________________________________

 Questions with " else-if" Statements:


Question 1:

Write a C++ program to determine the grade of a student based on their score using else-if
statements.

#include <iostream>

using namespace std;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int main() {

int score;

cout << "Enter the student's score: ";

cin >> score;

if (score >= 90) {

cout << "Grade: A" << endl;

} else if (score >= 80) {

cout << "Grade: B" << endl;

} else if (score >= 70) {

cout << "Grade: C" << endl;

} else if (score >= 60) {

cout << "Grade: D" << endl;

} else {

cout << "Grade: F" << endl;

return 0;

Question 2:

Create a C++ program that categorizes a number as positive, negative, or zero using else-if
statements.

#include <iostream>

using namespace std;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (num > 0) {

cout << num << " is positive." << endl;

} else if (num < 0) {

cout << num << " is negative." << endl;

} else {

cout << num << " is zero." << endl;

return 0;

Question 3:

Write a C++ program to determine if a year is a leap year, a common year, or an


exceptional common year using else-if statements.

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

cout << year << " is a leap year." << endl;

} else if (year % 100 == 0) {

cout << year << " is an exceptional common year." << endl;

} else {

cout << year << " is a common year." << endl;

return 0;

Question 4:

Create a C++ program to determine the largest of three numbers using else-if statements.

#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int largest;

if (num1 >= num2 && num1 >= num3) {

largest = num1;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
} else if (num2 >= num1 && num2 >= num3) {

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>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

if (ch >= 'A' && ch <= 'Z') {

cout << ch << " is an uppercase letter." << endl;

} else if (ch >= 'a' && ch <= 'z') {

cout << ch << " is a lowercase letter." << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
} 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>

using namespace std;

int main() {

int month;

cout << "Enter the month (1-12): ";

cin >> month;

if (month >= 1 && month <= 12) {

if (month >= 3 && month <= 5) {

cout << "Spring" << endl;

} else if (month >= 6 && month <= 8) {

cout << "Summer" << endl;

} else if (month >= 9 && month <= 11) {

cout << "Autumn" << endl;

} else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "Winter" << endl;

} else {

cout << "Invalid month." << endl;

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>

using namespace std;

int main() {

int a, b, c;

cout << "Enter the lengths of three sides of a triangle: ";

cin >> a >> b >> c;

if (a == b && b == c) {

cout << "Equilateral triangle" << endl;

} else if (a == b || b == c || a == c) {

cout << "Isosceles triangle" << endl;

} else {

cout << "Scalene triangle" << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
return 0;

_____________________________________________________________________________________

 Questions with " nested-if" Statements:


Question 1:

Create a C++ program to determine the largest of three numbers using nested if
statements.

#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

if (num1 >= num2) {

if (num1 >= num3) {

cout << "The largest number is: " << num1 << endl;

} else {

cout << "The largest number is: " << num3 << endl;

} else {

if (num2 >= num3) {

cout << "The largest number is: " << num2 << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
} else {

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>

using namespace std;

int main() {

int score;

bool absent;

cout << "Enter the student's score: ";

cin >> score;

cout << "Was the student absent? (1 for yes, 0 for no): ";

cin >> absent;

if (absent) {

cout << "Student failed due to absence." << endl;

} else {

if (score >= 40) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "Student has passed." << endl;

} else if (score >= 35) {

cout << "Student has passed but must improve." << endl;

} else {

cout << "Student has failed." << endl;

return 0;

Question 3:

Create a C++ program to check if a given year is a leap year using nested if statements.

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

cout << year << " is a leap year." << endl;

} else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << year << " is not a leap year." << endl;

} else {

cout << year << " is a leap year." << endl;

} 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>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (num > 0) {

cout << num << " is positive." << endl;

} else if (num < 0) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << num << " is negative." << endl;

} else {

cout << num << " is zero." << endl;

return 0;

Question 5:

Create a C++ program to determine the season based on the month using nested if
statements.

#include <iostream>

using namespace std;

int main() {

int month;

cout << "Enter the month (1-12): ";

cin >> month;

if (month >= 1 && month <= 12) {

if (month >= 3 && month <= 5) {

cout << "Spring" << endl;

} else if (month >= 6 && month <= 8) {

cout << "Summer" << endl;

} else if (month >= 9 && month <= 11) {

cout << "Autumn" << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
} else {

cout << "Winter" << endl;

} else {

cout << "Invalid month." << endl;

return 0;

Question 6:

Write a C++ program to check if a character is an alphabet, digit, or special character


using nested if statements.

#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." << endl;

} else if (ch >= '0' && ch <= '9') {

cout << ch << " is a digit." << endl;

} else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << ch << " is a special character." << endl;

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>

using namespace std;

int main() {

int a, b, c;

cout << "Enter the lengths of three sides of a triangle: ";

cin >> a >> b >> c;

if (a == b && b == c) {

cout << "Equilateral triangle" << endl;

} else {

if (a == b || b == c || a == c) {

cout << "Isosceles triangle" << endl;

} else {

cout << "Scalene triangle" << endl;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
return 0;

 Questions with " switch-statment":


Question 1:

Write a C++ program to determine the grade of a student based on their score using else-if
statements.

#include <iostream>

using namespace std;

int main() {

int score;

cout << "Enter the student's score: ";

cin >> score;

if (score >= 90) {

cout << "Grade: A" << endl;

} else if (score >= 80) {

cout << "Grade: B" << endl;

} else if (score >= 70) {

cout << "Grade: C" << endl;

} else if (score >= 60) {

cout << "Grade: D" << endl;

} else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "Grade: F" << endl;

return 0;

Question 2:

Create a C++ program that categorizes a number as positive, negative, or zero using else-if
statements.

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (num > 0) {

cout << num << " is positive." << endl;

} else if (num < 0) {

cout << num << " is negative." << endl;

} else {

cout << num << " is zero." << endl;

return 0;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
}

Question 3:

Write a C++ program to determine if a year is a leap year, a common year, or an


exceptional common year using else-if statements.

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

cout << year << " is a leap year." << endl;

} else if (year % 100 == 0) {

cout << year << " is an exceptional common year." << endl;

} else {

cout << year << " is a common year." << endl;

return 0;

Question 4:

Create a C++ program to determine the largest of three numbers using else-if statements.

#include <iostream>

using namespace std;


GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-
Programs)
int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int largest;

if (num1 >= num2 && num1 >= num3) {

largest = num1;

} else if (num2 >= num1 && num2 >= num3) {

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>

using namespace std;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

if (ch >= 'A' && ch <= 'Z') {

cout << ch << " is an uppercase letter." << endl;

} else if (ch >= 'a' && ch <= 'z') {

cout << ch << " is a lowercase letter." << endl;

} 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>

using namespace std;

int main() {

int month;

cout << "Enter the month (1-12): ";

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cin >> month;

if (month >= 1 && month <= 12) {

if (month >= 3 && month <= 5) {

cout << "Spring" << endl;

} else if (month >= 6 && month <= 8) {

cout << "Summer" << endl;

} else if (month >= 9 && month <= 11) {

cout << "Autumn" << endl;

} else {

cout << "Winter" << endl;

} else {

cout << "Invalid month." << endl;

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>

using namespace std;

int main() {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int a, b, c;

cout << "Enter the lengths of three sides of a triangle: ";

cin >> a >> b >> c;

if (a == b && b == c) {

cout << "Equilateral triangle" << endl;

} else if (a == b || b == c || a == c) {

cout << "Isosceles triangle" << endl;

} else {

cout << "Scalene triangle" << endl;

return 0;

 Questions with " for Loop":


Question 1:

Write a C++ program to calculate the sum of all even numbers from 1 to 50 using a "for"
loop.

#include <iostream>

using namespace std;

int main() {

int sum = 0;

for (int i = 2; i <= 50; i += 2) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
sum += i;

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>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

for (int i = 1; i <= 10; i++) {

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>

using namespace std;


GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-
Programs)
int main() {

int n;

cout << "Enter a number: ";

cin >> n;

int factorial = 1;

for (int i = 1; i <= n; i++) {

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>

using namespace std;

int main() {

int n;

cout << "Enter the number of terms in the Fibonacci series: ";

cin >> n;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int a = 0, b = 1, c;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; i++) {

cout << a << " ";

c = a + b;

a = b;

b = c;

cout << endl;

return 0;

Question 5:

Write a C++ program to find the prime numbers between 1 and 100 using a "for" loop.

#include <iostream>

using namespace std;

int main() {

cout << "Prime numbers between 1 and 100: ";

for (int num = 2; num <= 100; num++) {

bool isPrime = true;

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = false;

break;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
}

if (isPrime) {

cout << num << " ";

cout << endl;

return 0;

Question 6:

Create a C++ program to calculate the power of a number using a "for" loop.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double base, exponent, result = 1.0;

cout << "Enter the base: ";

cin >> base;

cout << "Enter the exponent: ";

cin >> exponent;

for (int i = 1; i <= exponent; i++) {

result *= base;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
}

cout << "Result: " << result << endl;

return 0;

Question 7:

Write a C++ program to count the number of digits in a given integer using a "for" loop.

#include <iostream>

using namespace std;

int main() {

int num, count = 0;

cout << "Enter an integer: ";

cin >> num;

for (; num != 0; num /= 10) {

count++;

cout << "Number of digits: " << count << endl;

return 0;

_____________________________________________________________________________________

 Questions with " while-Loop":


Question 1:

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
Create a C++ program to calculate the sum of all odd numbers from 1 to 100 using a
"while" loop.

#include <iostream>

using namespace std;

int main() {

int sum = 0;

int num = 1;

while (num <= 100) {

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>

using namespace std;

int main() {

int num = 1;

while (num <= 10) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << num << " ";

num++;

cout << endl;

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>

using namespace std;

int main() {

int num, square;

cout << "Enter a number: ";

cin >> num;

square = num * num;

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>

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
using namespace std;

int main() {

int count, total = 0;

double average;

cout << "Enter the number of values: ";

cin >> count;

int i = 0;

while (i < count) {

int value;

cout << "Enter a value: ";

cin >> value;

total += value;

i++;

average = static_cast<double>(total) / count;

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.

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
#include <iostream>

using namespace std;

int main() {

int num, originalNum, remainder, reversedNum = 0;

cout << "Enter an integer: ";

cin >> num;

originalNum = num;

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

if (originalNum == reversedNum) {

cout << "Palindrome" << endl;

} else {

cout << "Not a palindrome" << endl;

return 0;

Question 6:

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
Write a C++ program to find the GCD (Greatest Common Divisor) of two numbers using a
"while" loop.

#include <iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

while (num1 != num2) {

if (num1 > num2) {

num1 -= num2;

} else {

num2 -= num1;

cout << "GCD: " << num1 << endl;

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>

using namespace std;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int main() {

int n, sum = 0;

cout << "Enter a positive integer: ";

cin >> n;

int i = 1;

while (i <= n) {

sum += i;

i++;

cout << "Sum of natural numbers up to " << n << " is: " << sum << endl;

return 0;

_____________________________________________________________________________________

 Questions with " do-while-Loop":


Question 1:

Write a C++ program to find the sum of natural numbers from 1 to 100 using a "do-while"
loop.

#include <iostream>

using namespace std;

int main() {

int n = 1;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int sum = 0;

do {

sum += n;

n++;

} while (n <= 100);

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>

using namespace std;

int main() {

int n;

cout << "Enter a number: ";

cin >> n;

int factorial = 1;

int i = 1;

do {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
factorial *= i;

i++;

} while (i <= n);

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>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

int i = 1;

do {

cout << num * i << " ";

i++;

} while (i <= 10);

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << endl;

return 0;

Question 4:

Create a C++ program to check if a number is a palindrome or not using a "do-while"


loop.

#include <iostream>

using namespace std;

int main() {

int num, originalNum, remainder, reversedNum = 0;

cout << "Enter an integer: ";

cin >> num;

originalNum = num;

do {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

} while (num != 0);

if (originalNum == reversedNum) {

cout << "Palindrome" << endl;

} else {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "Not a palindrome" << endl;

return 0;

Question 5:

Write a C++ program to display the first 10 natural numbers using a "do-while" loop.

#include <iostream>

using namespace std;

int main() {

int n = 1;

do {

cout << n << " ";

n++;

} while (n <= 10);

cout << endl;

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>

using namespace std;


GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-
Programs)
int main() {

int num, square;

cout << "Enter a number: ";

cin >> num;

square = num * num;

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>

using namespace std;

int main() {

int n = 2;

int sum = 0;

do {

sum += n;

n += 2;

} while (n <= 100);

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "Sum of even numbers from 2 to 100: " << sum << endl;

return 0;

_____________________________________________________________________________________

 Questions with " nested-loop-Loop":


Question 1:

Create a C++ program to display a pattern of asterisks in a right-angled triangle using


nested loops.

#include <iostream>

using namespace std;

int main() {

int rows;

cout << "Enter the number of rows: ";

cin >> rows;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

cout << "* ";

cout << endl;

return 0;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
}

Question 2:

Write a C++ program to display a pattern of numbers in a right-angled triangle using


nested loops.

#include <iostream>

using namespace std;

int main() {

int rows;

cout << "Enter the number of rows: ";

cin >> rows;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

cout << j << " ";

cout << endl;

return 0;

Question 3:

Create a C++ program to display a pattern of alphabets in an inverted right-angled


triangle using nested loops.

#include <iostream>

using namespace std;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int main() {

int n;

cout << "Enter the number of rows: ";

cin >> n;

char ch = 'A';

for (int i = n; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

cout << ch << " ";

ch++;

cout << endl;

return 0;

Question 4:

Write a C++ program to display a pattern of numbers in an inverted right-angled triangle


using nested loops.

#include <iostream>

using namespace std;

int main() {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
int rows;

cout << "Enter the number of rows: ";

cin >> rows;

for (int i = rows; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

cout << j << " ";

cout << endl;

return 0;

Question 5:

Create a C++ program to display a pattern of asterisks in a diamond shape using nested
loops.

#include <iostream>

using namespace std;

int main() {

int n, spaces;

cout << "Enter the number of rows: ";

cin >> n;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
spaces = n - 1;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= spaces; j++) {

cout << " ";

spaces--;

for (int j = 1; j <= 2 * i - 1; j++) {

cout << "*";

cout << endl;

spaces = 1;

for (int i = 1; i <= n - 1; i++) {

for (int j = 1; j <= spaces; j++) {

cout << " ";

spaces++;

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
for (int j = 1; j <= 2 * (n - i) - 1; j++) {

cout << "*";

cout << endl;

return 0;

Question 6:

Write a C++ program to display a pattern of numbers in a pyramid shape using nested
loops.

#include <iostream>

using namespace std;

int main() {

int n, num = 1;

cout << "Enter the number of rows: ";

cin >> n;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n - i; j++) {

cout << " ";

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
for (int j = 1; j <= i; j++) {

cout << num << " ";

num++;

cout << endl;

return 0;

Question 7:

Create a C++ program to display a pattern of asterisks in a hollow square shape using
nested loops.

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the number of rows/columns: ";

cin >> n;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++) {

if (i == 1 || i == n || j == 1 || j == n) {

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)
cout << "*";

} else {

cout << " ";

cout << endl;

return 0;

_____________________________________________________________________________________

GHS Chitta Batta ,Developed by:(SA) C++ Programs ToTal:(63-


Programs)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy