Sunday, 19 February 2017

Solution to Checking Nos.

Solution to check numbers asked in ICSE

Program containing all the booleans of different Numbers :-

This program is the solution to my previous blog in which gives information about different types of numbers to be checked while solving ICSE-practical based questions.

Paste this program in a class editor source code of a Blue J File :-
___________________________________________________________________________________

import java.util.Scanner;
class CheckNOS
{
    Scanner S = new Scanner(System.in);
    int digitFactorial(int x)
    {
        int sum =0;
        while(x>0)
        {
            int d = x%10;
            int su = 1;
            for(int i = 1;i<=d;i++)
            su = su*i;
            sum = sum+su;
            x = x/10;
        }
        return sum;
    }
    public void main()
    {
        CheckNOS on = new CheckNOS();
        System.out.println("Enter a number");
        int no = S.nextInt();
        System.out.println("              Press 1 to check for Harshad/niven No.");
        System.out.println("              Press 2 to check for Disarum No.");
        System.out.println("              Press 3 to check for Palindrome No.");
        System.out.println("              Press 4 to check for Prime No.");
        System.out.println("              Press 5 to check for Special No.");
        System.out.println("              Press 6 to check for Automorphic No.");
        System.out.println("              Press 7 to check wether no. belongs to Fibonacci Series or not.");
        System.out.println("              Press 8 to check for Armstrong No.");
        System.out.println("              Press 9 to check for Prime Palindrome No.");
        System.out.println("              Press 10 to check for Duck No.");
        System.out.println("              Press 11 to check for Smith No.");
        System.out.println("              Press 12 to check for Pronic No.");
        System.out.println("              Press 13 to check for Emirp No.");
        System.out.println("              Press 14 to check for Amicable No.");
        System.out.println("              Press 15 to check for ISBN No.");
        System.out.println("              Press 16 to check for Composite No.");
        System.out.println("              Press 17 to check for Circular Prime.");
        System.out.println("              Press 18 to check for Happy Number");
        System.out.println("              Press 19 to check for Kaprekar Number");
        System.out.println("              Press 20 to exit.");
        System.out.println("Enter Your Choice :-");
        int ch = S.nextInt();
        switch(ch)
        {
            case 1 : if(on.isHarshad(no))
            System.out.println("It is a Harshad No.");
            else
            System.out.println("Not a Harshad No.");
            on.main();
            break;
            case 2 : if(on.isDisarum(no))
            System.out.println("It is a Disarum No.");
            else
            System.out.println("Not a Disarum No.");
            on.main();
            break;
            case 3 : if(on.isPalindrome(no))
            System.out.println("It is a Palindrome No.");
            else
            System.out.println("Not a Palindrome No.");
            on.main();
            break;
            case 4 : if(on.isPrime(no))
            System.out.println("It is a Prime No.");
            else
            System.out.println("Not a Prime No.");
            on.main();
            break;
            case 5 : if(on.isSpecial(no))
            System.out.println("It is a Special No.");
            else
            System.out.println("Not a Special No.");
            on.main();
            break;
            case 6 : if(on.isAutomorphic(no))
            System.out.println("It is a Automorphic No.");
            else
            System.out.println("Not a Automorphic No.");
            on.main();
            break;
            case 7 : if(on.isFibonacci(no))
            System.out.println("It is a Fibonacci No.");
            else
            System.out.println("Not a Fibonacci No.");
            on.main();
            break;
            case 8 : if(on.isArmstrong(no))
            System.out.println("It is a Armstrong No.");
            else
            System.out.println("Not a Armstrong No.");
            on.main();
            break;
            case 9 : if(on.isPrimePalindrome(no))
            System.out.println("It is a Prime Palindrome No.");
            else
            System.out.println("Not a Prime Palindrome No.");
            on.main();
            break;
            case 10 : if(on.isDuck(no))
            System.out.println("It is a Duck No.");
            else
            System.out.println("Not a Duck No.");
            on.main();
            break;
            case 11 : if(on.isSmith(no))
            System.out.println("It is a Smith No.");
            else
            System.out.println("Not a Smith No.");
            on.main();
            break;
            case 12 : if(on.isPronic(no))
            System.out.println("It is a Pronic No.");
            else
            System.out.println("Not a Pronic No.");
            on.main();
            break;
            case 13 : if(on.isEmirp(no))
            System.out.println("It is a Emirp No.");
            else
            System.out.println("Not a Emirp No.");
            on.main();
            break;
            case 14: System.out.println("Enter the two numbers ");
            System.out.println("Enter the first number ");
            int a = S.nextInt();
            System.out.println("Enter the second number ");
            int b = S.nextInt();
            if(isAmicable(a,b))
            System.out.println("They are Amicable No.");
            else
            System.out.println("They are not Amicable No.");
            break;
            case 15 : if(isISBN(no))
            System.out.println("It is a legal ISBN No.");
            else
            System.out.println("It is not a legal ISBN No.");
            break;
            case 16 : if(isPrime(no))
            System.out.println("Not a Composite Number");
            else 
            System.out.println("It is a Composite Number");
            break;
            case 17 : if(isCircularPrime(no))
            System.out.println("It is a Circular Prime Number");
            else 
            System.out.println("It not a Circular Prime Number");
            on.main();
            break;
            case 18 : if(isHappy(no))
            System.out.println("It is a Happy Number");
            else 
            System.out.println("It not a Happy Number");
            on.main();
            break;
            case 19 : if(isKaprekar(no))
            System.out.println("It is a Happy Number");
            else 
            System.out.println("It not a Happy Number");
            on.main();
            break;
            case 20 : System.exit(0);
            break;
            default : System.out.println("Wrong choice Entered ! Try again ");
            on.main();
            break;
        }
    }
    public boolean isISBN(long x)
    {
        long c = x;
        long sum = 0;
        for(int i = 1;i<=10;i++)
        {
            long d = c%10;
            c=c/10;
            sum = sum + (d*i);
        }
        if((sum%11)==0)
        return true;
        else 
        return false;
    }
    public boolean isHarshad(int xi)
    {
        int sum = 0,x=xi,d;
        while(x>0)
        {
             d = x%10;
            sum = sum+d;
            x=x/10;
            
        }
        if(xi%sum==0)
        return true;
        else 
        return false;
    }
    public boolean isDisarum(int x)
    {
        double sum = 0;
        int x1 = x;
        int power = 1;
        while(x>0)
        {
            int d= x%10;
            sum = sum + (Math.pow(d,power));
            power++;
            x = x/10;
        }
        if(x1==(int)sum)
        return true;
        else
        return false;
    }
    public boolean isPalindrome(int x)
    {
        int sum = 0;
        int y = x;
        while(y>0)
        {
            sum = sum*10;
            sum = sum+(y%10);
            y= y/10;
        }
        if(sum==x)
        return true;
        else
        return false;
    }
    public boolean isPrime(int x)
    {
        int killme = 0;
        for(int i = 2;i<x;i++)
        {
            if(x%i==0)
            killme++;
            else
            continue;
        }
        if(killme>0)
        return false;
        else
        return true;
    }
    public boolean isSpecial(int x)
    {
        int prod = 1;
        int r = x;
        int sum = 0;
        while(x!=0)
        {
            int d = x%10;
            prod = prod*d;
            sum = sum +d;
            x=x/10;
        }
        if((prod+sum)==r)
        return true;
        else 
        return false;
    }
    public boolean isAutomorphic(int x)
    {
        int u = x;
        int sq = x*x;
        String square = Integer.toString(sq);
        String u1 = Integer.toString(u);
        int len = square.length();
        int length = u1.length();
        if((square.charAt(len-1)==u1.charAt(length-1))&&(square.charAt(len-2)==u1.charAt(length-2)))
        return true;
        else
        return false;
    }
    public boolean isSmith(int x)
    {
        int y = x;
        CheckNOS ob = new CheckNOS();
        int sum =0;
        int sum1 =0;
        while(y>1)
        {
            for(int i= 2;i<=y;i++)
            {
                int s2 = 0;
                if(((y%i)==0)&&(ob.isPrime(i)))
                {
                    String st = Integer.toString(i);
                    int len = st.length();
                    if(len!=1)
                    {
                        int k = i;
                        while(k>0)
                        {
                            s2 = s2 + (k%10);
                            k = k/10;
                        }
                    }
                    else
                    s2 = s2+i;
                    y=y/i;
                }
                sum = sum + s2;
            }
        }
        y=x;
        while(y>0)
        {
            sum1 = sum1 + (y%10);
            y=y/10;
        }
        if(sum==sum1)
        return true;
        else
        return false;
    }
    public boolean isArmstrong(int x)
    {
        int y = x;
        int sum = 0;
        while(y>0)
        {
            int d = y%10;
            y=y/10;
            sum = sum +(d*d*d);
        }
        if(sum==x)
        return true;
        else
        return false;
    }
    public boolean isPrimePalindrome(int x)
    {
        int y = x;
        if(isPrime(y))
        {
            String str = Integer.toString(y);
            String ns = "";
            for(int i = 0;i<str.length();i++)
             ns = str.charAt(i)+ns;
            if(ns.equalsIgnoreCase(str))
            return true;
        }
        return false;
    }

