1a using for loop
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i; }
Using while
int sum = 0, i = 1;
while (i <= 100) {
sum += i;
i++; }
using do while loop
int sum = 0, i = 1;
do {
sum += i;
i++; }
while (i <= 100);
B using for loop
int sum = 0;
for (int i = 5; i <= 50; i += 5) {
sum += i; }
Using while loop
int sum = 0, i = 5;
while (i <= 50) {
sum += i;
i += 5; }
Using do while loop
int sum = 0, i = 5;
do {
sum += i;
i += 5;
} while (i <= 50);
C using for loop
double sum = 0.0;
for (int i = 1; i <= 15; i++) {
sum += 1.0 / i; }
Using while loop
double sum = 0.0;
int i = 1;
while (i <= 15) {
sum += 1.0 / i;
i++; }
Using do while loop
double sum = 0.0;
int i = 1;
do {
sum += 1.0 / i;
i++;
} while (i <= 15);
D using for loop
unsigned long long product = 1;
for (int i = 1; i <= 20; i++) {
product *= i; }
using while loop
unsigned long long product = 1;
int i = 1;
while (i <= 20) {
product *= i;
i++; }
using do while loop
unsigned long long product = 1;
int i = 1;
do {
product *= i;
i++;
} while (i <= 20);
2 #include <iostream>
using namespace std;
int main() {
for (int i = 10; i <= 49; i++) {
cout << i << " ";
if ((i + 1) % 10 == 0) // After every 10 numbers, move to the next line
cout << endl; }
return 0;
3 #include <iostream>
using namespace std;
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false; }
return true;
int main() {
cout << "Prime numbers between 1 and 100 are:" << endl;
for (int i = 1; i <= 100; i++) {
if (isPrime(i))
cout << i << " "; }
cout << endl;
return 0;
4 #include <iostream>
using namespace std;
int main() {
int number;
int count = 0;
cout << "Enter an integer: ";
cin >> number;
// Handle negative numbers
if (number < 0)
number = -number;
// Special case for 0
if (number == 0)
count = 1;
else {
while (number != 0) {
number /= 10;
count++; }
cout << "Number of digits: " << count << endl;
return 0;
5 #include <iostream>
using namespace std;
// Function to compute letter grade
char computeGrade(int total) {
if (total >= 90) return 'A';
else if (total >= 80) return 'B';
else if (total >= 70) return 'C';
else if (total >= 60) return 'D';
else return 'F';
}
int main() {
int mid, finalExam;
char choice;
do {
// Input and validation
cout << "Enter midterm mark (0 - 40): ";
cin >> mid;
cout << "Enter final exam mark (0 - 60): ";
cin >> finalExam;
if (mid < 0 || mid > 40 || finalExam < 0 || finalExam > 60) {
cout << "Error: Marks should be in the specified range (mid: 0–40, final: 0–60)." << endl;
} else {
int total = mid + finalExam;
char grade = computeGrade(total);
cout << "Total: " << total << " | Grade: " << grade << endl;
cout << "Do you want to enter another record? (Y/N): ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
cout << "Program ended." << endl;
return 0;
6 #include <iostream>
using namespace std;
int main() {
int number;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> number;
if (number < 0) {
cout << "Error: Please enter a positive number." << endl;
} else {
for (int i = 1; i <= number; ++i) {
factorial *= i;
cout << "Factorial of " << number << " is: " << factorial << endl;
return 0;
7 #include <iostream>
using namespace std;
int main() {
int n;
unsigned long long factorial = 1, sum = 0;
cout << "Enter a positive integer n: ";
cin >> n;
if (n < 1) {
cout << "Please enter a number greater than 0." << endl;
return 1;
for (int i = 1; i <= n; i++) {
factorial *= i; // Compute i!
sum += factorial; // Add i! to sum
cout << "Sum of series 1! + 2! + ... + " << n << "! is: " << sum << endl;
return 0;
8 #include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) { // Outer loop: 1 to 5 lines
for (int j = 65; j < 65 + i; j++) { // Inner loop: ASCII 'A' (65) to 65 + i - 1
cout << char(j);
cout << endl; // New line after each row
return 0;
9 #include <iostream>
using namespace std;
int main() {
char ch = 'a'; // ASCII value 97
for (int i = 1; i <= 6; i++) { // 6 lines
for (int j = 1; j <= i; j++) { // i characters in the ith line
cout << ch;
ch++; // move to next letter }
cout << endl; // new line }
return 0; }
10 #include <iostream>
using namespace std;
int main() {
// A
cout << "A.\n";
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= i; ++j) {
cout << "*";
cout << "\n";
// B
cout << "\nB.\n";
for (int i = 5; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
cout << "*";
cout << "\n";
// C
cout << "\nC.\n";
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5 - i; ++j) {
cout << " ";
for (int k = 1; k <= (2 * i - 1); ++k) {
cout << "*"; }
cout << "\n";
// D
cout << "\nD.\n";
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 3 - i; ++j) {
cout << " ";
for (int k = 1; k <= (2 * i - 1); ++k) {
cout << "*";
cout << "\n";
for (int i = 2; i >= 1; --i) {
for (int j = 1; j <= 3 - i; ++j) {
cout << " ";
for (int k = 1; k <= (2 * i - 1); ++k) {
cout << "*";
cout << "\n";
return 0;
11 #include <iostream>
using namespace std;
int main() {
int day = 1;
double temperature, total = 0.0;
cout << "Enter the temperatures for the past 10 days:\n";
while (day <= 10) {
cout << "Day " << day << ": ";
cin >> temperature;
total += temperature;
day++; }
double average = total / 10.0;
cout << "\nAverage temperature over 10 days: " << average << " degrees\n";
return 0;
12 #include <iostream>
#include <cmath> // for pow function
using namespace std;
int main() {
long long sum = 0;
for (int i = 1; i <= 30; ++i) {
sum += 2 * pow(3, i); }
cout << "The result of the summation ∑ (2 * 3^i) from i = 1 to 30 is: " << sum << endl;
return 0;
13 #include <iostream>
using namespace std;
int main() {
const int numStudents = 5;
double marks, total = 0.0;
for (int i = 1; i <= numStudents; ++i) {
do {
cout << "Enter marks for student " << i << " (0 to 100): ";
cin >> marks;
if (marks < 0 || marks > 100) {
cout << "Invalid mark. Please enter a value between 0 and 100.\n";
} while (marks < 0 || marks > 100);
total += marks;
double average = total / numStudents;
cout << "\nAverage marks of the five students: " << average << endl;
return 0; }
14 #include <iostream>
using namespace std;
int main() {
double num1, num2;
char op;
cout << "Enter an expression (e.g. 15 * 20): ";
cin >> num1 >> op >> num2;
switch (op) {
case '+':
cout << num1 << " + " << num2 << " equals " << (num1 + num2) << endl;
break;
case '-':
cout << num1 << " - " << num2 << " equals " << (num1 - num2) << endl;
break;
case '*':
cout << num1 << " * " << num2 << " equals " << (num1 * num2) << endl;
break;
case '/':
if (num2 == 0) {
cout << num1 << " / " << num2 << " can’t be computed: denominator is 0." << endl;
} else {
cout << num1 << " / " << num2 << " equals " << (num1 / num2) << endl;
break;
default:
cout << op << " is unrecognized operator" << endl;
return 0;
15 #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a single alphabet character: ";
cin >> ch;
// Check if the character is a letter
if (!isalpha(ch)) {
cout << "Invalid input. Please enter an alphabet letter.\n";
} else {
// Convert to lowercase to simplify checks
char lowerCh = tolower(ch);
// Check for vowel
if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u') {
cout << ch << " is a vowel.\n";
} else {
cout << ch << " is a consonant.\n" }
return 0; }
16 #include <iostream>
using namespace std;
int main() {
cout << "Even numbers between 0 and 20:\n";
for (int i = 0; i <= 20; i++) {
if (i % 2 == 0) {
cout << i << " "; }
cout << endl;
return 0;
17 #include <iostream>
using namespace std;
// Function to check if a year is a leap year
bool isLeapYear(int year) {
if (year % 4 != 0)
return false;
else if (year % 100 == 0 && year % 400 != 0)
return false;
else
return true;
// Function to get the number of days in a month
int getDaysInMonth(int month, int year) {
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 0; }
int main() {
int day, month, year;
cout << "Please enter a date (dd mm yyyy): ";
cin >> day >> month >> year;
if (month < 1 || month > 12) {
cout << "Invalid month: " << month << endl;
} else {
int maxDays = getDaysInMonth(month, year);
if (day < 1 || day > maxDays) {
cout << "Invalid day of month " << day << endl;
} else {
cout << day << "/" << month << "/" << year << " is a valid date" << endl;
return 0;