logo

Numer szpiegowski w Javie

W tej części się dowiemy co to jest numer szpiegowski a także tworzyć Programy Java aby sprawdzić, czy podany numer jest Szpieg albo nie. The program numerów szpiegowskich jest często zapraszany Jawa test kodowania.

Numer szpiegowski

Dodatnia liczba całkowita nazywana jest liczbą szpiegowską, jeśli suma I produkt jego cyfr są równe. Innymi słowy, liczba, której suma i iloczyn wszystkich cyfr jest równa, nazywa się a numer szpiegowski .

Przykład numeru szpiega

Weźmy numer 1124 i sprawdźmy, czy jest to numer szpiegowski, czy nie. Najpierw podzielimy go na cyfry (1, 1, 2, 4). Następnie znajdź sumę i iloczyn wszystkich cyfr.

Suma =1+1+2+4= 8

int do napisania Java

Produkt =1*1*2*4= 8

Zauważamy, że suma i iloczyn obu cyfr jest równa. Stąd, 1124 to numer szpiegowski.

W podobny sposób możemy sprawdzić także inne liczby. Niektóre inne numery szpiegowskie to 22, 123, 132 itd.

Numer szpiegowski w Javie

Kroki, aby znaleźć numer szpiega

  1. Przeczytaj lub zainicjuj liczbę ( N ), który chcesz sprawdzić.
  2. Zadeklaruj dwie zmienne suma I produkt do przechowywania sumy i iloczynu cyfr. Zainicjuj sumę za pomocą 0 i produkt z 1 .
  3. Znaleźć ostatni cyfra (n%10) danej liczby za pomocą operatora modulo.
  4. Dodaćostatnia cyfra sumy zmiennej.Zwielokrotniaćostatnia cyfra ze zmienną produktu.Dzielićpodaną liczbę (n) o 10. Usuwa ostatnią cyfrę.
  5. Powtórz kroki 3 do 6 aż podana liczba (n) stanie się 0.
  6. Jeśli suma zmiennych i iloczyn mają tę samą wartość, to podana liczba (n) to a szpieg numer , w przeciwnym razie nie jest to numer szpiegowski.

Zaimplementujmy powyższe kroki w programie Java.

Program Java numer szpiegowski

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.'); } } 

Wyjście 1:

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

Wyjście 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;>