Saturday, November 16, 2013

Display Armstrong Numbers From A List Of Entered Values



In this BlueJ program, user has to enter number of values and the program will show the armstrong numbers among the entered values.

Here is the program:

import java.io.*;
class A

{
int arr[];
int n,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
 System.out.println("How many elements you want to store:");
 n=Integer.parseInt(br.readLine());
 arr=new int [n];
 for(i=0;i< n;i++)
 {
 System.out.println("Enter value:");
 arr[i]=Integer.parseInt(br.readLine());
 }
 System.out.println("The armstrong numbers are:");
 for(i=0;i< n;i++)
 {
  if(armstrong(arr[i]))
  System.out.print(" "+arr[i]);
  }
  }
 private boolean armstrong(int x)
 {
  int j,sum=0;
  j=x;
  while(x>0)
  {
   sum+=Math.pow(x%10,3);
   x=x/10;
   }
   if(j==sum)
   return true;
   else
   return false;
   }
public static void main(String args[])throws Exception
{
 A ob=new A();
 ob.take();
 }
}

Related PostBlueJ Programs on Number

Here the numbers are stored in an array ‘arr’ and then the numbers are passed to another user defined function ‘private boolean armstrong (int)’  which returns ‘true’ if the passed argument is an Armstrong number otherwise it returns ‘false’.

Please visit this site, it may be helpful

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner