Numbers

 

NUMBERS

 

1.      Write a program to find the number is perfect number or not.

 

import java.util.*;

class perfect

{

    void main()

    {

        int n, i, s=0;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter the number");

        n=ob.nextInt();   

        for(i=1;i<n;i++)   

        {

            if(n%i==0)     

            s=s+i;        

        }

        if(s==n)

        System.out.print("The number is perfect number");

        else

        System.out.print("The number is not a perfect number");

    }

}

 

2.      Write a program to find the number is amicable number or not.

import java.util.*;

class amicable

{

    void main()

    {

        int a, b, i, j, sa=0, sb=0;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter first number");

        a=ob.nextInt();

        System.out.println("Enter second number");

        b=ob.nextInt();

        for(i=1;i<a;i++) 

        {

            if(a%i==0)    

            sa=sa+i;        

        }

        for(j=1;j<b;j++) 

        {

            if(b%j==0)    

            sb=sb+j;        

        }

        if(sa==b && sb==a)

        System.out.print("The number is amicable number");

        else

        System.out.print("The number is not a amicable number");

    }

}

3.      Write a program to find the number is spy number or not.

import java.util.*;

class spy

{

    void main()

    {

        int a, d=0, s=0, p=1;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");   //123

        a=ob.nextInt();

            while(a>0)        

            {

                d=a%10;       

                s=s+d;         

                p=p*d;        

                a=a/10;       

            }

            if(s==p)

            System.out.print("The number is spy");

            else

            System.out.print("The number is not a spy");

    }

}

4.      Write a program to find the number is neon number or not.

import java.util.*;

class neon

{

    void main()

    {

        int a, sq=0, d=0, s=0;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");

        a=ob.nextInt();

        sq=a*a;

        while(sq>0)

        {

            d=sq%10;

            s=s+d;

            sq=sq/10;

        }

        if(s==a)

        System.out.print("The number is neon number");

        else

        System.out.print("The number is not a neon number");

    }

}

5.      Write a program to find the number is automophic number or not.

import java.util.*;

public class automophic

{

    void main()

    {

        int n, d=0, c=1, r=0, a, sq=0;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");

        n=ob.nextInt();        

        a=n;

        sq=n*n;                

        while(n>0)     

        {

            d=n%10;     

            c=c*10;    

            n=n/10;    

        }

        r=sq%c;     

        if(r==a)

        System.out.print("The number is automophic number");

        else

        System.out.print("The number is not a automophic number");

    }

}

6.      Write a program to find the number is Armstrong number or not.

import java.util.*;

class armstrong

{

    void main()

    {

        int a, d=0, s=0, n;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");

        a=ob.nextInt();

        n=a;               

        while(n>0)       

        {

            d=n%10;       

            s=s+(d*d*d); 

            n=n/10;        

        }

        if(s==a)

        System.out.print("The number is armstrong number");

        else

        System.out.print("The number is not a armstrong number");

   

    }

}

7.      Write a program to find all two digit spy number.

import java.util.*;

class spy

{

    void main()

    {

        int i, a, d=0, s=0, p=1;

        for(i=100;i<=999;i++)      

        {

            a=i;               

            while(a>0)      

            {

                d=a%10;     

                s=s+d;         

                p=p*d;         

                a=a/10;       

            }

            if(s==p)

            System.out.print(" "+i);

            s=0;

            p=1;

        }

    }

}

8.      Write a program to find the number is tech number or not.

import java.util.*;

class tech

{

    public static void main()

    {

        int a, d=0, c=1,r=0,q=0,sq=0,n;

        double u=0.0;

        Scanner ob=new Scanner (System.in);

        System.out.print("Enter the number ");

        a=ob.nextInt();    

        u=Math.sqrt(a);   

        sq=(int)u;         

        n=sq;

        while(sq>0)

        {

            d=sq%10;

            c=c*10;    

            sq=sq/10;

        }

        r=a%c;    

        q=a/c;     

        if((r+q)==n)

        System.out.print("The number is tech");

        else

        System.out.print("The number is not a tech");

    }

}

9.      Write a program to find the number is palindrome number or not.

import java.util.*;

class palandromic

{

    void main()

