Java Program To Find Whether a Number is Armstrong

27 comments - Post a comment

/* Simple Java program to find whether a number is an armstrong number or not */


class Armstrong
{
int sumcube(int x)
{
int dig,sum = 0;
while(x >= 1)
{
dig = x % 10;
sum = sum + dig * dig * dig;
x = x / 10;
}
return(sum);
}
void display(int n)
{
if(sumcube (n)==n)
System.out.println(n + " is Armstrong Number");
else
System.out.println(n + " is not Armstrong Number");
}
void disp()
{
int x = 0,y = 0;
for(int k = x;k <= y;k++)
if(sumcube (k)==k)
System.out.println(k);
}
}

 
This Post has 27 Comments Add your own!
Unknown - January 6, 2009 at 7:48 PM

Thanks dude it really helped

bharti - February 9, 2009 at 3:32 PM

hey program suggested by you really works but can any body tell me whats wrong with this program,it is not showing me correct result.

import java.io.*;
class Armstrong{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int num,r,sum=0;
String number;
System.out.println("Enter any number");
number=in.readLine();
num=Integer.parseInt(number);
while(num!=0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}
if(sum==num)
System.out.println("given number is armstrong number");
else
System.out.println("given number is not an armstrong number");
}
}

Anonymous - April 24, 2009 at 10:41 PM

Hi i am Ashu. The program posted is good but I have an alternative program. Use this one !!

import java.io.* ;
public class armstrong
{
static BufferedReader stdin = new BufferedReader(new InputStreamreader(System.in)) ;
public static void IsArmstrong() throws IOException
{
int n, flag, orig ;
double sum = 0.0 , d ;
System.out.print("\n Enter the number : ");
n = Integer.parseInt(stdin.readLine());
orig = n ;
while(n>0)
{
d = n % 10 ;
sum = sum + Math.pow(d, 3);
n = n / 10 ;
}
if(sum == orig)
System.out.print("\n " + orig + " is an Armstong number.");
else
System.out.print("\n " + orig + " is not an Armstrong number.);
}
}

ved prakash - June 16, 2010 at 9:42 PM

here is the right one
public class Armstrong
{
public static void main(String[] argv)
{
int a,b,c=0,sum =0,k;
for(int i=1; i<=10000; i++)
{
a = b = i;
while(b!=0)
{
b = b/10;
c++;

}
while(a!=0)
{
k = a % 10;
sum = sum + (int)Math.pow(k,c);
a = a/10;
}
c=0;
if(sum==i)
{
System.out.println(i+" " + "is an armstrong number");
}
sum = 0;
}

}
}

Anonymous - July 14, 2010 at 1:34 AM

hii
i too have wriiten one prog for armstrong no. pls check for further modifying it.

import java.lang.Math;
class ano
{
public static void main ( String[] a )
{
int x=Integer.parseInt( a[0] );
double s=0; int y; int z;int i;



for(i=1; i<= a[0].length(); i++)
{
z= a[0].length();
y= x % 10;
x= x/10 ;
s= s + java.lang.Math.pow(y,z) ;
}

int p= (int)s;
System.out.println(s);
System.out.println(p);
System.out.println(x);

int q=Integer.parseInt( a[0] );

if( p==q)
{
System.out.println( " Number is armstrong ");
}
else
{
System.out.println( " Number is not armstrong ");
}
}
}

Anonymous - July 14, 2010 at 1:34 AM

hii
i too have wriiten one prog for armstrong no. pls check for further modifying it.

import java.lang.Math;
class ano
{
public static void main ( String[] a )
{
int x=Integer.parseInt( a[0] );
double s=0; int y; int z;int i;



for(i=1; i<= a[0].length(); i++)
{
z= a[0].length();
y= x % 10;
x= x/10 ;
s= s + java.lang.Math.pow(y,z) ;
}

int p= (int)s;
System.out.println(s);
System.out.println(p);
System.out.println(x);

int q=Integer.parseInt( a[0] );

if( p==q)
{
System.out.println( " Number is armstrong ");
}
else
{
System.out.println( " Number is not armstrong ");
}
}
}

Unknown - July 16, 2010 at 11:05 AM

i have error to run this program.this program do not has main function.

Anonymous - December 14, 2010 at 11:17 AM

its showing me error...

Anonymous - December 14, 2010 at 11:17 AM

its showing me error...

Anonymous - December 14, 2010 at 11:17 AM

its showing me error...

sathelli naveen - June 24, 2011 at 4:54 PM

import java.util.Scanner;

public class pro
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("pls enter the amstrong");
int d=s.nextInt();
int b=0;
int a=1;
for(int i=0;i<=1000;i++)
{
a=i*i*i;
b=b+a;
if(b==d)
{
System.out.println("asm");
System.exit(0);
}
}
System.out.println("not");
}
}

naveena - July 5, 2011 at 3:13 PM

tum logon ku kaaam dhanda nai hai re?! :P

sasidharan - July 17, 2011 at 7:07 AM

hi guys this is sasidharan doing my final year bca
programs must be simpler and easier to understand all the above programs have lengthy codings so i have written an easy coding to find armstrong numbers
PLS TRY IT !!!!!

PROGRAM:
class armstrong
{
public static void main(String args[])
{
int n=1000 , sum = 0,mod, m,i,a;
for(i=1;i<=n;i++)
{
mod = sum = 0;
a = i;
m = i;
do
{
mod = a % 10;
sum = sum + mod * mod * mod;
a = a/10;
}
while(a>0);
if(sum == m)
System.out.println(sum);
}
}
}

