Guest User

Fascinating Number

a guest
Oct 29th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. /**
  2.  * This program inputs a number and checks whether the number entered is a Fascinating Number or not.
  3.  * For more information, please visit: https://goo.gl/oLrjqm
  4.  */
  5. import java.util.*;
  6. public class Fascinating_Number
  7. {
  8.     static long getNumber(int num) //Function to multiply the number and join the resulting numbers.
  9.     {
  10.         String one = Integer.toString(num);
  11.         String two = Integer.toString(num*2);
  12.         String three = Integer.toString(num*3);
  13.         String res = one + two + three;
  14.         long result = Long.parseLong(res);
  15.         return result;
  16.     }
  17.    
  18.     static int getSum(long num) //Function to get the sum of a number.
  19.     {
  20.         String  a = Long.toString(num);
  21.         int i, sum = 0;
  22.         for(i=0;i<a.length();i++)
  23.         {
  24.             sum += Character.getNumericValue(a.charAt(i));
  25.         }
  26.        
  27.         return sum;
  28.     }
  29.    
  30.     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.
  31.     {
  32.         int tensum = getSum(1234567890); //Getting the sum of numbers from 1 - 9.
  33.         int numsum = getSum(num); //Getting the sum of the combined number.
  34.        
  35.         if(tensum == numsum)
  36.             return true;
  37.         else
  38.             return false;
  39.     }
  40.    
  41.     static boolean checkDigits(long num) //Function to check whether the number has any repeated digits except for 0.
  42.     {
  43.         String a = Long.toString(num);
  44.         char x;
  45.         int i, flag = 1;
  46.         for(i=0;i<a.length();i++)
  47.         {
  48.             x = a.charAt(i);
  49.             if(a.indexOf(x) != a.lastIndexOf(x) && x != '0') //Checks numbers 1 - 9 and ignores zeros.
  50.             {
  51.                 flag = 0;
  52.                 break;
  53.             }
  54.         }
  55.        
  56.         if(flag == 1)
  57.             return true;
  58.         else
  59.             return false;
  60.     }
  61.    
  62.     public static void main(String[] args)
  63.     {
  64.         Scanner sc = new Scanner(System.in);
  65.         //Taking user input.
  66.         System.out.println("Please enter a number.");
  67.         int n = sc.nextInt();
  68.         //Multiplying and joining the number.
  69.         long num = getNumber(n);
  70.         if(checkSum(num) && checkDigits(num)) //The two step verification makes the program fool-proof.
  71.             System.out.println(n+" is a Fascinating Number.");
  72.         else
  73.             System.out.println(n+" is not a Fascinating Number.");
  74.        
  75.         sc.close();
  76.     }
  77. }
Add Comment
Please, Sign In to add comment