Question

In: Computer Science

Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing...

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

Solutions

Expert Solution

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 :)


Related Solutions

An Armstrong number is a number that is the sum of its own digits, each raised...
An Armstrong number is a number that is the sum of its own digits, each raised to the power of the number of its digits. For example, 153 is considered an Armstrong number because 13 + 53 + 33 = 153. Write a VBA program that lists all 3-digit Armstrong numbers within a specified range of 3-digit positive integers. Your sub should do the following: 1) Using two input boxes, ask the user to input two 3-digit positive integers (a...
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
A random number X is generated by reporting the sum of the digits on two randomly...
A random number X is generated by reporting the sum of the digits on two randomly chosen ping-pong balls, one from a bucket of two balls labeled “0” and “1” and one from a bucket of three balls labeled “0”, “1”, and “2”. Find the probability mass function of X by specifiying the values x that X can take and the probability p(x) = P (X = x) of each of those values. Please explain how you arrived at the...
A triangular number is the sum of the n natural numbers from 1 to n. For...
A triangular number is the sum of the n natural numbers from 1 to n. For example: The triangular number for 3 is 1 + 2 + 3 = 6 The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n...
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n natural numbers: H(n) = 1+ 1/2 + 1/3 +1/4 +... +1/n Write a recursive method and an accompanying main method to compute the n^th Harmonic number. I have tried but get a blank and would really apreciate a fully explained code.
Find the sum (n=5, to infinity) of the series (n2 -n)/2n
Find the sum (n=5, to infinity) of the series (n2 -n)/2n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT