It's Magic...................
import javax.swing.JOptionPane;
public class MagicOf9 {
public static void main(String[] args) {
String msg = "Think and write somewhere n digit number, \n" +
"note that all digits must be unique \nfor e.g. for three" +
" digit number I am assuming 123, \nwhere 1, 2, and 3 are " +
"different and unique digits. \nNow add all the digits in " +
"the number \nfor e.g. in our case it is 1 + 2 + 3. \nThe sum " +
"is 6. \nNow subtract this resultant (i.e. 6 in this example) \n" +
"from the supposed number. \nEnter your any (n - 1) digits like \n" +
"in my case the result is 117 \nand I am giving 1 and 7. \nThe missing " +
"digit will be given by me. \nNow press OK and give me digits.";
JOptionPane.showMessageDialog(null, msg);
long digits = Long.parseLong(JOptionPane.showInputDialog(null, "Enter your resultant n - 1 digits"));
long sum = 0;
while (digits > 0) {
sum += digits % 10;
digits /= 10;
}
if (sum <= 9)
JOptionPane.showMessageDialog(null, "Missing digit is " + (9 - sum));
else if (sum > 9 && sum <= 18)
JOptionPane.showMessageDialog(null, "Missing digit is " + (18 - sum));
else if (sum > 18 && sum <= 27)
JOptionPane.showMessageDialog(null, "Missing digit is " + (27 - sum));
else if (sum > 27 && sum <= 36)
JOptionPane.showMessageDialog(null, "Missing digit is " + (36 - sum));
else if (sum > 36 && sum <= 45)
JOptionPane.showMessageDialog(null, "Missing digit is " + (45 - sum));
}
}
public class MagicOf9 {
public static void main(String[] args) {
String msg = "Think and write somewhere n digit number, \n" +
"note that all digits must be unique \nfor e.g. for three" +
" digit number I am assuming 123, \nwhere 1, 2, and 3 are " +
"different and unique digits. \nNow add all the digits in " +
"the number \nfor e.g. in our case it is 1 + 2 + 3. \nThe sum " +
"is 6. \nNow subtract this resultant (i.e. 6 in this example) \n" +
"from the supposed number. \nEnter your any (n - 1) digits like \n" +
"in my case the result is 117 \nand I am giving 1 and 7. \nThe missing " +
"digit will be given by me. \nNow press OK and give me digits.";
JOptionPane.showMessageDialog(null, msg);
long digits = Long.parseLong(JOptionPane.showInputDialog(null, "Enter your resultant n - 1 digits"));
long sum = 0;
while (digits > 0) {
sum += digits % 10;
digits /= 10;
}
if (sum <= 9)
JOptionPane.showMessageDialog(null, "Missing digit is " + (9 - sum));
else if (sum > 9 && sum <= 18)
JOptionPane.showMessageDialog(null, "Missing digit is " + (18 - sum));
else if (sum > 18 && sum <= 27)
JOptionPane.showMessageDialog(null, "Missing digit is " + (27 - sum));
else if (sum > 27 && sum <= 36)
JOptionPane.showMessageDialog(null, "Missing digit is " + (36 - sum));
else if (sum > 36 && sum <= 45)
JOptionPane.showMessageDialog(null, "Missing digit is " + (45 - sum));
}
}
Comments
Post a Comment
Post Your Valuable Comments