| 1. |
Write a program in java to accept a word and display the ascii code of each character of the word.Sample Input: BLUEJSample Output: B:- 66L:- 75U:- 84E:- 69J:- 73 |
Answer» Required Answer:-Given Question:
Approach:This is an easy question. Check out the answer (given below). import java.util.*; public class ASCIIValue { public STATIC void main(String args[]) { /* * Written by: @AnindyaAdhikari. */ SCANNER sc= new Scanner(System.in); System.out.print("Enter a word: "); String word = sc.next(); int len = word.length(); for (int i = 0;i < len; i++) { char ch = word.charAt(i); System.out.println("Character: " + ch); System.out.println("ASCII Value: " + (int)ch + "\n"); } sc.close(); } } How it's solved ?Firstly, we will import Scanner class. Scanner class is used to take input from the user. It is present in utility package of Java. In the next step, I have created a class, main() METHOD. Now, I have declared objects of Scanner class. Using scanner class, take a word as input from the user. The next() method of Scanner class is used to input a word from the keyboard. Now, we will calculate the length of the word and then iterate a loop from i=0 to Variable Description:
Output is attached. |
|