Computer Science, asked by sushmita1233, 11 months ago

Wap to enter a number and check if it is a magic number or not

Using java and function parameter

Don't use scanner class ​

Answers

Answered by amannishad0512p5zxh6
3

Definition of Magic Number

A number is said to be a Magic number if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits.If the single digit comes to be 1 then the number is a magic number.

Example- 199 is a magic number as 1+9+9=19 but 19 is not a sigle digit number so 1+9=10 and then 1+0=1 which is a single digit number and also 1.Hence it is a magic number.

class Magic_Number

{

public int magic(int t)

{

int sum=0,num=t,s;

while(num>9)

{

sum=num;

s=0;

while(sum!=0)

{

s=s+(sum%10);

sum=sum/10;

}

num=s;

}

return (num);

}

public static void main(int n)

{

Magic_Number ob=new Magic_Number(System.in);

int check=on.magic(n);

if(check==1)

{

System.out.println(n+" is a Magic Number.");

}

else

{

System.out.println(n+" is not a Magic Number.");

}

}

}

Hope it will help you,my friend Sushmita.


amannishad0512p5zxh6: what is it
amannishad0512p5zxh6: In U.P.
sushmita1233: by the way why u have used return
amannishad0512p5zxh6: in program
sushmita1233: yea
amannishad0512p5zxh6: to return the value back to check
amannishad0512p5zxh6: to return the value back to check
sushmita1233: ok
amannishad0512p5zxh6: Kanpur
amannishad0512p5zxh6: i belong
Similar questions