Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program inputs two numbers and prints the composite magic numbers in the entered range (Start and end included).
- * For more information, please visit: https://goo.gl/XsZQ4p .
- */
- import java.util.Scanner;
- public class Magic_Composite_Range
- {
- static boolean isComposite(int num)
- {
- if(num == 2)
- return false;
- int i,flag = 0;
- for(i=2;i<num;i++)
- {
- if(num%i==0)
- {
- flag=1;
- break;
- }
- }
- if(flag == 1)
- return true;
- else
- return false;
- }
- static int digitSum(int num)
- {
- String a = Integer.toString(num);
- int i,x,sum=0;
- for(i=0;i<a.length();i++)
- {
- x = Character.getNumericValue(a.charAt(i));
- sum += x;
- }
- return sum;
- }
- static boolean isMagic(int num)
- {
- int sum = 0;
- sum = digitSum(num);
- while(sum > 9) //Since any number <=9 can't have the sum of 1 unless it is 1 itself.
- {
- sum = digitSum(sum); //Finding the eventual sum.
- }
- if(sum == 1)
- return true;
- else
- return false;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter the lower limit.");
- int start = sc.nextInt();
- System.out.println("Please enter the upper limit.");
- int end = sc.nextInt();
- System.out.println();
- if(start<end)
- {
- int i,ct=0;
- for(i=start;i<=end;i++)
- {
- boolean check1 = isComposite(i);
- boolean check2 = isMagic(i);
- if(check1 && check2)
- {
- System.out.print(i+" | ");
- ct++;
- }
- }
- System.out.println("\nNumber of composite magic numbers between "+start+" and "+end+" = "+ct);
- }
- else
- System.out.println("Incorrect input. The lower limit must be lesser than the upper limit.");
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement