Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Mat
- */
- package Fact100;
- import java.math.BigInteger;
- public class Fact100Add {
- public static void main(String[] args) {
- // set as one as the first factorial is 1*
- BigInteger number1 = new BigInteger("1");
- //loop through multiplying n= n*(n+1)
- for(BigInteger i = new BigInteger("2");
- i.compareTo(BigInteger.valueOf(100)) <= 0; i = i.add(BigInteger.ONE)){
- number1 = number1.multiply(i);
- }
- System.out.println("!100 factorial is\n" + number1.toString());
- //creates a new string object which is number1
- String s = number1.toString();
- int totalnum = 0;
- //loop through each number of the array turn it into a int and add to total
- for(int i = 0;i < s.length();i++){
- totalnum+=Integer.parseInt(s.substring(i, i+1));
- }
- System.out.println("Added together is " + totalnum);
- }
- }
- /* Output
- * !100 factorial is
- * 93326215443944152681699238856266700490715968264381621468592963895217599993229
- * 91560894146397615651828625369792082722375825118521091686400000000000000000000
- * 0000
- * Added together is 648
- */
Advertisement
Add Comment
Please, Sign In to add comment