Watch java videos

The sum of the number’s digits calculated until the sum equal 1. If it does equal to 1, then it is a magic number. Here is the code to solve the program recursively.
Example 10 is a magic number. Because 1+0=1.

import java.io.*;
class Magic
{
    int num;
    public static int sumdig(int n)
    {
        if(n==0)
        {
            return 0;
        }
        else
        {
            int rem=n%10;
            return (rem+sumdig(n/10));
        }
    }

    void isMagic()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ENTER A NUMBER");
        num=Integer.parseInt(br.readLine());
        while(num>9)
        {
            num=sumdig(num);
        }
        if(num==1)
        {
            System.out.println("A MAGIC NUMBER");
        }
        else
        {
            System.out.println("NOT A MAGIC NUMBER");
        }
    }

    public static void main()throws IOException
    {
        Magic obj=new Magic();
        obj.isMagic();
    }
}
magic number

Download free Sample papers

LEAVE A REPLY

Please enter your comment!
Please enter your name here