Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java20;
- /**
- * Program to calculate the factorial of a given number.
- * Once implemented, it will calculate how many digits the answer includes.
- * It will then sum these digits together to provide a total.
- * @author shardy
- * date: 30/09/2012
- */
- //import java.math.BigInteger;
- public class Java20 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //Using given number stored in factorialNo, calculates factorial
- //currently only works for numbers between 1! and 31! :(
- int fact= 1;
- int factorialNo = 10;
- for (int i = 1; i <= factorialNo; i++)
- {
- fact=fact*i;
- }
- System.out.println("The factorial of " + factorialNo +
- " (or " + factorialNo + "!) is: " + fact);
- //Using answer stored in fact, calculates how many digits the answer has
- final int answerNo = fact;
- final int digits = 1 + (int)Math.floor(Math.log10(answerNo));
- System.out.println("The number of digits in the factorials "
- + "answer is: " + digits);
- //Using remainders, calculates each digits value and sums them together
- int number = fact;
- int reminder;
- int sum = 0;
- while(number>=1)
- {
- reminder=number%10;
- sum=sum+reminder;
- number=number/10;
- }
- System.out.println("The total sum of all the " + digits
- + " idividual digits from the answer of the factorial of "
- + factorialNo + " is: " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement