Magic Number
Problem to find the Magic Number or Not.
Magic Number is to sum the each and every digit and continue the same process until length become one.If value is "1" that is Magic Number.
Example-1:
Input : n = 1234 Output : Magic number Explain : 1 + 2 + 3 + 4 = 10 1 + 0 = 1[So its Magic Number]
Example-2:
Input : n = 12345 Output : Not a Magic Number Explain : 1 + 2 + 3 + 4 + 5 = 15 1 + 5 = 6[So its not Magic Number]
Solution
public class Main { public static void main(String [] args) { int num = 1234; int sum = 0; if(num % 9 == 0 && num != 0){ sum = 9; } else{ sum = num % 9; } if(sum == 1){ System.out.print("Magic Number"); } else{ System.out.print("Not a Magic Number"); } } }
num = 1234 if(num % 9 == 0 and num != 0): k = 9 else: k = num % 9 if(k==1): print("Magic number") else: print("Not a Magic number")
Output
Magic Number