a) Write a program in Java to read differe types of data and print them.
import java.util.Scanner;
public class InputAllDataTypes
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter byte value: ");
byte b = sc.nextByte();
System.out.print("Enter short value: ");
short s = sc.nextShort();
System.out.print("Enter int value: ");
int i = sc.nextInt();
System.out.print("Enter long value: ");
long l = sc.nextLong();
System.out.print("Enter float value: ");
float f = sc.nextFloat();
System.out.print("Enter double value: ");
double d = sc.nextDouble();
System.out.print("Enter char value: ");
char c = sc.next().charAt(0);
System.out.print("Enter boolean value (true/false): ");
boolean bool = sc.nextBoolean();
System.out.println("You entered: ");
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + bool);
}
}
b) Write a Java program to display thr range of all the data types available in Java.
public class DataTypeRanges {
public static void main(String[] args) {
System.out.println("byte : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int : " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("float : " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
System.out.println("double : " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
System.out.println("char : " + (int) Character.MIN_VALUE + " to " + (int)
Character.MAX_VALUE);
System.out.println("boolean : " + Boolean.FALSE + " or " + Boolean.TRUE);
}
}
c) Write a Java program to display formatted data using printf method in Java
public class FormatExamples {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.printf("Example 1 :%d\n", 123);
System.out.printf("Example 2 :%8d\n", 123);
System.out.printf("Example 3 :%-8d\n", 123);
System.out.printf("Example 4 :%8d\n", -123);
System.out.printf("Example 5 :%f\n", 123.456);
System.out.printf("Example 6 :%f\n", 123.4567896);
System.out.printf("Example 7 :%e\n", 123.456);
System.out.printf("Example 8 :%e\n", 1.23456e2);
System.out.printf("Example 9 :%e\n", 123.456e+0);
System.out.printf("Example 10 :%f\n", 1.23456E+2);
System.out.printf("Example 11 :%4.2f\n", 123.456);
System.out.printf("Example 12 :%8.2f\n", 123.456);
System.out.printf("Example 13 :%.3e\n", 123.456);
System.out.printf("Example 14 :%12.3e\n", 123.456);
}
}