lordmat

Problem 20

Jun 14th, 2011
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Mat
  4.  */
  5. package Fact100;
  6.  
  7. import java.math.BigInteger;
  8.  
  9. public class Fact100Add {
  10.  
  11.     public static void main(String[] args) {
  12.         // set as one as the first factorial is 1*
  13.         BigInteger number1 = new BigInteger("1");
  14.                
  15.         //loop through multiplying n= n*(n+1)
  16.         for(BigInteger i = new BigInteger("2");
  17.                 i.compareTo(BigInteger.valueOf(100)) <= 0; i = i.add(BigInteger.ONE)){
  18.             number1 = number1.multiply(i);            
  19.         }
  20.        
  21.         System.out.println("!100 factorial is\n" + number1.toString());
  22.        
  23.         //creates a new string object which is number1
  24.         String s = number1.toString();
  25.         int totalnum = 0;    
  26.        
  27.        
  28.         //loop through each number of the array turn it into a int and add to total
  29.         for(int i = 0;i < s.length();i++){
  30.             totalnum+=Integer.parseInt(s.substring(i, i+1));
  31.         }
  32.        
  33.         System.out.println("Added together is " + totalnum);
  34.     }
  35. }
  36. /* Output
  37.  * !100 factorial is
  38.  * 93326215443944152681699238856266700490715968264381621468592963895217599993229
  39.  * 91560894146397615651828625369792082722375825118521091686400000000000000000000
  40.  * 0000
  41.  * Added together is 648
  42.  */
Advertisement
Add Comment
Please, Sign In to add comment