Wednesday 8 April 2020

Write a program to input two characters from the keyboard. Find the difference (d) between their ASCII codes.

Write a program to input two characters from the keyboard. Find the difference (d) between their ASCII codes. Display the following messages: If d=0 : both the characters are same. If d<0 : first character is smaller. If d>0 : second character is smaller.


import java.util.Scanner;
public class ATS{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first character: ");
char ch1 = in.next().charAt(0);
System.out.print("Enter second character: ");
char ch2 = in.next().charAt(0);
int d = (int)ch1 - (int)ch2;
if (d > 0)
System.out.println("Second character is smaller");
else if (d < 0)
System.out.println("First character is smaller");
else
System.out.println("Both the characters are same");
    }
}

No comments:

Post a Comment