    {

        int a, d=0, r=0, n;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");

        a=ob.nextInt();    

        n=a;               

        while(n>0)         

        {

            d=n%10;        

            r=(r*10)+d;    

            n=n/10;        

        }

        if(r==a)

        System.out.print("The number is palandromic number");

        else

        System.out.print("The number is not a palandromic number");

    }

}

10.  Write a program to find the number is special number or not.

import java.util.*;

class special_number

{

    void main()

    {

        int a, n, d=0, f=1, i, s=0;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter the number");

        a=ob.nextInt();   

        n=a;               

        while(n>0)        

        {

            d=n%10;       

            for(i=d;i>=1;i--) 

            {

                f=f*i;      

            }

            s=s+f;         

            n=n/10;        

            f=1;

        }

        if(s==a)

        System.out.println("The number is special number");

        else

        System.out.println("The number is not a special number");

    }

}

11.  Write a program to convert the number into binary number.

import java.util.*;

class binary

{

    void main()

    {

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");

        int n=ob.nextInt();    

        int d=0, r=0;

        while(n>0)              

        {

            d=n%10;            

            r=r*10+d;          

            n=n/10;            

        }

        System.out.print("The reverse number is "+r);

    }

}


12. Write a program to find the number is palandromic or not using function.

 import java.util.*;  

class Palin 

{  

    int num;  

    int revnum;  

    Palin() 

    {  

        num = 0; 

        revnum = 0;  

    }

    void accept()  

    {  

        Scanner ob = new  Scanner(System.in);

        System.out.print("Enter the Number:");

        num = ob.nextInt();         

    }  

    int reverse(int y)              

    {  

        int d=0;

        while(y>0)                 

        {

            d=y%10;               

            revnum=revnum*10+d;     

            y=y/10;                 

        }

        return(revnum);         

    }  

    void check() 

    {  

        revnum = reverse(num);  

        if(num == revnum)  

        {

            System.out.println("Nrnnber is palindrome");

        } 

        else

        {  

            System.out.println("Number is not palindrome");  

        }  

    }  

    public static void main(String args[]) 

    {  

        Palin p = new Palin();  

        p.accept();  

        p.check();  

    }  

}           

13. Write a program to find the number is Smit number or not.

import java.util.Scanner;

class SmithNumber

{

    void main()

    {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter number: ");

        int n = in.nextInt();

        if (n <= 0)

        {

            System.out.println(n + " is not a Smith Number.");

            return;

        }

        boolean isComposite = false;

        for (int i = 2; i < n; i++)         //2 3 4 5 6

        {

            if (n % i == 0) 

            {

                isComposite = true;

                break;

            }

        }


        if (isComposite && n != 1)

        {

            int sumDigits = 0;

            int t = n;

            while (t != 0) 

            {

                int d = t % 10;

                sumDigits += d;         //7

                t /= 10;

            }

            int sumPrimeDigits = 0;

            t = n;

            for(int i = 2; i < t; i++)  //2 3 4 5 6

            {

                while(t % i == 0) //2 3 4 6 

                {

                    t =t/ i;

                    int temp = i;

                    while (temp != 0)

                    {

                        int d = temp % 10;

                        sumPrimeDigits += d;

                        temp /= 10;

                    }

                }

            }

            if(t > 2) 

            {

                while (t != 0) 

                {

                    int d = t % 10;

                    sumPrimeDigits += d;

                    t /= 10;

                }

            }

            if (sumPrimeDigits == sumDigits)

                System.out.println(n + " is a Smith Number.");

            else

                System.out.println(n + " is not a Smith Number.");

        }

        else 

        {

            System.out.println(n + " is not a Smith Number.");

        }

    }

}

14. Write a program to find the number is Duck number or not.

import java.util.*;

class duck

{

    void main()

    {

        int a, d=0, c=0, n, r=0;

        Scanner ob=new Scanner(System.in);

        System.out.print("Enter the number");   

        a=ob.nextInt(); 

        n=a;

        while(a>0)          

        {

            d=a%10;

            r=r*10+d;

            if(d==0)

                c++;

            a=a/10;        

        }

        if(r%10==0 || c==0)

        System.out.print("The number not a duck number");

        else

        System.out.print("The number is a duck number");

    }

}


15. Write a program to print all prime palandromic number from 1 to 1000.


class prime_palandromic

{

    void main()

