![]() ![]() Output: *** List of Magic Numbers between 1 to 10000 *** ("*** List of Magic Numbers between 1 to 10000 ***") Let’s now take a look into the program which prints all the Magic Numbers within the range of 1 to 10000 package com.javainterviewpoint Third Iteration fails as the value of the number is zero.įinally, calculate the product of sum and reverseSum, and check if it is equal to the original number.ġ729 is a Magic Number Java Program to Find Magic Numbers between 0 Now number is 1 and reverse is 9, while( 1 > 0) is TRUE Remove the last digit from the number, so we need to divide the number by 10 and so now number = 1 Reverse = reverse * 10 + digit (reverse = 0 + 9), now reverse is 9 Number is 19 and reverse is 0, while( 19 > 0) is TRUEĭigit = number % 10 (digit = 19 % 10), now digit is 9 Now need to calculate the reverse of the sum, the findReverse() method calculates the reverse of the sum, here also the loop continues to executes until the value is not zero Now number is 1 and sum is 18, while (1 > 0) is TRUEįifth Iteration fails as the number is now zero. Now number is 17 and sum is 11, while (17 > 0) is TRUE Now remove the last digit from the number, The value of both number and sum are changed, while (172 > 0) is TRUE, so the execution continues into the while loop Now we need to remove the last digit from the number, so we need to divide the number by 10 and so now number = 172 Sum = sum + number % 10 ( sum = 0 + 9), now sum is 9 The calculateSum() calculates the sum of all individual digits, while loop continues to run until the number is greater than zero.Īt the start, number is 1729 and sum is 0, while (1729 > 0) is TRUE, now inside the while loop ![]() OriginalNumber hold the input number entered by the user Calculate the Sum of each digit Public static int findReverse(int number) Public static int calculateSum(int number) (originalNumber +" is not a Magic Number") Scanner scanner = new Scanner(System.in) Multiply both sum and reverse, If the product is the same as the original number then it is a Magic Number.Calculate the sum of the individual digits.In this program, let’s print the Ramanujan Number Sum of all digits is 19, the reverse of the Sum is 91, and Product of these numbers 19 * 91 is equal to the original number (19 * 91 = 1729). This is also known as Ramanujan Number or Taxicab Number. Program 1: Ramanujan Number or Taxicab NumberĪ Magic Number is a number which is equal to the product of the sum of all digits of a number and reverse of the sum. I got two different answers for a magic number in programming, let’s take a look into the code for both of them. Java class Files will start with the magic text Êþº¾ –> Hex (CAFEBABE).PNG Files will start with the magic text %PNG –> Hex (25 50 4E 47).PDF Files will start with the magic text %PDF –> Hex (25 50 44 46).
0 Comments
Leave a Reply. |