| Subject: Programming Fundamentals | Level: Beginner |
This lecture applies for, while, and do-while loops to practical programming problems, including factorials, number sums, digit counting, and menu-driven programs.
By the end of this lecture, students will be able to:
The factorial of a positive integer is the product of all positive integers from 1 up to that number.
For example:
\[ 5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \]
Remember that \(0! = 1\).
#include <iostream>
using namespace std;
int main()
{
int n;
long long factorial = 1;
cout << "Enter a number (0 to 20): ";
cin >> n;
if (n < 0 || n > 20)
{
cout << "Please enter a number from 0 to 20.";
}
else
{
for (int i = 1; i <= n; i++)
{
factorial = factorial * i;
}
cout << "Factorial = " << factorial;
}
return 0;
}
factorial = 1 initializes the product.n.factorial = factorial * i multiplies the running result by each number.long long factorial calculation.For input 5, the calculation is:
i |
Calculation | Factorial |
|---|---|---|
| 1 | 1 × 1 | 1 |
| 2 | 1 × 2 | 2 |
| 3 | 2 × 3 | 6 |
| 4 | 6 × 4 | 24 |
| 5 | 24 × 5 | 120 |
Output:
Enter a number (0 to 20): 5
Factorial = 120
while loop instead of a for loop.0.Suppose the user enters 456. The sum of its digits is:
\[ 4 + 5 + 6 = 15 \]
#include <iostream>
using namespace std;
int main()
{
int n;
int sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
{
cout << "Please enter a non-negative integer.";
}
else
{
while (n > 0)
{
int digit = n % 10;
sum = sum + digit;
n = n / 10;
}
cout << "Sum of digits = " << sum;
}
return 0;
}
Two operators are important:
n % 10 gives the last digit.n / 10 removes the last digit when both operands are integers.For example, if n = 456:
Current n |
n % 10 |
Sum | New n |
|---|---|---|---|
| 456 | 6 | 6 | 45 |
| 45 | 5 | 11 | 4 |
| 4 | 4 | 15 | 0 |
When n becomes 0, the loop stops.
Output:
Enter a positive integer: 456
Sum of digits = 15
1234.For example, the number 58321 contains five digits.
#include <iostream>
using namespace std;
int main()
{
int n;
int count = 0;
cout << "Enter an integer: ";
cin >> n;
if (n < 0)
{
n = -n;
}
if (n == 0)
{
count = 1;
}
else
{
while (n > 0)
{
count++;
n = n / 10;
}
}
cout << "Number of digits = " << count;
return 0;
}
count = 0 initializes the digit counter.count++ increases the count by 1 for each digit.n = n / 10 removes the last digit.n > 0 would initially be false.Sample output:
Enter an integer: 58321
Number of digits = 5
700.0.-12345.A number is reversed by taking its last digit and adding it to a new number.
For example:
1234 becomes 4321.
#include <iostream>
using namespace std;
int main()
{
int n;
int reverse = 0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
{
cout << "Please enter a non-negative integer.";
}
else
{
while (n > 0)
{
int digit = n % 10;
reverse = reverse * 10 + digit;
n = n / 10;
}
cout << "Reversed number = " << reverse;
}
return 0;
}
The key statement is:
reverse = reverse * 10 + digit;
Multiplying reverse by 10 shifts its existing digits one place to the left, making room for the new last digit.
For input 1234:
| Digit extracted | Calculation | New reverse |
|---|---|---|
| 4 | 0 × 10 + 4 | 4 |
| 3 | 4 × 10 + 3 | 43 |
| 2 | 43 × 10 + 2 | 432 |
| 1 | 432 × 10 + 1 | 4321 |
Output:
Enter a positive integer: 1234
Reversed number = 4321
Note: Leading zeros disappear when a number is stored as an integer. For example, reversing 1200 produces 21.
56789.1005.121.A menu-driven program repeatedly displays options until the user chooses to exit.
#include <iostream>
using namespace std;
int main()
{
int choice;
double a, b;
do
{
cout << "\n--- Simple Calculator ---" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1 || choice == 2)
{
cout << "Enter two numbers: ";
cin >> a >> b;
if (choice == 1)
{
cout << "Result = " << a + b << endl;
}
else
{
cout << "Result = " << a - b << endl;
}
}
else if (choice == 3)
{
cout << "Calculator closed." << endl;
}
else
{
cout << "Invalid choice." << endl;
}
}
while (choice != 3);
return 0;
}
do-while loop displays the menu at least once.choice stores the selected option.if-else statements determine which operation to perform.3.Solve the following tasks independently.
1. Print all numbers from 1 to 100.
2. Calculate the sum of numbers from 1 to n.
3. Print the multiplication table of a number.
4. Count the digits of an integer.
5. Calculate the sum of the digits of an integer.
6. Reverse an integer using a loop.
7. Calculate the factorial of a number.
8. Check whether a number is a palindrome.
9. Display all even numbers between two user-entered limits.
10. Create a menu-driven calculator using do-while.
0 of 10 completed
1. What does `n % 10` return for n = 456?
45
6
456
2. What is the value of 5!?
25
60
120
3. Which loop is suitable for a menu that should appear at least once?
do-while
if only
No loop
4. What does integer division `456 / 10` produce in C++?
45.6
45
46
Check Answers
% 10 and / 10.break stops a loop, while continue skips an iteration.do-while is useful for menus that should appear at least once.Suggested next lecture: Choosing the right loop (for vs while vs do-while) and solving mixed loop problems with trace tables.