logo

Java의 스파이 번호

이 섹션에서는 다음 내용을 학습합니다. 스파이 번호가 뭐야? 그리고 또한 생성 자바 프로그램 주어진 숫자가 다음과 같은지 확인하려면 스파이 아니면. 그만큼 스파이 번호 프로그램 에서 자주 묻는다 자바 코딩 테스트.

스파이 번호

다음과 같은 경우 양의 정수를 스파이 번호라고 합니다. 합집합 그리고 제품 그 자릿수가 동일합니다. 즉, 모든 숫자의 합과 곱이 같은 수를 '수'라고 합니다. 스파이 번호 .

스파이 번호의 예

1124라는 번호를 가지고 그 번호가 스파이인지 아닌지 확인해 봅시다. 먼저 숫자(1, 1, 2, 4)로 나누어보겠습니다. 그런 다음 모든 숫자의 합과 곱을 구하세요.

합집합 =1+1+2+4= 8

제품 =1*1*2*4= 8

우리는 숫자의 합과 곱이 모두 같다는 것을 관찰합니다. 따라서, 1124 스파이 번호입니다.

마찬가지로 다른 숫자도 확인할 수 있습니다. 다른 스파이 번호는 22, 123, 132 등입니다.

Java의 스파이 번호

스파이 번호를 찾는 단계

  1. 숫자 읽기 또는 초기화( N ) 확인하고 싶은 내용입니다.
  2. 두 개의 변수 선언 합집합 그리고 제품 숫자의 합과 곱을 저장합니다. 다음으로 합계 초기화 0 그리고 제품 1 .
  3. 찾기 마지막 모듈로 연산자를 사용하여 주어진 숫자의 숫자(n%10)를 계산합니다.
  4. 추가하다변수 합계의 마지막 숫자입니다.곱하다제품 변수의 마지막 숫자입니다.나누다주어진 숫자(n)를 10으로 변환합니다. 마지막 숫자를 제거합니다.
  5. 단계를 반복하세요 3~6 주어진 숫자(n)가 0이 될 때까지
  6. 변수 sum과 product의 값이 같은 경우 주어진 숫자(n)는 스파이 숫자 , 그렇지 않으면 스파이 번호가 아닙니다.

위의 단계를 Java 프로그램에서 구현해 보겠습니다.

스파이 번호 Java 프로그램

SpyNumberExample1.java

 import java.util.Scanner; public class SpyNumberExample1 { public static void main(String args[]) { int num, product=1, sum=0, lastdigit; // create object of scanner Scanner sc = new Scanner(System.in); System.out.print('Enter the number to check: ' ); //reads an integer from the user and stores it in the variable num num=sc.nextInt(); //executes untill the condition becomes false while(num>0) { //finds the last digit of the number lastdigit=num%10; //adds last digit to the variable sum sum=sum+lastdigit; //calculates the product product=product*lastdigit; //removes the last digit from the given number num=num/10; } //compares the sum and product if(sum==product) //prints if the above condition returns true System.out.println('The given number is a spy number.'); else //prints if the above condition returns false System.out.println('The given number is not a spy number.'); } } 

출력 1:

 Enter the number to check: 123 The given number is a spy number. 

출력 2:

 Enter the number to check: 456 The given number is a not spy number. 

SpyNumberExample2.java

 import java.util.Scanner; public class SpyNumberExample2 { //method to check the Spy number private static boolean isSpyNumber(int number) { int lastDigit = 0; int sum = 0; int product = 1; //executes until the condition returns true while(number != 0) { //determines the last digit of the given number lastDigit = number % 10; //adds the last digit to the variable sum sum = sum + lastDigit; //multiply last digit with product product = product * lastDigit; //removes the last digit of the given number number = number / 10; } //compares the variable sum with product and returns the result accordingly if(sum == product) return true; return false; } //driver code public static void main(String args[]) { int lowerRange = 0, upperRange = 0; Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the lower range: &apos;); //reads lower range lowerRange = sc.nextInt(); System.out.print(&apos;Enter upper range: &apos;); //reads the upper range upperRange = sc.nextInt(); System.out.println(&apos;The Spy numbers between &apos;+ lowerRange + &apos; to &apos;+ upperRange+&apos; are: &apos;); for(int i=lowerRange; i<=upperrange; i++) { calling user-defined function that checks if the given number is spy or not if(isspynumber(i)) prints all numbers system.out.print(i +' '); } < pre> <p> <strong>Output:</strong> </p> <pre> Enter the lower range: 1 Enter upper range: 10000 The Spy numbers between 1 to 10000 are: 1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421 2114 2141 2411 4112 4121 4211 </pre> <hr></=upperrange;>