Given a starting number and the ending number the task is to find the all the Magic Number present in that given range and also to display it’s frequency.
Magic Number is a number whose eventual sum of digits is equal to 1.
Example 1: 19. 1+9 = 10. 1+0 = 1. Hence it is a Magic Number.
Java
import java.util.*;
public class MagicNumberRange
{
public static void main(String args[])
{
int start=0,end=0,frequency=0,r=0,s=0,temp=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Magic Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
s=i;
temp=i;
while(s>9)
{
s=0;
while(temp>0)
{
r=temp%10;
s=s+r;
temp=temp/10;
}
temp=s;
}
if(s==1)
{
System.out.println(i);
frequency++;
}
}
System.out.println("Frequency: "+frequency);
}
}Java