Anonymous - September 22, 2011 at 4:33 PM

hey i am Mukul


import java.io.*;
class Armstrong{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int num, r ,sum=0;
String number;
System.out.print("Enter any number : ");
number=in.readLine();
num=Integer.parseInt(number);
int orignum = num;

while(num!=0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}

if(sum==orignum){
System.out.println("given number " + orignum+ " is armstrong number");
}else
System.out.println("given number " + orignum+ " is not an armstrong number");
}
}

Anonymous - September 22, 2011 at 4:34 PM

import java.io.*;
class Armstrong{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int num, r ,sum=0;
String number;
System.out.print("Enter any number : ");
number=in.readLine();
num=Integer.parseInt(number);
int orignum = num;

while(num!=0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}

if(sum==orignum){
System.out.println("given number " + orignum+ " is armstrong number");
}else
System.out.println("given number " + orignum+ " is not an armstrong number");
}
}

Anonymous - September 22, 2011 at 4:35 PM

import java.io.*;
class Armstrong{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int num, r ,sum=0;
String number;
System.out.print("Enter any number : ");
number=in.readLine();
num=Integer.parseInt(number);
int orignum = num;

while(num!=0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}

if(sum==orignum){
System.out.println("given number " + orignum+ " is armstrong number");
}else
System.out.println("given number " + orignum+ " is not an armstrong number");
}
}

Anonymous - September 30, 2011 at 11:45 AM

please say me what's wrong with this program



import java.util.Scanner;
public class Armstrong
{
public static void main(String[] args)
{
int no,r,q,sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no");
no = Integer.parseInt(sc.next());
while(no>0)
{
r=no%10;
sum=sum+r*r*r;
no=no/10;
}
if(no==sum)
{
System.out.println("It's a armstrong no");
}
else
{
System.out.println("It's not a armstrong no");
}
}
}

VicKraNt TaNeJa - November 2, 2011 at 10:22 AM

class armstrng
{
public static void main(String[] arg)
{
int a=153,b,c=0;
int org = a;
while(a>0)
{
b=a%10;
c=c+( b*b*b);
a=a/10;
}

System.out.println("c:"+c);
if(org== c)
System.out.println("armstrong");
else

System.out.println("not armstrong");

}
}

Anonymous - November 2, 2011 at 10:23 AM

class armstrng
{
public static void main(String[] arg)
{
int a=153,b,c=0;
int org = a;
while(a>0)
{
b=a%10;
c=c+( b*b*b);
a=a/10;
}

System.out.println("c:"+c);
if(org== c)
System.out.println("armstrong");
else

System.out.println("not armstrong");

}
}
Autor
WicKed:boy

SysAdmin - November 23, 2011 at 10:45 PM

Here is the actual program which supports any number of digits..
Armstrong Number - Java Program

$@ty@' s - January 9, 2012 at 10:50 AM

hi this is satya
this is simple prog to find Amstrong number

class Amstrong
{
static int cube(int num){
return num*num*num;
}
static int SumofCubes(int num){
return cube(((num /10)/10)% 10) + cube((num / 10)%10)+cube(num%10);
}
static boolean isAmstrong(int num){
return num == SumofCubes(num);
}
static void listAmstrong(){
for(int num = 100; num<1000; num++)
if(isAmstrong(num))
System.out.println(num+" "+"is an Armstrong");
}
public static void main(String[] args)
{
listAmstrong();
}
}

$@ty@' s - January 9, 2012 at 10:51 AM

class Amstrong
{
static int cube(int num){
return num*num*num;
}
static int SumofCubes(int num){
return cube(((num /10)/10)% 10) + cube((num / 10)%10)+cube(num%10);
}
static boolean isAmstrong(int num){
return num == SumofCubes(num);
}
static void listAmstrong(){
for(int num = 100; num< 1000; num++)
if(isAmstrong(num))
System.out.println(num+" "+"is an Armstrong");
}
public static void main(String[] args)
{
listAmstrong();
}
}

Anonymous - February 27, 2012 at 10:10 PM

Hi i am Suman , a class 9 student .the problem with bharti's prog is with the num. Its value changes in the loop.
Try this out--
//to check whether a number is an armstrong number
import java.io.*;
class ArmstrongNumber
{
private int n,r,sum=0,temp;
private String s;
public void main(String args[])throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter a number: ");
s=br.readLine();
n=Integer.parseInt(s);
temp=n;
while(n!=0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==temp)
System.out.print(temp+" is an armstrong number");
else
System.out.print(temp+" is not an armstrong number");

}
}

java67 - August 31, 2012 at 8:23 AM

There are multiple way you can do this. Here is my way of finding Armstrong number in java

Abhirupa Mitra - October 1, 2012 at 9:56 PM

Hi,
I need help regarding a matter.
What is actually an armstrong number?

Is it the sum of the cubes of each digit or, the sum of the each digit raised to the number of digits

Abhirupa Mitra - October 1, 2012 at 9:59 PM

What is the accurate definition of an armstrong number?
Sum of the cubes of each digit or the sum of each digit raised to the number of digits?

Please help

Anonymous - October 3, 2012 at 4:45 PM

c program for armstrong number
#include
#include
void main()
int a,b,c,d=0,e,f;
printf("enter the number");
scanf("%d",&a);
a=e;
while (a>0)
{
b=a%10;
c=b*b*b;
f=d+c;
a=a/10;
}
if(e==f)
{
printf("it is armstrong number");
}
else
{
printf("it is not armstrong number");
}
getch();
}

Post a Comment