Skip to main content

What is magic number in java ?

A number is said to be magic number if the repetitive sum of digits of number is equal to 1 or a single digit number . If the repetitive sum ends up  as 1 then the number is a magic number, else not.
For example-
n= 289;.   2+8+9 = 19
     19;.      1+9 = 10
     10;.      1+0= 0
      1
Since the number ends in 1, hence 289 is a magic number.

Magic number in java is the java program to check whether a number is magic number or not .
For this we do repetitive addition of the digits of the number till the number is a two or more digit number.
If after repetitive adding digits of the number ends up to 1 then the loop stops and a message is displayed.

For this program we need to extract digits of the number for this we use while loop and add this digit .
Code snippet to get digits of the number -
while(n!=0)
{
       int d = n%10;
        s=s+d;
       n=n/10;
}


ALGORITHM-
STEP 1: start
STEP 2: print "enter a number"
STEP 3: taking input of integer in n
STEP 4: repeat STEP 5 to STEP 10 until n>9
STEP 5: int s=0
STEP 6: repeat STEP 7 to STEP 9 until n!=0
STEP 7: int d=n%10
STEP 8: s=s+d
STEP 9: n=n/10
STEP 10:n=s
STEP 11:if n==1 then goto STEP 12 else goto STEP 13
STEP 12:print "it is a magic number
STEP 13:print " it is not a magic number
STEP 14:end



Java code

import java.util.*;
public class magic
{
public static void main()
{
Scanner sc=new Scanner(System.in);
  System.out.println("enter a number");
  int n=sc.nextInt();
while(n>9)
{
int s=0;
while(n!=0)
{
int d=n%10;
s=s+d;
n=n/10;
}
n=s;
}
if(n==1)
{
System.out.println("it is a magic number");
}
else
{
System.out.println("it is not a magic number");
 }
}
}

Output-

enter a number
289
It is a magic number

similar java codes
if you find helpful please share 

Comments

Popular posts from this blog

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 minute break .once all the cha

disarium number in java

A number is said to be disarium if the sum of its digit powered with their positions is equal to the number itself. EXAMPLE- 135 = (1^1) + (3^2) + (5^3)                             = 1 + 9 + 125                             = 135 A simple java program to check disarium number can be made by firstly calculating the length of the number and then adding digits of the number raised to the power of its position in the number. ALGORITHM - STEP 1 : Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int l= Integer.toString().length() STEP 5 : int no=n STEP 6 : int s=0 STEP 7 : repeat STEP 8 to STEP 11 until (no!=0) STEP 8 : int d=no%10 STEP 9 : s=s+(int)(Math.pow(d,l) STEP 10 :no=no/10 STEP 11 : l-- STEP 12 : if  (s==n) then goto STEP 13 else goto STEP 14 STEP 13 : print "the number is disarium" STEP 14 : print "the number is not disarium" STEP 15 : end JAVA CODE- import java.util.*; public class disarium {     public static void m