MODULE - 4 java
MODULE - 4 java
JAVA PRROGRAMMING
• ASCII stands for American Standard Code for Information Interchange. It was
developed by the ANSI (American National Standards Institute) and it is used to
interchange the information from a high-level language to low-level language.
Machine or Computer understand only binary languages. So, the character data type
represents integers.
• Procedure to develop the program to display the ASCII value of alphabets in
Java:-
1) Take an alphabet as input from the end-user
The next().charAt(0) is used to read character input. The next() method is used to read a
word_upto space (String), and when we use the charAt(index) method of String class then it
gives the character values of String at index position, so next().charAt(0) gives the char value
at index 0 in the String.
2) We can get the ASCII value of char by converting it to the Integer data type
(byte/short/int). Different ways for converting are,
char ch= 'a';
//direct assigning
int n1 = ch;
// type-casting
int n2 = (int)ch;
The n1 and n2 variables hold the ASCII value of the character ‘a’. The below-given program
demonstrates it.
// read alphabet
System.out.print("Enter an alphabet:: ");
ch = scan.next().charAt(0);
// Or,
// we can directly display ASCII value
// using type-casting (int)ch
System.out.println("ASCII value of "+
ch+" = "+ (int)ch );
Enter an alphabet:: 1
ASCII value of 1 = 49
ASCII value of 1 = 49
Enter an alphabet:: A
ASCII value of A = 65
ASCII value of A = 65
2. Write the Java Program Display ASCII value in the range
• We can also display ASCII value in range. For example:- ASCII value of a to z in
java. For this purpose, we need to take the help of a loop. You can use while loop, for
loop, or do-while.
import java.util.Scanner;
// declare variables
char minRange , maxRange;
// read inputs
System.out.print("Enter minRange value:: ");
minRange = scan.next().charAt(0);
System.out.print("Enter maxRange value:: ");
maxRange = scan.next().charAt(0);
TASKS TO BE COMPLETED:
1. Write the Java Program to Calculate Area of Circle?