Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program inputs a number and checks whether the number entered is a Fascinating Number or not.
- * For more information, please visit: https://goo.gl/oLrjqm
- */
- import java.util.*;
- public class Fascinating_Number
- {
- static long getNumber(int num) //Function to multiply the number and join the resulting numbers.
- {
- String one = Integer.toString(num);
- String two = Integer.toString(num*2);
- String three = Integer.toString(num*3);
- String res = one + two + three;
- long result = Long.parseLong(res);
- return result;
- }
- static int getSum(long num) //Function to get the sum of a number.
- {
- String a = Long.toString(num);
- int i, sum = 0;
- for(i=0;i<a.length();i++)
- {
- sum += Character.getNumericValue(a.charAt(i));
- }
- return sum;
- }
- static boolean checkSum(long num) //Function to check whether the sum of the combined number is equal to the sum of numbers from 1 - 9.
- {
- int tensum = getSum(1234567890); //Getting the sum of numbers from 1 - 9.
- int numsum = getSum(num); //Getting the sum of the combined number.
- if(tensum == numsum)
- return true;
- else
- return false;
- }
- static boolean checkDigits(long num) //Function to check whether the number has any repeated digits except for 0.
- {
- String a = Long.toString(num);
- char x;
- int i, flag = 1;
- for(i=0;i<a.length();i++)
- {
- x = a.charAt(i);
- if(a.indexOf(x) != a.lastIndexOf(x) && x != '0') //Checks numbers 1 - 9 and ignores zeros.
- {
- flag = 0;
- break;
- }
- }
- if(flag == 1)
- return true;
- else
- return false;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- //Taking user input.
- System.out.println("Please enter a number.");
- int n = sc.nextInt();
- //Multiplying and joining the number.
- long num = getNumber(n);
- if(checkSum(num) && checkDigits(num)) //The two step verification makes the program fool-proof.
- System.out.println(n+" is a Fascinating Number.");
- else
- System.out.println(n+" is not a Fascinating Number.");
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment