Question

Holy digits Batman! The Riddler is planning his next caper somewhere on Pennsylvania Avenue. In his...

Holy digits Batman! The Riddler is planning his next caper somewhere on Pennsylvania Avenue. In his usual sporting fashion, the Riddler has left the address in the form of a puzzle. The address on Pennsylvania is a four-digit number where all the following criteria are true:

All four digits are different

The digit in the thousands place is three times the digit in the tens place

The number is odd

The sum of all four digits == 27

Write a Java console application that uses a single loop to figure out the address (4-digit number) where the Riddler intends to strike. Display the address as "The Riddler intends to strike #### Pennsylvania Avenue."

Homework Answers

Answer #1

RiddlerStrike.java :
_________________

public class RiddlerStrike {
   public static void main(String[] args) {
       int digits[] = {0,1,2,3,4,5,6,7,8,9};
       int four[] = {3,7,8,9};//we need to have a four digits sum =27,so these four only give sum 27
       StringBuffer sb = new StringBuffer();
       sb.append(9); // thousands place is three times the digit in tens place
       sb.append(four[2]);
       sb.append(3);//tens place
       sb.append(four[1]);// sum needs to be odd
       //Here the checking
       int sum = 0;
       for(int i=0;i<sb.length();i++){
           sum += Character.getNumericValue(sb.charAt(i));
       }
       int num = Integer.parseInt(sb.toString());
       if(sum == 27 && num%2!=0)
           System.out.println("The Riddler intends to strike "+ num +" Pennsylvania Avenue");
   }
}

Sample Input and Output:
______________________

The Riddler intends to strike 9837 Pennsylvania Avenue

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions