Thursday, January 30, 2014

Program - Digit Separation - I




Separating the digits of a given number. Print the sum of the digits and number of digits present in the given number.



import java.io.*;
    public class Digit_Sep
        {
            public static void main(String args[])throws IOException
                {
                    int n,sum=0,c=0;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a No. : ");
                    n=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tDigit in Number "+n+" is in reverse order ");
                    while(n>0)
                        {
                            int rem=n%10;
                            n/=10;
                            System.out.print(rem+" ");
                            sum=sum+rem;
                            c++;
                        }
                    System.out.print("\n\tNos. of Digits "+c+" an sum of the digits "+sum);   
               }
        }


Output


          Enter a No. : 1234

          Digit in Number 1234 is in reverse order 4 3 2 1
          Nos. of Digits 4 an sum of the digits 10



When sum of cube of each digits is eaual to the given number is known as Armstrong number. 153, 370, 371, 407


import java.io.*;
    public class Armstrong
        {
            public static void main(String args[])throws IOException
                {
                    int n;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a No. : ");
                    n=Integer.parseInt(Br.readLine());
                    Armstrong A=new Armstrong();
                    if(A.Sum(n)==n)
                        {
                            System.out.print("\n\t"+n+" is a Armstrong Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Armstrong Number");   
                        }

               }
            int Sum(int n)
                {
                    int s=0;
                    while(n>0)
                        {
                            s=s+(int)(Math.pow(n%10,3));
                            n/=10;

                        }
                    return s;
                }
        }



Output


          Enter a No. : 153
          153 is a Armstrong Number

          Enter a No. : 454
          454 is not a Armstrong Number


Palindromic number is a number reamins equal even when digits of the numbers are reversed. 11, 121, 232


import java.io.*;
    public class Palindrome
        {
            public static void main(String args[])throws IOException
                {
                    int n;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a No. : ");
                    n=Integer.parseInt(Br.readLine());
                    Palindrome P=new Palindrome();
                    if(P.isPalin(n)==n)
                        {
                            System.out.print("\n\t"+n+" is a Palindrome Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Palindrome Number");   
                        }

               }
            int isPalin(int n)
                {
                    int sum=0;
                    while(n>0)
                        {
                            sum=sum*10+n%10;
                            n/=10;

                        }
                    return sum;
                }
        }


Output


          Enter a No. : 121
          121 is a Palindrome Number

          Enter a No. : 321
          321 is not a Palindrome Number


When square of a number ends with the number itself, it is known as Automorphic number.   5, 6, 25, 76, 376


import java.io.*;
    public class Automophic
        {
            public static void main(String args[])throws IOException
                {
                    int n,c,Sq;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a No. : ");
                    n=Integer.parseInt(Br.readLine());
                    Automophic A=new Automophic();
                    c=A.Count(n);
                    Sq=(int)Math.pow(n,2);
                    if(n==Sq%(int)Math.pow(10,c))
                        {
                            System.out.print("\n\t"+n+" is a Automophic Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Automophic Number");   
                        }

               }
            int Count(int n)
                {
                    int c=0;
                    while(n>0)
                        {
                            c++;
                            n/=10;

                        }
                    return c;
                }
        }



Output


          Enter a No. : 25
          25 is a Automophic Number

          Enter a No. : 125
          125 is not a Automophic Number

Thursday, January 23, 2014

HCF / LCM





HCF and LCM by factorization



import java.io.*;
    public class HCF_LCM
        {
            public static void main()throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    HCF_LCM HL =new HCF_LCM();
                    HL.HCF(Math.max(num1,num2),Math.min(num1,num2));
                    HL.LCM(Math.max(num1,num2),Math.min(num1,num2));
                }
    void HCF(int a, int b)
        {
            int num=0;
            for(int i=1;i<=a;i++)
                {
                    if(a%i==0 && b%i==0)
                        {
                            num=i;
                        }
                }
            System.out.println("\n\tHighest Common Factor of "+a+" and "+b+" is = "+num);
        }
    void LCM(int a, int b)
        {
            int i;
            for(i=a;i<=a*b;i++)
                {
                    if(i%a==0 && i%b==0)
                        {
                            break;
                        }
                }
            System.out.println("\n\tLowest Common Factor of "+a+" and "+b+" is = "+i);
        }       
    }



Output



          Enter First No. : 5
          Enter Second No. : 32

          Highest Common Factor of 32 and 5 is = 1
          Lowest Common Factor of 32 and 5 is = 160



