logo

Numărul de spionaj în Java

În această secțiune, vom învăța ce este un număr spion și, de asemenea, creați programe Java pentru a verifica dacă numărul dat este Spion sau nu. The program de număr spion este frecvent întrebat în Java test de codare.

Numărul de spionaj

Un număr întreg pozitiv se numește număr spion dacă sumă și produs din cifrele sale sunt egale. Cu alte cuvinte, un număr a cărui sumă și produs al tuturor cifrelor sunt egale se numește a număr spion .

Exemplu de număr de spionaj

Să luăm numărul 1124 și să verificăm dacă numărul este un spion sau nu. Mai întâi, îl vom împărți în cifre (1, 1, 2, 4). După aceea, găsiți suma și produsul tuturor cifrelor.

Sumă =1+1+2+4= 8

șir în int

Produs =1*1*2*4= 8

Observăm că suma și produsul cifrelor sunt ambele egale. Prin urmare, 1124 este un număr spion.

matrice java

În mod similar, putem verifica și alte numere. Alte numere spion sunt 22, 123, 132 etc.

Numărul de spionaj în Java

Pași pentru a găsi numărul de spion

  1. Citiți sau inițializați un număr ( n ) pe care doriți să le verificați.
  2. Declarați două variabile sumă și produs pentru a stoca suma și produsul cifrelor. Inițializați suma cu 0 si produs cu 1 .
  3. Găsi ultimul cifra (n%10) a numărului dat utilizând operatorul modulo.
  4. Adăugaultima cifră la suma variabilei.Multiplicaultima cifră cu variabila produs.Dividenumărul dat (n) cu 10. Îndepărtează ultima cifră.
  5. Repetați pașii 3 la 6 până când numărul dat (n) devine 0.
  6. Dacă variabila suma și produsul au aceeași valoare, atunci numărul dat (n) este a spion număr , altfel nu este un număr spion.

Să implementăm pașii de mai sus într-un program Java.

Programul Java cu număr spion

SpyNumberExample1.java

tipuri de testare software
 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.'); } } 

Ieșire 1:

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

Ieșire 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;>