산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

For 루프를 사용하는 JAVA 프로그램의 암스트롱 번호

암스트롱 번호란 무엇입니까?

암스트롱 숫자에서 개별 숫자의 거듭제곱의 합은 숫자 자체와 같습니다.

즉, 다음 방정식이 성립합니다.

xy..z = xn + yn+.....+ zn

n은 숫자의 자릿수입니다.

예를 들어 3자리 Armstrong 번호입니다.

370 = 33 + 73 + o3
 = 27 + 343 + 0
 = 370

암스트롱 숫자의 예

 0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.

이것을 프로그램으로 작성해 봅시다:

숫자가 암스트롱 번호인지 확인하는 자바 프로그램

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;
 
public class ArmstrongNumber {
 
	public static void main(String[] args) {
		
		 int inputArmstrongNumber = 153; //Input number to check armstrong  
		 int tempNumber, digit, digitCubeSum = 0;
 
	       tempNumber = inputArmstrongNumber;
	        while (tempNumber != 0)
	        {
	        	
	        	/* On each iteration, remainder is powered by thetempNumber of digits n
	        	 */
	            System.out.println("Current Number is "+tempNumber);
	            digit =tempNumber % 10;
				System.out.println("Current Digit is "+digit);
	            //sum of cubes of each digits is equal to thetempNumber itself
	            digitCubeSum = digitCubeSum + digit*digit*digit;
				System.out.println("Current digitCubeSum is "+digitCubeSum);
	            tempNumber /= 10;
	           
	        }
 
	        //check giventempNumber and digitCubeSum is equal to or not 
	        if(digitCubeSum == inputArmstrongNumber)
	            System.out.println(inputArmstrongNumber + " is an Armstrong Number");
	        else
	            System.out.println(inputArmstrongNumber + " is not an Armstrong Number");
 
	}
 
}

출력

Current Number is 153
Current Digit is 3
Current digitCubeSum is 27
Current Number is 15
Current Digit is 5
Current digitCubeSum is 152
Current Number is 1
Current Digit is 1
Current digitCubeSum is 153
153 is an Armstrong Number

0에서 999까지 Armstrong 숫자를 인쇄하는 Java 프로그램

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;

public class ArmstrongNumber {

    public static void main(String[] args) {
        int tempNumber, digit, digitCubeSum;

        for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++) {
            tempNumber = inputArmstrongNumber;
            digitCubeSum = 0;
            while (tempNumber != 0) {

                /* On each iteration, remainder is powered by thetempNumber of digits n
                 */

                digit = tempNumber % 10;

                //sum of cubes of each digits is equal to thetempNumber itself
                digitCubeSum = digitCubeSum + digit * digit * digit;

                tempNumber /= 10;

            }

            //check giventempNumber and digitCubeSum is equal to or not 
            if (digitCubeSum == inputArmstrongNumber)
                System.out.println(inputArmstrongNumber + " is an Armstrong Number");

        }

    }

}

출력

0 is an Armstrong Number
1 is an Armstrong Number
153 is an Armstrong Number
370 is an Armstrong Number
371 is an Armstrong Number
407 is an Armstrong Number

java

  1. 자바 Hello World 프로그램
  2. 자바 for-each 루프
  3. Java의 생성자 오버로딩:프로그램의 정의 및 예제
  4. 소수를 확인하는 자바 프로그램
  5. Java에서 1에서 100까지의 소수를 인쇄하는 프로그램
  6. 재귀 및 루프 프로그램을 사용하는 Java의 피보나치 수열
  7. 재귀를 사용하여 Java에서 문자열을 뒤집는 방법
  8. while 및 for 루프를 사용하는 Java의 회문 수 프로그램
  9. 프로그램 예제가 있는 Java의 삽입 정렬 알고리즘
  10. 예제가 있는 Java 프로그램의 선택 정렬