Thursday, February 27, 2014

Nested Loop - IV


//Magic Numbers : When the sum of digits of a number resultant to 1
//e.g. 19 = 1 + 9 = 10 = 1+ 0 = 1


import java.io.*;   
    class Magic_Happy
        {
            public static void main(String args[])throws IOException
                {
                    Magic_Happy MH=new Magic_Happy();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    boolean flag;
                    System.out.print("\n\tEnter a number ");
                    n=Integer.parseInt(Br.readLine());
                    if(MH.Magic(n))
                        {
                            System.out.print("\n\t"+n+" is a Magic Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Magic Number");
                        }                       
                    if(MH.Happy(n))
                        {
                            System.out.print("\n\t"+n+" is a Magic Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Magic Number");
                        }  
                }
            boolean Magic(int n)
                {
                    int m=n,s=0;
                    while(m>9)
                        {
                            s=0;
                            while(m!=0)
                                {
                                    s=s+m%10;
                                    m=m/10;
                                }
                            m=s;   
                        }
                    if(s==1)
                        {
                            return true;
                        }
                    return false;   
               }


//when sum of the squares of its digits of a number is = 1
//Happy Numbers : 28 = 22 + 82 = 4 + 64 = 68 = 62 + 82 =100 =1

            boolean Happy(int n)
                {
                    int m=n,s=0;
                    while(m>9)
                        {
                            s=0;
                            while(m!=0)
                                {
                                    s=s+(int)Math.pow(m%10,2);
                                    m=m/10;
                                }
                            m=s;   
                        }
                    if(s==1)
                        {
                            return true;
                        }
                    return false;   
               }
        }



Output



          Enter a number 19

          19 is a Magic Number
          19 is a Happy Number

          Enter a number 37

          37 is a Magic Number
          37 is not a Happy Number



// A composite number that the sum of the number's digits equals the sum of the digits of its distinct prime factors.

// hoax numbers are 22, 58, 84, 85, 94, 136, 160, 166, 202, 234,


import java.io.*;
    class hoax
    {
        int n;
        hoax()
            {}
        hoax(int a)
            {
                n=a;
            }

            int digitsum(int n1)
                {
                    int r,s=0;
                    while(n1>0)
                        {
                            r=n1%10;
                            s=s+r;
                            n1=n1/10;
                        }
                    return  s;
                }

            int prime(int n2)
                {
                    int m,f1=0;
                    for(m=2;m
                        {
                            if(n2%m==0)
                                {
                                    return 0;
                                }
                        }
                    return 1;
                }
            void main()throws IOException
                {
                    int i,j,s1=0;
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tHoax No : ");
                    int x=Integer.parseInt(br.readLine());
                    hoax ob=new hoax(x);
                    for(i=2;i
                        {
                            if(x%i==0 && ob.prime(i)==1)
                                {
                                    s1=s1+ob.digitsum(i);
                                }
                        }
                    if(s1==ob.digitsum(x))
                        {
                            System.out.println("\n\t"+x+" is a hoax number");
                        }
                    else
                        {
                            System.out.println("\n\t"+x+" is not a hoax number");
                        }
                }
            }    



Output



          Hoax No : 22

          22 is a hoax number
          Hoax No : 33

          33 is not a hoax number



No comments: