0% found this document useful (0 votes)
0 views

MODULE - 4 java

The document provides an overview of ASCII and its significance in Java programming for converting characters to their ASCII values. It includes step-by-step procedures and example Java programs to display the ASCII value of individual characters and ranges of characters. Additionally, it outlines tasks such as writing a Java program to calculate the area of a circle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

MODULE - 4 java

The document provides an overview of ASCII and its significance in Java programming for converting characters to their ASCII values. It includes step-by-step procedures and example Java programs to display the ASCII value of individual characters and ranges of characters. Additionally, it outlines tasks such as writing a Java program to calculate the area of a circle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MODULE – 4

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.

1. Write the Program to display ASCII value of alphabets in Java?


import java.util.Scanner;

public class AlphabetASCIIValue {

public static void main(String[] args) {

// declare a char variable


char ch ;
// declare a int variable
int value ;
// create Scanner class object
// to read input
Scanner scan = new Scanner(System.in);

// read alphabet
System.out.print("Enter an alphabet:: ");
ch = scan.next().charAt(0);

// convert char to ascii value


value = ch;

// display ascii value


System.out.println("ASCII value of "+
ch+" = "+value);

// Or,
// we can directly display ASCII value
// using type-casting (int)ch
System.out.println("ASCII value of "+
ch+" = "+ (int)ch );

// close Scanner class object


scan.close();
}
}

The output for different test-cases are:-


Enter an alphabet:: a
ASCII value of a = 97
ASCII value of a = 97

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;

public class ASCIIValueInRange {

public static void main(String[] args) {

// declare variables
char minRange , maxRange;

// create Scanner class object


Scanner scan = new Scanner(System.in);

// read inputs
System.out.print("Enter minRange value:: ");
minRange = scan.next().charAt(0);
System.out.print("Enter maxRange value:: ");
maxRange = scan.next().charAt(0);

// display ASCII values


System.out.println("ASCII value of "+
minRange+" to "+maxRange+" :: ");

for(char i = minRange; i<=maxRange; i++) {


System.out.print(i+"="+(int)i);
System.out.print(",\t"); //give space
}
// close Scanner class object
scan.close();
}
}
The output for the different test-cases:-
Enter minRange value:: a
Enter maxRange value:: z
ASCII value of a to z ::
a=97, b=98, c=99, d=100, e=101, f=102, g=103, h=104, i=105, j=106, k=107, l=108, m=109,
n=110, o=111, p=112, q=113, r=114, s=115, t=116, u=117, v=118, w=119, x=120, y=121, z=1
22,
Enter minRange value:: A
Enter maxRange value:: Z
ASCII value of A to Z ::
A=65, B=66, C=67, D=68, E=69, F=70, G=71, H=72, I=73, J=74, K=75, L=76, M=77, N=78
, O=79, P=80, Q=81, R=82, S=83, T=84, U=85, V=86, W=87, X=88, Y=89, Z=90,
Enter minRange value:: 0
Enter maxRange value:: 9
ASCII value of 0 to 9 ::
0=48, 1=49, 2=50, 3=51, 4=52, 5=53, 6=54, 7=55, 8=56, 9=57,

TASKS TO BE COMPLETED:
1. Write the Java Program to Calculate Area of Circle?

You might also like