Answer Sheet
Answer Sheet
Steps:
Start
Input number
Check if the number is divisible by 2 (number % 2 == 0)
o Yes → Display "Even"
o No → Display "Odd"
End
2) Obtain two numbers from the keyboard, and determine and display which (if
either) is the larger of the two numbers.
Steps:
Start
Input first number (num1)
Input second number (num2)
Check if num1 > num2
o Yes → Display num1 is larger
o No → Check if num2 > num1
Yes → Display num2 is larger
No → Display "Both are equal"
End
Steps:
Start
Input first number (num1)
Input second number (num2)
Input third number (num3)
Compare all three numbers to arrange in ascending order (use sorting or comparisons)
Display numbers in ascending order
End
4) Add the numbers from 1 to 100 and display the sum.
Steps:
Start
Initialize sum to 0
Loop through numbers from 1 to 100
o Add each number to sum
Display sum
End
5) Add the even numbers between 0 and any positive integer number given by
the user.
Steps:
Start
Input a positive integer (num)
Initialize sum to 0
Loop through numbers from 0 to num
o If the number is even, add it to sum
Display the sum
End
Steps:
Start
Input first number (num1)
Input second number (num2)
Calculate average: (num1 + num2) / 2
Display the average
End
7) Find the average, maximum, minimum, and sum of three numbers given by
the user.
Steps:
Start
Input first number (num1)
Input second number (num2)
Input third number (num3)
Calculate sum: num1 + num2 + num3
Find maximum of the three numbers
Find minimum of the three numbers
Calculate average: (num1 + num2 + num3) / 3
Display sum, average, maximum, and minimum
End
8) Find the area of a circle where the radius is provided by the user.
Steps:
Start
Input radius (r)
Calculate area: π * r^2 (use 3.14 for π)
Display the area
End
Steps:
Start
Input first number (num1)
Input second number (num2)
Use a third variable (temp)
o Set temp = num1
o Set num1 = num2
o Set num2 = temp
Display num1 and num2 after swapping
End
10) Swap the content of two variables without using a third variable.
Steps:
Start
Input first number (num1)
Input second number (num2)
Swap the values using arithmetic:
o Set num1 = num1 + num2
o Set num2 = num1 - num2
o Set num1 = num1 - num2
Display num1 and num2 after swapping
End
11) Read an integer value from the keyboard and display a message indicating if
this number is odd or even.
Steps:
Start
Input number (num)
Check if num % 2 == 0
o Yes → Display "Even"
o No → Display "Odd"
End
12) Read 10 integers from the keyboard in the range 0 - 100, and count how
many of them are larger than 50, and display this result.
Steps:
Start
Initialize counter to 0
Loop 10 times to input 10 integers
o If the number is greater than 50, increase the counter
Display the counter value
End
13) Take an integer from the user and display the factorial of that number.
Steps:
Start
Input integer (n)
Initialize result to 1
Loop from 1 to n
o Multiply result by the loop index (i)
Display the result (factorial)
End
Worksheet 2
1) Receive a number and determine whether it is odd or even.
#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;
}
2. Obtain two numbers from the keyboard, and determine and display which (if either) is the
larger of the two numbers.
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
cout << "Numbers in ascending order: " << num1 << ", " << num2 << ", " << num3 << endl;
return 0;
}
4. Add the numbers from 1 to 100 and display the sum.
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
cout << "The sum of numbers from 1 to 100 is: " << sum << endl;
return 0;
}
5. Add the even numbers between 0 and any positive integer number given by the user.
#include <iostream>
using namespace std;
int main() {
int limit, sum = 0;
cout << "Enter a positive integer: ";
cin >> limit;
cout << "The sum of even numbers from 0 to " << limit << " is: " << sum << endl;
return 0;
}
6. Find the average of two numbers given by the user.
#include <iostream>
using namespace std;
int main() {
double num1, num2, average;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int main() {
int num1, num2, num3, maxVal, minVal, sum;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
return 0;
}
8. Find the area of a circle where the radius is provided by the user.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double radius, area;
cout << "Enter the radius of the circle: ";
cin >> radius;
area = M_PI * radius * radius;
cout << "The area of the circle is: " << area << endl;
return 0;
}
9. Swap the contents of two variables using a third variable.
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
// Swap using a third variable
temp = a;
a = b;
b = temp;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
10. Swap the contents of two variables without using a third variable.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}
return 0;
}
12. Read 10 integers from the keyboard in the range 0 - 100, and count how many of them are
larger than 50, and display this result.
#include <iostream>
using namespace std;
int main() {
int num, count = 0;
cout << "Enter 10 integers (0-100):" << endl;
cout << "There are " << count << " numbers larger than 50." << endl;
return 0;
}
13. Take an integer from the user and display the factorial of that number.
#include <iostream>
using namespace std;
int main() {
int num;
unsigned long long factorial = 1;
cout << "Enter an integer: ";
cin >> num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
cout << "The factorial of " << num << " is: " << factorial << endl;
}
return 0;
}
Worksheet 3
1. Write for, do-while, and while statements to compute the following sums
and products.
a. Sum of 1 + 2 + 3 + … + 100
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
cout << "Sum (1 + 2 + ... + 100) using for loop: " << sum << endl;
return 0;
}
Using while loop:
#include <iostream>
using namespace std;
int main() {
int sum = 0, i = 1;
while (i <= 100) {
sum += i;
i++;
}
cout << "Sum (1 + 2 + ... + 100) using while loop: " << sum << endl;
return 0;
}
Using do-while loop:
#include <iostream>
using namespace std;
int main() {
int sum = 0, i = 1;
do {
sum += i;
i++;
} while (i <= 100);
cout << "Sum (1 + 2 + ... + 100) using do-while loop: " << sum << endl;
return 0;
}
b. Sum of 5 + 10 + 15 + … + 50
Using for loop:
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 5; i <= 50; i += 5) {
sum += i;
}
cout << "Sum (5 + 10 + 15 + ... + 50) using for loop: " << sum << endl;
return 0;
}
Using while loop:
#include <iostream>
using namespace std;
int main() {
int sum = 0, i = 5;
while (i <= 50) {
sum += i;
i += 5;
}
cout << "Sum (5 + 10 + 15 + ... + 50) using while loop: " << sum << endl;
return 0;
}
Using do-while loop:
#include <iostream>
using namespace std;
int main() {
int sum = 0, i = 5;
do {
sum += i;
i += 5;
} while (i <= 50);
cout << "Sum (5 + 10 + 15 + ... + 50) using do-while loop: " << sum << endl;
return 0;
}
c. Sum of 1 + 1/2 + 1/3 + 1/4 + … + 1/15
Using for loop:
#include <iostream>
using namespace std;
int main() {
double sum = 0;
for (int i = 1; i <= 15; i++) {
sum += 1.0 / i;
}
cout << "Sum (1 + 1/2 + 1/3 + ... + 1/15) using for loop: " << sum << endl;
return 0;
}
Using while loop:
#include <iostream>
using namespace std;
int main() {
double sum = 0;
int i = 1;
while (i <= 15) {
sum += 1.0 / i;
i++;
}
cout << "Sum (1 + 1/2 + 1/3 + ... + 1/15) using while loop: " << sum << endl;
return 0;
}
Using do-while loop:
#include <iostream>
using namespace std;
int main() {
double sum = 0;
int i = 1;
do {
sum += 1.0 / i;
i++;
} while (i <= 15);
cout << "Sum (1 + 1/2 + 1/3 + ... + 1/15) using do-while loop: " << sum << endl;
return 0;
}
d. Product of 1 * 2 * 3 * … * 20
Using for loop:
#include <iostream>
using namespace std;
int main() {
long long product = 1;
for (int i = 1; i <= 20; i++) {
product *= i;
}
cout << "Product (1 * 2 * 3 * ... * 20) using for loop: " << product << endl;
return 0;
}
Using while loop:
#include <iostream>
using namespace std;
int main() {
long long product = 1, i = 1;
while (i <= 20) {
product *= i;
i++;
}
cout << "Product (1 * 2 * 3 * ... * 20) using while loop: " << product << endl;
return 0;
}
Using do-while loop:
#include <iostream>
using namespace std;
int main() {
long long product = 1, i = 1;
do {
product *= i;
i++;
} while (i <= 20);
cout << "Product (1 * 2 * 3 * ... * 20) using do-while loop: " << product << endl;
return 0;
}
2. Print the numbers 10 through 49 in the following manner:
#include <iostream>
using namespace std;
int main() {
for (int i = 10; i <= 49; i++) {
cout << i << " ";
if (i % 10 == 9) {
cout << endl;
}
}
return 0;
}
3. Display all the prime numbers between 1 and 100.
#include <iostream>
using namespace std;
int main() {
for (int num = 2; num <= 100; num++) {
if (isPrime(num)) {
cout << num << " ";
}
}
cout << endl;
return 0;
}
4. Count the number of digits in an integer number.
#include <iostream>
using namespace std;
int main() {
int number, count = 0;
cout << "Enter an integer: ";
cin >> number;
while (number != 0) {
number /= 10;
count++;
}
cout << "The number has " << count << " digits." << endl;
return 0;
}
5. Compute the letter grade of a student after accepting the mid and final marks.
#include <iostream>
using namespace std;
char calculateGrade(int mid, int finalExam) {
int total = mid + finalExam;
if (mid < 0 || mid > 40 || finalExam < 0 || finalExam > 60) {
cout << "Error: Marks should be in the range (mid: 0-40, final: 0-60)." << endl;
return 'E'; // Error grade
}
if (total >= 90) return 'A';
if (total >= 80) return 'B';
if (total >= 70) return 'C';
if (total >= 60) return 'D';
return 'F';
}
int main() {
int mid, finalExam;
char continueChoice;
do {
cout << "Enter midterm mark (0-40): ";
cin >> mid;
cout << "Enter final exam mark (0-60): ";
cin >> finalExam;
return 0;
}
6. Compute the factorial of a positive number using a for loop.
#include <iostream>
using namespace std;
int main() {
int number;
long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> number;
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
cout << "The factorial of " << number << " is: " << factorial << endl;
}
return 0;
}
7. Write a C++ code that computes the sum of the following series.
Sum = 1! + 2! + 3! + 4! + …n!
The program should accept the number from the user.
#include <iostream>
using namespace std;
int main() {
int n;
long long sum = 0;
return 0;
}
8. Using the ASCII table numbers, write a program to print the following
output, using a
nested for loop. (Hint: the outer loop should loop from 1 to 5, and the inner
loop’s
start variable should be 65, the value of ASCII “A”).
A
AB
ABC
ABCD
ABCDE
#include <iostream>
using namespace std;
int main() {
// Outer loop to control the number of rows (1 to 5)
for (int i = 1; i <= 5; i++) {
// Inner loop to print characters from 'A' onwards
for (int j = 65; j < 65 + i; j++) {
cout << (char)j; // Print the character corresponding to ASCII value 'j'
}
cout << endl; // Move to the next line after each row
}
return 0;
}
9. Write a C++ program that displays the following output using their ASCII
values.
a
bc
def
gehi
jklmn
opqrst
#include <iostream>
using namespace std;
int main() {
// Variable to keep track of the starting ASCII value
int start = 97; // ASCII value of 'a'
return 0;
}
10. Write a C++ program that will print the following shapes.
A.
*
**
***
****
*****
B.
*****
****
***
**
*
C.
*
***
*****
*******
*********
D.
*
***
*****
***
#include <iostream>
using namespace std;
int main() {
// Shape A
// Shape B
// Shape C
}
cout << endl;
// Shape D
return 0;
11. Write a weather-calculator program that asks for a list of the previous 10
days’
temperatures, computes the average, and prints the results. You have to
compute the
total as the input occurs, then divide that total by 10 to find the average. Use
a while
loop for the 10 repetitions
#include <iostream>
int day = 1;
cout << "Enter temperature for day " << day << ": ";
total += temperature;
cout << "\nTotal temperature for the last 10 days: " << total << endl;
return 0;
12. Write a C++ program using for loop that can compute the following
summation:
#include <iostream.h>
int main()
double sum=0;
for(int i=1;i<=30;i++)
sum=sum+((i/3.0)*2);
return 0;
Write a C++ program that accepts marks of five students and then displays
their
average. The program should not accept mark which is less than 0 and mark
greater
than 100.
#include <iostream>
int main() {
double average;
do {
cout << "Enter marks for student " << (i + 1) << " (between 0 and 100): ";
cout << "Invalid mark. Please enter a mark between 0 and 100." << endl;
} while (marks[i] < 0 || marks[i] > 100); // Keep asking for the mark until it's valid
#include <iostream>
using namespace std;
int main() {
char op;
if (op == '+') {
cout << num1 << " " << op << " " << num2 << " equals " << num1 + num2 << endl;
cout << num1 << " " << op << " " << num2 << " equals " << num1 - num2 << endl;
cout << num1 << " " << op << " " << num2 << " equals " << num1 * num2 << endl;
if (num2 == 0) {
cout << num1 << " " << op << " " << num2 << " can’t be computed: denominator is 0." << endl;
} else {
cout << num1 << " " << op << " " << num2 << " equals " << num1 / num2 << endl;
} else {
}
return 0;
int main() {
char ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
else {
} else {
return 0;
Write a C++ code to display only even numbers found between 0 and 20.
#include <iostream>
int main() {
if (i % 2 == 0) {
cout << endl; // End the line after displaying all even numbers
return 0;
Write a C++ application that extracts a day, month and year and
determine whether
the date is valid. If the program is given a valid date, an appropriate
message is
displayed. If instead the program is given an invalid date, an
explanatory message is
given. Note: to recognize whether the date is valid, we must be able
to determine
whether the year is a leap year or not.
An example of the expected input/output behavior for a valid date
follows
Please enter a date (dd mm yyyy) : 30 4 2006
30/4/2006 is a valid date
Please enter a date (dd mm yyyy) : 1 13 2006
Invalid month: 13
Please enter a date (dd mm yyyy) : 29 2 1899
Invalid day of month 29
If the year is a leap year, then February will have total of 29 days.
Otherwise, it
will have 28 days. If the year is not a century year and is evenly
divisible by 4,
then the year is a leap year. If the year is a century year (years
whose last digits
are 00) and is evenly divisible by 400, then the year is a leap year.
#include <iostream>
} else {
return false;
int daysInMonth;
switch (month) {
break;
break;
case 2:
if (isLeapYear(year)) {