Magic Number Or Not? Find out in Java

 


What is a Magic Number?

A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number. 


Answer:


import java.util.Scanner;

public class MagicNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number to check magic number or not");
        int n = sc.nextInt();

        if(n%9==1){
            System.out.println("It is a Magic Number");
        } else{
            System.out.println("It is not a Magic Number");
        }

        sc.close();
    }
}

Enter the numbers to check whether Magic Number or not.

Comments

Popular posts from this blog

C program to find the greatest of three numbers with user input.

How to add to integers in C programming

Check whether a character Vowel or Consonant