144k views
0 votes
Instructions Write a program that asks the user for a number. If the number is between 1 and 255, the program outputs the corresponding ASCII character. If it is outside of that range, the program outputs Out of range.

User EgorD
by
5.3k points

1 Answer

4 votes

Answer:

import java.util.Scanner;

public class num5 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a number between 1 -255");

int num = in.nextInt();

if(num >= 1 && num<=255){

char c = (char)num;

System.out.println("The ASCII Value of "+num+" is "+c);

}

else{

System.out.println("Out of range");

}

}

}

Step-by-step explanation:

  • Use the scanner class to receive a number from the user save in a variable num
  • Use if statement to ensure the number is within the valid range num >= 1 && num<=255
  • Convert the number to the ASCII equivalent by casting it to char data type
  • Output the number and the ASCII equivalent
  • if num was out of range, print Out of range

User EldadT
by
6.5k points