    Scanner S = new Scanner(System.in);
    public boolean isKaprekar(int x)
    {
        int x2=x*x;
        int nod= numberOfDigits(x2);
        int n1,n2;
        n1=x2/((int)Math.pow(10,nod/2));
        n2=x2%((int)Math.pow(10,nod/2));
        if((n1+n2)==x)
        {
            System.out.println(x+"x"+x+" = "+(x*x)+", right hand piece of "+(x*x)+" = "+n2+" and left hand piece of "+(x*x)+" = "+n1+"\nSum = "+n1+" + "+n2+" = "+(n1+n2)+", i.e. equal to the number");
            return true;
        }
        else
        return false;
    }
    public int numberOfDigits(int x)
    {
        int y=x;
        int result=0;
        while(y>0)
        {
            y=y/10;
            result++;
        }
        return result;
    }
    public boolean isDuck(int x)
    {
        String str = Integer.toString(x);
        String ns = "";
        if(str.charAt(0)=='0')
        return false;
        else
        {
            for(int i =1;i<str.length();i++)
            {
                if(str.charAt(i)=='0')
                return true;
            }
        }
        return false;
    }
    public boolean isPronic(int x)
    {
        for(int i = 0; i<x;i++)
        {
            if((i*(i+1))==x)
            return true;
        }
        return false;
    }
    public boolean isFibonacci(int x)
    {
        int n1 = 1;
        int n2 = 1;
        while(n2<=x)
        {
            n1 = n2;
            n2 = n1+n2;
            if(n2==x)
            return true;
        }
        return false;
    }
    public boolean isEmirp(int x)
    {
        if(isPrime(x))
        {
            int y=x;
            int rev = 0;
            while(y>0)
            {
                 int d=y%10;
                 rev=rev*10+d;
                 y=y/10;
            }
            if(isPrime(rev))
            return true;
        }
        return false;
    }
    boolean isAmicable(int a,int b)
    {
        int s=0,i;
        for(i=1;i<a;i++)
        {
            if(a%i==0)
            {
                s=s+i;
            }
        }
        if(s==b)
        {
            s=0;
            for(i=1;i<b;i++)
            {
                if(b%i==0)
                {
                    s=s+i;
                }
            }
            if(s==a)
            return true;
            else
            return false;
        }
        return false;
    }
    public int no_of_digits(int x)
    {
        int y = x;
        int nod=0;
        while(y>0)
        {
            y=y/10;
            nod++;
        }
        return nod;
    }

    public boolean isCircularPrime(int x)
    {
        int nod = no_of_digits(x);
        int y = x;
        while(isPrime(y)==true)
        {
            int c = y%10;
            y = y/10;
            y=(c*(power(10,(nod-1))))+y;
            if(x==y)
            return true;
        }
        return false;
    }
    public int power(int x, int y)
    {
        int result = 1;
        while(y>0)
        {
            result = result*x;
            y--;
        }
        return result;
    }
    public boolean isHappy(int x)
    {
        int y = x;
        int sum = sumofdigitsquare(y);
        int u=0;
        while(sum>9)
        sum = sumofdigitsquare(sum);
        if(sum==1)
        return true;
        else
        return false;
    }
    public int sumofdigitsquare(int x)
    {
        int sum = 0;
        while(x>0)
        {
            sum = sum +((x%10)*(x%10));
            x=x/10;
        }
        return sum;
    }
}





------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x----------x-----------x-----------x-----------x-----------x--
Note : If any one of you guys want me to upload a video on how the logic of checking different numbers works, leave a comment below and I would be uploading that shortly...