To write C++ programs for:
- Factorial of an integer
- Sum of digits of an integer
- Reverse of a string
- Reverse of an integer
vs code, C++ Complier.
-
Factorial (n!) → The product of all positive integers from 1 to n. Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
-
Sum of Digits → Each digit of the number is extracted using modulus (% 10) and added until the number becomes.
-
Reverse of String → A string can be reversed by traversing it from last character to the first.
4.Reverse of Integer → Digits are extracted using modulus (% 10) and arranged in reverse order using multiplication and addition.
1️⃣ Factorial of an Integer
-
Start
-
Input an integer n.
-
Initialize fact = 1.
-
Repeat from i = 1 to n: multiply
-
fact = fact * i.
-
Print fact.
-
Stop
2️⃣Sum of Digits of an Integer
-
Start
-
Input an integer n.
-
Initialize sum = 0.
-
While n > 0:
5.Extract digit = n % 10.
6.Add digit to sum.
7=Divide n = n / 10
8.Print sum.
- Stop
3️⃣ Reverse a String
-
Start
-
Input a string str.
-
Initialize empty string rev.
-
Traverse from last character of str to first.
-
Append each character to rev.
-
Print rev.
-
Stop
4️⃣ Reverse an Integer
-
Start
-
Input an integer n.
-
Initialize rev = 0.
-
While n > 0:
5.Extract digit = n % 10.
6.Update rev = rev * 10 + digit.
7.Divide n = n / 10.
-
Print rev.
-
Stop
Using loops and conditional statements, we can calculate factorial of an integer efficiently. By applying the modulus (%) and division (/) operations, we can easily find the sum of digits of an integer.Strings can be manipulated and reversed by traversing them from end to start.Integers can also be reversed digit by digit using simple arithmetic operations.Thus, we successfully implemented and verified the programs for factorial, sum of digits, reverse of a string, and reverse of an integer using C++.