Showing posts with label Magic Number. Show all posts
Showing posts with label Magic Number. Show all posts

Magic Number

 Magic number is the if the sum of its digits recursively are calculated till a single digit If the single digit is 1 then the number is a magic numberMagic number is very similar with Happy Number. 2+2+6=10 sum of digits is 10 then again 1+0=1 now we get a single digit number is 1.

import java.util.*;

class magic

{ //class magic opens

    public static void main(String[]args)

    { //main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no

        int r=0,sum=0,k=0;//variable r to extract the digits,variable sum and k to store the sum

        sum=n;

        while(sum>9)

        {

            k=sum;

            sum=0;

            while(k>0)

            {

                r=k%10;

                sum=sum+r;

                k=k/10;

            }

        }

        if(sum==1)

        System.out.println(n+" is a magic no");

        else

        System.out.println(n+" is not a magic no");

    }//main method closes

}//class closes


Palindrome Number

Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. It can also be a string like LOL, MADAM etc.

 import java.util.*;

class pallinodrome

{      //class pallinodrome opens

    public static void main(String[]args)

    {  //main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n accepts the no

        int r=0,rev=0,k=n;//variable r to extract the digits,variable rev to reverse the no,variable k to store the no

        while(n>0)

        {

            r=n%10;

            rev=(rev*10)+r;

            n=n/10;

        }

        if(k==rev)

        System.out.println(k+" is a pallinodrome no");

        else

        System.out.println(k+" is not a pallinodrome no");

    }//main method closes

}//class closes


Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...