    {

        int i, j, c=0, r=0, d=0, n;

        for(i=1;i<=1000;i++)        

        {

            for(j=2;j<i;j++)      

            {

                if(i%j==0)          

                c++;               

            }

            if(c==0)

            {

                n=i;            

                while(n>0)    

                {

                    d=n%10;     

                    r=r*10+d;  

                    n=n/10;    

                }

                if(r==i)

                System.out.print(" "+i);        

            }

            c=0;

            r=0;

        }

    }

16. Write a program to find the number is superspy number or not.


import java.util.*;

class superspy

{

    void main()

    {

        int a, d=0, c=0, s=0;

        Scanner ob =new Scanner(System.in);

        System.out.println("Enter the number");

        a=ob.nextInt();         

        while(a>0)

        {

            d=a%10;    

            s=s+d;     

            c++;      

            a=a/10;     

        }

        if(c==s)       

        System.out.println("The number is superspy");

        else

        System.out.println("The number is not a superspy");

    }


}


17. Write a program to find the number is Evil number or not.

}import java.util.Scanner;

public class EvilNumber

{

     public static void main() 

     {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter a positive number: ");

        int n = in.nextInt();       

        if (n < 0) 

        {

            System.out.println("Invalid Input");

            return;

        }

        int count = 0;

        int p = 0;

        int binNum = 0;

        while (n > 0)                               

        {

            int d = n % 2;                          

            if (d == 1)                             

                count++;                            

            binNum=binNum+ (int)(d * Math.pow(10, p));   

            p++;                                    

            n /= 2;                                 

        }

        System.out.println("Binary Equivalent: " + binNum);

        System.out.println("No. of 1's: " + count);

        if (count % 2 == 0)

            System.out.println("Output: Evil Number");

        else

            System.out.println("Output: Not an Evil Number");

    }

}

18. Write a program to find the number is trangular number or not.

import java.util.*;

class trangular_number

{

    void main()

    {

        int n, i, s=0, p=0;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter the number");

        n=ob.nextInt();     //10

        for(i=1;i<=n;i++)   //1 2 3 4 5 6 7 8 9 10

        {

            s=s+i;          //1+2+3+4

            if(s==n)        //

            {

                p=1;

                break;

            }

        }

        if(p==1)

        System.out.println("The number is trangular number");

        else

        System.out.println("The number is not a trangular number");

    }

}


19. Write a program to find the number is Bouncy number or not.

import java.util.Scanner;

public class BouncyNumber

{

    public static void main()

    {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int n = in.nextInt();      

        if (n < 100) 

        {

            System.out.println(n + " is not a Bouncy Number.");

            return;

        }

        int t = n; 

        boolean isIncreasing = true, isDecreasing = true;

        int prev = t % 10;

        while (t>0) 

        {

            int d = t % 10;     

            if (d > prev) 

            {

                isIncreasing = false;       

                break;

            }

            prev = d;   

            t /= 10;   

        }

        t = n;

        prev = t % 10;

        while (t != 0)

        {

            int d = t % 10;

            if (d < prev) 

            {

                isDecreasing = false;

                break;

            }

            prev = d;

            t /= 10;

        }

        if (!isIncreasing && !isDecreasing)

            System.out.println(n + " is a Bouncy Number.");

        else

            System.out.println(n + " is not a Bouncy Number.");

    }

}

20. Write a program to find the number is Fascinating number or not.

import java.util.Scanner;

public class FascinatingNo

{

    public static void main()

    {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter the number to check: ");

        int num = in.nextInt();//192

        if (num < 100) 

        {

            System.out.println(num + " is not a Fascinating Number");

            return;

        }

        int num2 = num * 2; //192*2 =384

        int num3 = num * 3; //192*3 = 576

        boolean isFascinating = true;

        String str = "" + num + num2 + num3; // str=" 192384526"

        for (char i = '1'; i <= '9'; i++)   //i=1

        {

            int idx1 = str.indexOf(i);  //1

            int idx2 = str.lastIndexOf(i);//1

            if (idx1 == -1 || idx1 != idx2)

            {

                isFascinating = false;

                break;

            }

        }

        if (isFascinating)

            System.out.println(num + " is a Fascinating Number");

        else

            System.out.println(num + " is not a Fascinating Number");

    }

}

No comments:

Post a Comment