Sunday, April 28, 2024

BlueJ Program On Magic Number Checking


Magic numbers are those numbers where sum of the digits when converted into a single digit becomes 1.

Examples of some of the magic numbers are 100, 37, 55, 127 
100 while converted in to single digit becomes 1+0+0=1
37 while converted in to single digit becomes 3+7 = 10 = 1+0=1


import java.io.*;
class Max
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum;
public void show()throws Exception
{
System.out.println("Enter the number:");
a=Integer.parseInt(br.readLine());
while(true)
{
sum=0;
if(a<10)
break;
while(a>0)
{
 sum=sum+a%10;
 a=a/10;
}
a=sum;
sum=0;
}
if(a==1)
System.out.println("The number is a magic number.");
else
System.out.println("The number is not a magic number.");

}
public static void main(String args[])throws Exception
{
 Max ob=new Max();
 ob.show();
}
}

Related PostBlueJ Programs on Number

Download eBook on BlueJ 

Sample output:

Enter the number:
127
The number is a magic number.

Enter the number:
34
The number is not a magic number.


7 comments:

  1. hello sir!
    sir can you please give some expected programs(solved) for the isc 2012 practical. practical is on 24-02-2012.

    ReplyDelete
    Replies
    1. I have already posted expected programs in ISC 2012, please search.

      Delete
  2. how to convert a character to its ascii code??

    ReplyDelete
    Replies
    1. class Pattern
      {
      void show(char ch)
      {
      int i;
      i=(int)ch;
      System.out.println("Character="+ch + " and the ascii value value="+i);
      }
      }

      Delete
  3. thanx a lot 4 posting these programs!

    ReplyDelete
  4. Can u please post some pgms important for icse 2014 please sir it's a request

    ReplyDelete
    Replies
    1. Please go through: http://schooljava.blogspot.com/2014/02/last-minute-suggestion-for-icse-2014.html

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner