阿姆斯特朗数

Armstrong number is a n digit number such that the sum of digits raised to the power n is equal to the number.

阿姆斯壮数字是一个数字,使得加到幂n的数字之和等于该数字。

For example:

例如:

153 is armstrong number because here n=3 and 13+53+33=153.

153是阿姆斯壮数,因为这里n = 3和13 + 53 + 33 = 153。

120 is not armstrong number because 13+23+03!=120.

120不是阿姆斯壮数字,因为13 + 23 + 03!= 120。

Below I have shared a Java program that checks whether a number is armstrong number or not.

在下面,我共享了一个Java程序,该程序检查数字是否为armstrong数字。

Java中的阿姆斯特朗编号程序 (Program for Armstrong Number in Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner; //import Scanner class for reading from keyboard
class ArmstrongNumberExample
{
public static void main(String...s)
{
Scanner sc=new Scanner(System.in);
int num,len,sum=0,temp,rem;
System.out.println("Enter a number:");
num=sc.nextInt();
temp=num;
len=String.valueOf(num).length();
while(temp!=0)
{
rem=temp%10;
sum+=(int)Math.pow(rem,len);
temp/=10;
}
if(sum==num)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong Number");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java . util . Scanner ; //import Scanner class for reading from keyboard
class ArmstrongNumberExample
{
public static void main ( String . . . s )
{
Scanner sc = new Scanner ( System . in ) ;
int num , len , sum = 0 , temp , rem ;
System . out . println ( "Enter a number:" ) ;
num = sc . nextInt ( ) ;
temp = num ;
len = String . valueOf ( num ) . length ( ) ;
while ( temp != 0 )
{
rem = temp % 10 ;
sum += ( int ) Math . pow ( rem , len ) ;
temp /= 10 ;
}
if ( sum == num )
System . out . println ( "Armstrong Number" ) ;
else
System . out . println ( "Not Armstrong Number" ) ;
}
}

Output

输出量

阿姆斯特朗数_Java中的阿姆斯特朗编号程序

翻译自: https://www.thecrazyprogrammer.com/2015/08/program-for-armstrong-number-in-java.html

阿姆斯特朗数

相关文章:

  • 2021-05-29
  • 2022-01-10
  • 2021-12-05
  • 2021-07-07
  • 2021-10-06
  • 2021-05-22
  • 2021-12-26
猜你喜欢
  • 2021-10-19
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-11-15
  • 2021-10-13
相关资源
相似解决方案