logo

Vohunska številka v Javi

V tem razdelku se bomo naučili kaj je vohunska številka in tudi ustvarjati Java programi da preverite, ali je dana številka Vohun ali ne. The program vohunske številke se pogosto sprašuje v Java test kodiranja.

Vohunska številka

Pozitivno celo število imenujemo vohunsko število, če je vsota in izdelek njegovih števk je enakih. Z drugimi besedami, število, katerega vsota in zmnožek vseh števk sta enaka, se imenuje a vohunska številka .

Primer vohunske številke

Vzemimo številko 1124 in preverimo, ali je številka vohunska ali ne. Najprej ga bomo razdelili na števke (1, 1, 2, 4). Nato poiščite vsoto in produkt vseh števk.

vsota =1+1+2+4= 8

preobremenitev metode

Izdelek =1*1*2*4= 8

Opazimo, da sta vsota in zmnožek števk enaka. torej 1124 je vohunska številka.

koliko tednov je v mesecu

Podobno lahko preverimo tudi druge številke. Nekatere druge vohunske številke so 22, 123, 132 itd.

Vohunska številka v Javi

Koraki za iskanje vohunske številke

  1. Preberite ali inicializirajte številko ( n ), ki jih želite preveriti.
  2. Deklarirajte dve spremenljivki vsota in izdelek za shranjevanje vsote in produkta števk. Inicializirajte vsoto z 0 in izdelek z 1 .
  3. Poišči zadnja števko (n%10) danega števila z uporabo modulo operatorja.
  4. Dodajzadnja številka spremenljive vsote.Pomnožitezadnja številka s spremenljivko produkta.Razdelipodano število (n) za 10. Odstrani zadnjo števko.
  5. Ponovite korake 3 do 6 dokler dano število (n) ne postane 0.
  6. Če imata spremenljivka vsota in produkt enako vrednost, je dano število (n) a vohun število , sicer ni vohunska številka.

Izvedimo zgornje korake v programu Java.

Java program Spy Number

SpyNumberExample1.java

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

Izhod 1:

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

Rezultat 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;>