Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Challenge1 {
- public static int Factorial(int number)
- {
- int temp;
- if(number <= 1)
- {
- return 1;
- }
- temp = number* Factorial(number -1);
- return temp;
- }
- public static int FindLastNonZero(int number)
- {
- int foundNumber = number;
- int count = 0;
- //Convert number to an array of integers
- //First: find how long the array will be
- while(foundNumber != 0)
- {
- foundNumber /= 10;
- count++;
- }
- // create the new array based off of the found length
- int numberArray[] = new int[count];
- count = 0;
- foundNumber = number;
- //Create the array and put the last digit into the first position.
- // i.e. 1024 array[0] = 4, array[1] = 2, array[2] =0, etc...
- while (foundNumber != 0)
- {
- numberArray[count] = foundNumber % 10;
- foundNumber /= 10;
- count++;
- }
- int position=0;
- // iterate through the array to find the last non-zero integer
- while (numberArray[position] == 0)
- {
- position++;
- }
- return numberArray[position];
- }
- public static void main(String [] args) throws NumberFormatException, IOException
- {
- int inputNum;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- inputNum = Integer.parseInt(br.readLine());
- System.out.println("Factorial Result= " + Factorial(inputNum));
- System.out.println("Last non-zero integer= " + FindLastNonZero(Factorial(inputNum)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement