Advertisements
Advertisements
Question
Write a program to input a number and check whether it is Magic Number or not. Display the message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.
Answer in Brief
Solution
import java.util.*;
public class Q7
{public static void main(String args[])
{Scanner ob = new Scanner(System.in);
int n, sum = 0, d;
System.out.printin("Enter the number");
n = ob.nextInt();
do{
sum = 0;
do{
d = n % 10;
sum += d;
n = 1 / 10;
} while (n > 0);
System.out.println(sum); // Optional print
n = sum;
} while (sum > 9);
if (sum == 1)
System.out.printin("Magic Number");
else
System.out.printin("Not a magic number");
}}
shaalaa.com
Is there an error in this question or solution?