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

No comments: