使用java检查数字是否是阿姆斯特朗数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22895936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:45:54  来源:igfitidea点击:

To check if a number is Armstrong number using java

java

提问by user3503832

this is my program:

这是我的程序:

public class ArmstrongNumber {

    public static void main(String args[]) {
        int n = 0, temp = 0, r = 0, s = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number ");
        if (in.hasNextInt()) {
            n = in.nextInt(); // if there is another number  
        } else {
            n = 0;
        }
        temp = n;
        while (n != 0) {
            r = n % 10;
            s = s + (r * r * r);
            n = n / 10;
        }

        if (temp == s) {
            System.out.println(n + " is an Armstrong Number");
        } else {
            System.out.println(n + " is not an Armstrong Number");
        }
    }
}  

output:

输出:

Exception in thread "main"java.lang.NoClassDefFoundError

线程异常 "main"java.lang.NoClassDefFoundError

I tried it using DataInputStreambut still getting same error.

我尝试使用DataInputStream但仍然出现相同的错误。

采纳答案by dev2d

you need to set CLASS_PATHvariable and point it to where ever your class file is

您需要设置CLASS_PATH变量并将其指向类文件所在的位置

then this should work

那么这应该工作

I have tried it locally, refer my answerto check how to set class path and how to compile and run java code using command prompt

我在本地试过了,参考我的答案来检查如何设置类路径以及如何使用命令提示符编译和运行 java 代码

回答by Nusrat Ahmed Asha

import java.util.Scanner;

/* a number is armstrong if the sum of cubes if individual digits of 
a number is equal to the number itself.for example, 371 is 
an armstrong number. 3^3+7^3+1^3=371.
some others are 153,370,407 etc.*/

public class ArmstrongNumber {

    public static void main(String args[]) {

        int input, store, output=0, modolus;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a number for ckecking.");
        input = in.nextInt();
        store = input;
        while(input != 0) {
            modolus = input % 10;
            output = output + (modolus * modolus * modolus);
            input = input / 10;
        }
        System.out.println(output);

        if(store == output) {
            System.out.println("This is an armstrong number.");
        } else {
            System.out.println("This is not an armstrong number.");
        }
        in.close();
    }
}

回答by sort_01out

// To check the given no is Armstrong number (Java Code)

// 检查给定的 no 是 Armstrong 数(Java 代码)

class CheckArmStrong{
public static void main(String str[]){
    int n=153,a, b=0, c=n;
    while(n>0){
              a=n%10;  n=n/10;  b=b+(a*a*a);
              System.out.println(a+"  "+n+"  "+b);  // to see the logic
            }
    if(c==b)  System.out.println("Armstrong number");
    else      System.out.println(" Not Armstrong number");
    }
}

回答by srikanth r

import java.util.Scanner;

public class AmstrongNumber {

    public static void main(String[] args) {

        System.out.println("Enter the number");
        Scanner scan=new Scanner(System.in);
        int x=scan.nextInt();
        int temp2=0;

        String s1 = Integer.toString(x);
        int[] a = new int[s1.length()];
        int[] a1 = new int[s1.length()];
        for (int i = 0; i < s1.length(); i++){
            a[i] = s1.charAt(i)- '0';
            int temp1=a[i];
            a1[i]=temp1*temp1*temp1;

        }
        for (int i = 0; i < s1.length(); i++){
            temp2=temp2+a1[i];
            if(i==s1.length()-1){
                if(x==temp2){
                    System.out.println("Amstrong num");
                }else{
                    System.out.println("Not !");
                }
            }
        }


    }

}

回答by Md_Zubair Ahmed

//This is my program to check whether the number is armstrong or not!!

//这是我检查号码是否为armstrong的程序!!

package myprogram2;
public class Myprogram2 {

    public static void main(String[] args) 
    {
        String No="407";
        int length_no=No.length();
        char[] S=new char[length_no];
        int[] b = new int[length_no];
        int arm=0;
        for(int i=0;i<length_no;i++)
        {
           S[i]=No.charAt(i);
           b[i]=Character.getNumericValue(S[i]);
           //System.out.print(b[i]);
           arm=arm + (b[i]*b[i]*b[i]);
           System.out.println(arm);
        }
        //System.out.println(" is the number \n now Checking for its Armstrong condition");
        int orgno = Integer.parseInt(No);
        if (orgno==arm)
            System.out.println("YESm its an armstrong");
        else
            System.out.println("\n<<Not an armstrong>>");
            //System.out.println(length_no);
        System.out.println("Original number is  "+orgno);
        System.out.println("Sum of cubes  "+arm);
    }

}

回答by DSlomer64

There are a couple of nice String-based solutions and numeric solutions with single-letter variable names.

有几个基于 niceString的解决方案和带有单字母变量名称的数字解决方案。

Consider this to make sense of how it works numerically, which includes a couple of interesting numeric tricks:

考虑一下它是如何在数字上工作的,其中包括一些有趣的数字技巧:

import java.io.*;

public class Armstrong
{
  public static void main(String args[]) throws IOException
  {
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader    in   = new BufferedReader(read);
    int modifiedNumber, originalNumber, modifiedNumberWithUnitsDigitZero, 
        unitsDigit, runningSum; 

    System.out.println("Enter your number:");
    modifiedNumber = Integer.parseInt(in.readLine()); 

    runningSum = 0;
    originalNumber = modifiedNumber;

    while(modifiedNumber > 0)
    {
      modifiedNumberWithUnitsDigitZero = modifiedNumber / 10 * 10;
      unitsDigit     = modifiedNumber - modifiedNumberWithUnitsDigitZero;
      runningSum    += unitsDigit * unitsDigit * unitsDigit;
      modifiedNumber = modifiedNumber / 10;
    }

    System.out.println("The number " + originalNumber 
      + (originalNumber == runningSum ? " IS" : " is NOT") 
      + " an Armstrong number because sum of cubes of digits is " + runningSum);
  }
}

回答by Sekhar Ray

This is the simple logic for Armstrong number program :

这是阿姆斯壮数程序的简单逻辑:

for (int i = number; i > 0; i = i / 10)
{
    remainder = i % 10;
    sum = sum + remainder * remainder * remainder;
}

if(sum == number)
{
    System.out.println("\n" + number + " is an Armstrong Number\n");
}

Reference :

参考 :

http://topjavatutorial.com/java/java-programs/java-program-to-check-if-a-number-is-armstrong-number/

http://topjavatutorial.com/java/java-programs/java-program-to-check-if-a-number-is-armstrong-number/

回答by Manish Sharan

import java.util.Scanner;

public class Amst {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter The No. To Find ArmStrong Check");
        int i = sc.nextInt();
        int sum = 0;
        for(int j = i; j>0 ; j = j/10){
            sum = sum + ((j%10)*(j%10)*(j%10));
        }
        if(sum == i)
            System.out.println("Armstrong");
        else
            System.out.println("Not Armstrong");

    }
}

回答by Code Wines

import java.util.*; 
public class ArmstrongNumber 
{
    public static void main( String[] args ) 
    {
        int n = 0, temp = 0, r = 0, s = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number ");
        if (in.hasNextInt()) {
            n = in.nextInt(); // if there is another number  
        } else {
            n = 0;
        }
        temp = n;
        while (n != 0) {
            r = n % 10;
            s = s + (r * r * r);
            n = n / 10;
        }

        if (temp == s) {
            System.out.println(temp + " is an Armstrong Number");
        } else {
            System.out.println(temp + " is not an Armstrong Number");
        }
    }
 }
  1. You missed to import java.util package

  2. Change n to temp in S.O.P

  1. 你错过了导入 java.util 包

  2. 在 SOP 中将 n 更改为 temp

回答by Jijo

For 'N' digit amstrong number

对于 'N' 位 amstrong 数字

package jjtest;

public class Amstrong {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num=54748;
        int c=0;
        int temp=num;

        int b=1;
        int length = (int)(Math.log10(num)+1);
        while(num>0){   
            int r = num%10;
            num=num/10;
            int a =1;
            for(int i=1;i<=length;++i){
                 b=b*r;
        }
            c = c + b;
            b=1;
    }
        System.out.println(c);
        if(c==temp){
            System.out.println("its an amstrong number");
        }else{
            System.out.println("its not an amstrong number");
        }
    }
}