40.9k views
5 votes
Write a program that will use user input. The program should be called PrintNumberToWordie which prints "ONE", "TWO",..., "FIVE" UP to TEN if the int variable "number" is 1, 2,..., 5, 10, respectively be sure to code for the else/default option if no match is found.

This program must Use (a) a "nested-if" statement to complete; (b) Secondly the program must have an additional if statement to check to see if the user input is divisible by 2.

1 Answer

2 votes

Answer:

import java.util.Scanner;

public class PrintNumberToWordie {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a number");

int num = in.nextInt();

if(num == 1){

System.out.println("ONE");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 2){

System.out.println("TWO");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 3){

System.out.println("THREE");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 4){

System.out.println("FOUR");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 5){

System.out.println("FIVE");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 6){

System.out.println("SIX");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 7){

System.out.println("SEVEN");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 8){

System.out.println("EIGHT");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 9){

System.out.println("NINE");

if(num%2==0)

System.out.println("Divisible by 2");

}

else if (num == 10){

System.out.println("TEN");

if(num%2==0)

System.out.println("Divisible by 2");

}

else{

System.out.println("No Match");

}

}

}

Step-by-step explanation:

  • Using Java programming language
  • Import Scanner class
  • Prompt user to enter a number, receive and store in a variable
  • Use if and else if statements to check the value of the number and output the corresponding spelling
  • Use and inner if statement to check divisibility by two using the modulo operator
User Debashrita
by
5.3k points