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

Lab Programs

Uploaded by

abhikannadiga20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab Programs

Uploaded by

abhikannadiga20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PART A:

1. Write a Java program to illustrate the creation of variables of


basic types and the effect of type conversions
class vtc
{
public static void main(String[] args)
{
byte b = 10;
short s = 20;
int i = 30;
long l = 40L;
float f = 50.5f;
double d = 60.5;
char c='g';

System.out.println("Original Values:");
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(" ");

double p=i;
int q= (int)d;
double r = f;
int g= b + s;

System.out.println("After Type Conversions:");


System.out.println("Int to Double: " + i + " -> " + p);
System.out.println("Double to Int: " + d + " -> " + q);
System.out.println("Float to Double: " + f + " -> " + r);
System.out.println("Byte + Short in integer: " + b + " + " + s + " -> " + g);

}
}

User Input datatype

import java.io.DataInputStream;
import java.io.IOException;

public class data


{
public static void main(String[] args) throws IOException
{
DataInputStream dis = new DataInputStream(System.in);
System.out.print("Enter an integer: ");
int intValue = Integer.parseInt(dis.readLine());

System.out.print("Enter a float: ");


float floatValue = Float.parseFloat(dis.readLine());

System.out.print("Enter a double: ");


double doubleValue = Double.parseDouble(dis.readLine());

System.out.print("Enter a long: ");


long longValue = Long.parseLong(dis.readLine());

System.out.print("Enter a short: ");


short shortValue = Short.parseShort(dis.readLine());

System.out.print("Enter a byte: ");


byte byteValue = Byte.parseByte(dis.readLine());

System.out.print("Enter a single character: ");


char charValue = (char) dis.read(); // Reads a single character

// Output the values


System.out.println("\nYou entered:");
System.out.println("Integer: " + intValue);
System.out.println("Float: " + floatValue);
System.out.println("Double: " + doubleValue);
System.out.println("Long: " + longValue);
System.out.println("Short: " + shortValue);
System.out.println("Byte: " + byteValue);
System.out.println("Character: " + charValue);

}
}

2. Write a Java program that displays roots of Quadratic Equation


ax2+bx+c=0, calculates the discriminante D and based on the value
of D, describes the nature of the roots (use nested if statement).
import java.io.DataInputStream;
import java.io.IOException;

class qe
{
public static void main(String[] args) throws IOException
{
DataInputStream dis = new DataInputStream(System.in);

System.out.print("Enter coefficients a, b, c: ");


double a = Double.parseDouble(dis.readLine());
double b = Double.parseDouble(dis.readLine());
double c = Double.parseDouble(dis.readLine());

double discriminant = b * b - 4 * a * c;

if (discriminant >0)
{
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Roots are real and distinct: " + root1 + ", " + root2);
}
else
{
if(discriminant==0)
{
double root = -b / (2 * a);
System.out.println("Roots are real and equal: " + root);

}
else
{
System.out.println("Roots are complex and distinct.");
}
}

}
}

You might also like