What is a nasty number? Well it is a number whose four different factors can be arranged in such a way that sum of two is equal to difference of other two. Take an example of nasty number 36. It have factors 1,2,3,4,6,9,12,18. So, we have 2+1=6-3 . So, it is a nasty number.
Main class
import java.util.Scanner; class NastyMain{ public static void main(String args[]){ Scanner br = new Scanner(System.in); char x='y'; int num=0; NastyFinder Nasty = new NastyFinder(); do{ System.out.print("Enter the number you want to check : "); num = br.nextInt(); Nasty.isNasty(num); System.out.println("\n Do you want to check another number : y/anykey"); x=br.next().charAt(0); } while(x=='y' || x=='Y'); } }
Nasty Number Finder Class
class NastyFinder{ public void isNasty(int num){ // declare arrays int arr[] = new int[50]; // to store factors of given number int arrPlus[][] = new int[50][50]; // to store addition of factors int arrMinus[][] = new int[50][50]; // to store difference of factors // convert num to a positive if it's negative if(num=arIndex){ d=0; c++; } } // increment value of a and b like we have done with c and d c=0; d=0; b++; if(b>=arIndex){ b=0; a++; } } if(yes==1){ System.out.print("\n Yes it is a nasty number"); yes=0; } else{ System.out.print("\n No it is not a nasty number"); } }// isNasty closed }