Magic number | Magic number in java

 

Magic number | Magic number in java

Magic number | Magic number in java

A number is said to a magic number if the sum of its digit when calculated till a single digit is come recursively adding the summation of its digits and it is equal to 1.

Example, 19, 29, 91, 991, 226 etc

19 --> 1 + 9 = 10  = 1 + 0 = 1

91-->  9 + 1 = 10  = 1 + 0 =1

991-->  9 + 9 + 1 = 19 = 1 + 9  = 10 = 1 + 0 = 1

import java.util.Scanner;

// Magic Number check

public class MagicNumberCheck

{

    public static void main(String[] args)

    {

        int number;     // Number to check magic or not

        int rem = 1;    // reminder

        int num;        // store number

        int sum = 0;    // summation

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number to Check Magic Number: ");

        number = sc.nextInt();

        num = number;

        while (num > 9)

        {

            while (num > 0)

            {

                rem = num % 10;

                sum = sum + rem;

                num = num / 10;

            }

            num = sum;

            sum = 0;

        }

        if (num == 1)

        {

            System.out.println("Magic Number");

        }

        else

        {

            System.out.println("Not Magic Number");

        }

    }

}

Java program



SHARE THIS
Previous Post
Next Post