HCF by division Method



import java.io.*;
    public class GCD
        {
            public static void main(String args[])throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    GCD G=new GCD();
                    int result=G.Check(Math.max(num1,num2),Math.min(num1,num2));
                    System.out.println("\n\tGreatest Common Factor of "+num1+" and "+num2+" is = "+result);
                }
    int Check(int a, int b)
        {
           while(b>0)
            {
                int c=a%b;
                a=b;
                b=c;
            }
                return a;

        }

    }



Output


          Enter First No. : 8
          Enter Second No. : 18

          Greatest Common Factor of 8 and 18 is = 2



HCF by Recursive Function



import java.io.*;
    public class GCD_Recursive
        {
            public static void main(String args[])throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    GCD_Recursive G=new GCD_Recursive();
                    int result=G.Check(Math.max(num1,num2),Math.min(num1,num2));
                    System.out.println("\n\tGreatest Common Factor of "+num1+" and "+num2+" is = "+result);
                }
    int Check(int a, int b)
        {
           if(b>0)
            {
                return Check(b,a%b);
            }
           return a;
        }
    }



Output



          Enter First No. : 12
          Enter Second No. : 40

          Greatest Common Factor of 12 and 40 is = 4




Thursday, January 16, 2014

Powerful & Smith Number


Smith Number

Smith numbers are the numbers whose  sum of  digits is equal to the sum of the digits of its prime factor.
Example, 666  
prime factor (2,3,3,37) = 2+3+3+3+7 = 18
sum of digits 6 + 6 + 6 = 18.



//4, 22,  319, 346,  636,666,1086

import java.io.*;
    class Smith_No
        {
            public static void main(String args[])throws IOException
                {
                    Smith_No sm=new Smith_No();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n,num,s,sum=0,f,i=2;
                    System.out.print("\n\tEnter a Number : ");
                    n=Integer.parseInt(Br.readLine());
                    num=n;
                    s=sm.sep(n);
                    while(n>1)
                        {
                            f=0;
                            if(n%i==0)
                                {
                                    n=n/i;
                                    if(sm.prime(i))
                                        {
                                            sum=sum+sm.sep(i);;
                                        }    
                                }
                            else
                                {
                                    i++;
                                }
                            }       
                    if(s==sum)
                        {
                            System.out.println("\n\t"+num+" is a Smith Number"); 
                        }
                    else
                        {
                            System.out.println("\n\t"+num+" is not a Smith Number");
                        }
                }

           boolean prime(int n)
                {
                    int i;
                    for(i=2;i<=n/2;i++)
                        {
                            if(n%i==0)
                            return false;
                        }
                    return true;
                }

           int sep(int n)
                {
                    int s=0;
                    while(n>0)
                        {
                            s=s+n%10;
                            n=n/10;
                        }
                    return s;
                }
        }


Output

          Enter a Number : 666
          666 is a Smith Number

          Enter a Number : 456
          456 is Not a Powerful Number


Powerful Number


A powerful number is a positive integer that the square of each prime factor also be a factor of the number.
Example. 9
prime factor of 9 = 3 where 32 also a factor of the number
factor of 81 is 3,9 and 32 and 92 also facor of 81.



//4, 8, 9, 16, 100, 225, 243, 972, 1000

import java.io.*;
    public class Powerful_No
        {
            public static void main(String args[]) throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n,i=2,j;
                    Powerful_No PN=new Powerful_No();
                    boolean flag=true;
                    System.out.print("\n\tEnter a Number : ");
                    n=Integer.parseInt(Br.readLine());
                    for(j=2;j<=n/2;j++)
                        {
                            if(n%j==0)
                                {
                                    if(PN.isPrime(j))
                                        {
                                            if(n%(j*j)!=0)
                                                {
                                                    flag=false;
                                                    break;
                                                }
                                        }
                                }
                       }
                    if(flag)
                        {
                            System.out.print("\n\t"+n+" is a Powerful Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is Not a Powerful Number");
                        }
                }
   // Checking for Prime Numbers
  boolean isPrime(int n)
     {
       int i;
       for(i=2;i<=n/2;i++)
        {
          if(n%i==0)
            return false;
         }
       return true;
    }
  }


Output


          Enter a Number : 81
          81 is a Powerful Number


          Enter a Number : 38
          38 is Not a Powerful Number