Question: My code works perfectly but when I type a number like 13, 14, or 15 the factorial is inaccurate I can't tell what's wrong. #include

 My code works perfectly but when I type a number like
My code works perfectly but when I type a number like 13, 14, or 15 the factorial is inaccurate I can't tell what's wrong.
#include
#include
using namespace std;
//Function Prototypes
long Sum(long);
long Factorial(long);
int main()
{
//Declare variables for factorial, sum, and num.
long Total;
long num;
long factorial;
//prompt and read a number
cout
cin >> num;
//loop until a valid integer is entered
while(cin.fail() || num
{
cin.clear();
cin.ignore(100, ' ');
cout
cout
cin >> num;
}
//Obtain the factorial using the "Factorial" function
factorial = Factorial(num);
//Show the value of the factorial answer.
cout
//Obtain the sum of the integers within the factorial using the "Sum" function.
Total = Sum(factorial);
//Show the sum of digits inside the factorial.
cout
cout
}
//This function takes an integer as a parameter and returns its factorial.
long Factorial(long num)
{
//Set the function factorial equal to 1
long factorial = 1;
//Do a for loop which will calculate the factorial.
//Run the loop from 1 to num.
for (long i=1; i
{
//Multiply the factorial by i
factorial = factorial * i;
}
//Return the factorial.
return factorial;
}
long Sum(long num)
{
long Total = 0;
long number = num;
//loop for int(log10(number)) + 1 number of times.
for (int i=1; i
{
//Obtain the first digit and add it to total sum of the factorial.
Total = Total + num % 10;
//Here take out the first digit of the factorial.
num = num / 10;
}
//Return the total sum of digits
return Total;
}
CS135.1002 Assignment 6: Factorial Digit Sum 46789012 456789 01234567 89012345 106629 Description In this program, you will compute the factorial of a number and then sum up each of its digits. Once again, if the number entered is negative or cannot be interpreted as a number, re-prompt the user and you will keep re-prompting the user until a valid positive integer has been entered. The definition of factorials for say n isn (n-1) (n -2) (n-3) 1, so for example the factorial of 5 which is expressed as 5! is 5.4.3-2-1 which is 120. So the sum of the digits of 5! would be 3 because 5! is 120 and then 1 2 +0 is 3. Computing the factorial is the "easy" part, now you will have to break the number apart into integers and sum them up, and no, you cannot convert the integer into a string variable (because that would be way too easy). To break a number into individual digits, you will need to do a series of integer divisions and mod divisions to grab each digit and then remove that digit from the number. Here's a hint, if we have the number 12345 we want to get 1 out of this number (the leftmost digit), what will we divide 12345 by to get the number 1? After we get the 1, now we want to remove that 1 from 12345 to get 2345 (what will we need to do now)? Once we have 2345, we want to get the leftmost digit again (which is 2), so we perform the same operation as earlier and then we add that 2 to the 1, and once again we need to get rid of the 2 to get 345 and we do this a number of times. The number of times we will do this is int(log10 (n)1, taking the base 10 logarithm of a number (casted to an integer) and added by 1 tells you how many digits the number has, if you don' believe me, try it! Don't use the int type in this assignment or you could get incorrect answers and/or warnings! Use the datatype long, a long is a type that can hold a really big integer not use numbers larger than 15 for the input! YOU ARE NOT ALLOWED TO CONVERT THE NUMBER INTO A STRING VARIABLE, OR READ A STRING VARIABLE, THAT WOULD BE WAY TOO EASY

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!