Sunday, January 9, 2011

Armstrong Number Checking Program In BlueJ

What is Armstrong number?

From any number, the digits are to be separated and sum of the digit to the power number of digits of each digit is to be calculated. If the sum and number matches then the number is an Armstrong number.

For example if we enter 134 as the number then the number of digit in the number is 3. The sum would be 1^3+3^3+4^3 which is not equal to 134, so 134 is not an Armstrong number. Again if the number is 1234 then the sum would be 1^4+2^4+3^4+4^4. 153 is an Armstrong number as 1^3+5^3+3^3 equals to 153.

It is found that students always use 3 as the power but it is not correct way of checking Armstrong number although 3 digit numbers are Armstrong number only.

Codes of the Armstrong number checking program using 3 as power value

import java.io.*;
class Armstrong
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p,sum=0,c,spy=0,i;

public void takeData() throws IOException
{
System.out.println("Enter the number:-");
i=Integer.parseInt(br.readLine());
p=i;
while(p>9)
{
spy=1;
c=p%10;
sum=sum+(int)Math.pow(c,3);
p=p/10;
}
if(spy==1)
{
sum=sum+(int)Math.pow(p,3);
if(sum==i)
System.out.println(i+ " is amstrong number");
else
System.out.println("Not amstrong number.");
}
else
System.out.println("Not possible.");
}
public static void main(String args[])throws IOException
{
Armstrong obj=new Armstrong();
obj.takeData();
}
}

Now I will do the same program using number of digits of the number as power value.

Variable description of the Armstrong number checking program

BufferedReader br is used to take the number from user.
int p is a variable is used as loop control variable in this program.
int digit counts the number of digits in the entered number
int sum stores the sum of the digit to the power number of digits value.
int c holds the individual digits of the number.
int spy is used for checking purpose, whether the number is single digit or not.
int i holds the original number entered by user.

How to proceed in the program

Firstly the number is stored in variable ‘i’ and using do while loop the number of digits is counted and stored in variable ‘digit’.
The second do while loop is used to extract the digits of the number one by one and the power value is stored in the variable ‘sum’. At the end of the loop, values of ‘sum’ and the number is compared to reach the conclusion.

Modified codes of the same program on Armstrong number checking


import java.io.*;
class Armstrong
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p,digit=0,sum=0,c,spy=0,i;

public void takeNumber() throws IOException
{
System.out.println("Enter the number:-");
i=Integer.parseInt(br.readLine());
p=i;
do
{
digit++;
p=p/10;
}while(p!=0);
p=i;
do
{
spy=1;
c=p%10;
sum=sum+(int)Math.pow(c,digit);
p=p/10;
}while(p!=0);
if(spy==1)
{
if(sum==i)
System.out.println(i+ " is amstrong number");
else
System.out.println("Not amstrong number.");
}
else
System.out.println("Not possible.");
}
public static void main(String args[])throws IOException
{
Armstrong obj=new Armstrong();
obj.takeNumber();
}
}

Related PostBlueJ Programs on Number

19 comments:

  1. same to you. If my programs help anybody its a great pleasure for me.

    ReplyDelete
  2. Can u give me a program that would give me the sum of digits of a user specified integer.

    ReplyDelete
  3. class S
    {
    int sum,i;
    public void show(int n)
    {
    sum=0;
    for( i=n; i > 0; i=i/10)

    {
    sum=sum+i%10;
    }
    System.out.println(" Sum of the digits=" + sum);
    }
    }

    ReplyDelete
  4. can u please give the code for binary recursive search and find the position of the number found in the array....

    ReplyDelete
  5. Please check my post on 19 August 2011

    ReplyDelete
  6. can any one give me the prog to check for an armstrong no using for ..... in blue java

    ReplyDelete
  7. Program on armstrong number is already posted in my site. Please search it.

    ReplyDelete
  8. Can u give me program such that if user enters 5
    then output should be=12345 and if example 7 output=1234567

    ReplyDelete
  9. import java.io.*;
    class Series
    {
    int i,n;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public void show() throws Exception
    {
    System.out.println("Enter the number:");
    n=Integer.parseInt(br.readLine());
    for(i=1;i< =n;i++)
    System.out.print(i);
    }
    public static void main(String args[])throws Exception
    {
    Series ob=new Series();
    ob.show();
    }
    }

    ReplyDelete
  10. thank u
    can u give me a program for the patteren below
    a
    a a
    a a a
    a a a a
    a a a
    a a
    a

    ReplyDelete
    Replies
    1. class Arr
      {
      int i,j,x=1;
      public void show()
      {
      for (i=0;i<7;i++)
      {
      for(j=0;j=7/2)
      x--;
      else
      x++;
      System.out.println();
      }
      }
      }

      Delete
  11. thankyoi sir. ur site gives me a lot more confidence :)

    ReplyDelete
  12. can you give me a program to take the input size of array and then take the input for those many numbers and print the Amstrong numbers?

    ReplyDelete
  13. can u pls give a program to check a no.which is Armstrong no. or not using bufferedReader & using while loop

    ReplyDelete
  14. Can you give me a program to extract the first digit of a number..
    Eg: 23456
    Output: 2

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner