In: Computer Science
Directions: Given a factorial n!. Find the sum
of its digits, and the number of trailing zeros (ie: the number of
zeros at the end of the factorial).
Input: integer n , a non-negative integer where n
≤ 100
Output: integer x y , the concatenation of x and
y, where x is the sum of the digits in n! and y is the number
of the zeros in n!) Note, 0 ≤ x , y .
Example: Consider the factorial: 5! = 5x4x3x2x1 = 120. The sum of its digits is 1+2+0 = 3, and the number of zeros at the end of 5! = 120 is 1. Then here, x = 3 and y = 1, so the answer/output for 5! is 31.
Extra Credit: allow the user to choose any integer
code in c++ plz
Code Screenshot :
Executable Code:
#include <iostream>
using namespace std;
int main() {
//Declaring required variables
int i, fact = 1, number, sum = 0, m;
//Prompting the user for input
cout << "Enter any Number: ";
cin >> number;
//Finding the factorial
for (i = 1; i <= number; i++) {
fact = fact * i;
}
//Printing the factorial
cout << "Factorial :" << fact;
//Finding the sum of digits of
factorial
while (fact > 0) {
m = fact % 10;
sum = sum + m;
fact = fact / 10;
}
//Printing the sum of digits
cout << "\nSum of digits: " << sum;
//Finding the number of traling zeroes in
factorial
int count = 0;
for (int i = 5; number / i >= 1; i *= 5)
count += number / i;
cout << "\nTrailing zeroes: " << count;
//COmbining both the numbers
string s1 = to_string(sum);
string s2 = to_string(count);
string s = s1 + s2;
int result = stoi(s);
//Printing the result
cout << "\nResult :" << result;
return 0;
}
Sample Output :
Please comment
below if you have any queries.
Please do give a thumbs up if you liked the answer thanks
:)