logo

מספר ריגול ב-Java

בחלק זה נלמד מהו מספר מרגל וגם ליצור תוכניות Java כדי לבדוק אם המספר הנתון הוא מרגל או שלא. ה תוכנית מספרי ריגול נשאל לעתים קרובות פנימה Java מבחן קידוד.

מספר מרגל

מספר שלם חיובי נקרא מספר מרגל אם סְכוּם ו מוצר מהספרות שלו שוות. במילים אחרות, מספר שהסכום והמכפלה שלו של כל הספרות שווים נקרא a מספר מרגל .

דוגמה למספר מרגל

ניקח את המספר 1124 ונבדוק אם המספר הוא מרגל או לא. ראשית, נחלק אותו לספרות (1, 1, 2, 4). לאחר מכן מצא את הסכום והמכפלה של כל הספרות.

סְכוּם =1+1+2+4= 8

חלון.פתח

מוצר =1*1*2*4= 8

אנו רואים שהסכום והמכפלה של הספרות שניהם שווים. לָכֵן, 1124 הוא מספר מרגל.

שאלות ראיון ב-java

באופן דומה, אנו יכולים לבדוק גם מספרים אחרים. כמה מספרי ריגול אחרים הם 22, 123, 132 וכו'.

מספר ריגול ב-Java

שלבים למציאת מספר ריגול

  1. קרא או אתחול מספר ( נ ) שאתה רוצה לבדוק.
  2. הכריז על שני משתנים סְכוּם ו מוצר לאחסן סכום ומכפלה של ספרות. אתחול סכום עם 0 ומוצר עם 1 .
  3. למצוא את ה אחרון ספרה (n%10) של המספר הנתון באמצעות האופרטור modulo.
  4. לְהוֹסִיףהספרה האחרונה לסכום המשתנה.לְהַכפִּילהספרה האחרונה עם משתנה המוצר.לחלקהמספר הנתון (n) ב-10. הוא מסיר את הספרה האחרונה.
  5. חזור על שלבים 3 עד 6 עד שהמספר הנתון (n) הופך ל-0.
  6. אם לסכום ולמכפל המשתנה יש אותו ערך, אז המספר הנתון (n) הוא a מרגל מספר , אחרת לא מספר מרגל.

בואו ליישם את השלבים לעיל בתוכנית Java.

תוכנית Java מספר ריגול

SpyNumberExample1.java

10 מתוך מיליון
 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;>