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

java Exercise-1

The document contains two Java programs: one that displays the default values of all primitive data types and another that calculates the roots of a quadratic equation. The first program initializes static variables for each primitive type and prints their default values, while the second program prompts the user for coefficients of a quadratic equation, calculates the discriminant, and determines the nature of the roots. The outputs demonstrate the default values and the results of the quadratic equation based on user input.

Uploaded by

cse4671
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java Exercise-1

The document contains two Java programs: one that displays the default values of all primitive data types and another that calculates the roots of a quadratic equation. The first program initializes static variables for each primitive type and prints their default values, while the second program prompts the user for coefficients of a quadratic equation, calculates the discriminant, and determines the nature of the roots. The outputs demonstrate the default values and the results of the quadratic equation based on user input.

Uploaded by

cse4671
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AIM: Write a java program to display default value of all primitive data type of java

import java.io.*;

class DefaultValues

static byte b;

static short s;

static int i;

static long l;

static float f;

static double d;

static char c;

static boolean bl;

public static void main(String[] args)

System.out.println("The default values of primitive data types are:");

System.out.println("Byte default value :"+b);

System.out.println("Short default value :"+s);

System.out.println("Int default value :"+i);

System.out.println("Long default value :"+l);

System.out.println("Float default value :"+f);

System.out.println("Double default value :"+d);

System.out.println("Char default value :"+c);

System.out.println("Boolean default value :"+bl);

Output:
The default values of primitive data types are:

Byte default value :0

Short default value :0

Int default value :0

Long default value :0

Float default value :0.0

Double default value :0.0

Char default value :

Boolean default value :false

AIM: To write a java program that display the roots of a quadratic equation ax2+bx=0.

Calculate the discriminate D and basing on value of D, describe the nature of root.

import java.util.*;

class quadraticdemo

public static void main(String[] args)

int a, b, c;

double r1, r2, D;

Scanner s = new Scanner(System.in);

System.out.println("Given quadratic equation:ax^2 + bx + c");

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

a = s.nextInt();

System.out.print("Enter b:");

b = s.nextInt();

System.out.print("Enter c:");

c = s.nextInt();
D = b * b - 4 * a * c;

if(D > 0)

System.out.println("Roots are real and unequal");

r1 = ( - b + Math.sqrt(D))/(2*a);

r2 = (-b - Math.sqrt(D))/(2*a);

System.out.println("First root is:"+r1);

System.out.println("Second root is:"+r2);

else if(D == 0)

System.out.println("Roots are real and equal");

r1 = (-b+Math.sqrt(D))/(2*a);

System.out.println("Root:"+r1);

else

System.out.println("Roots are imaginary");

Output:

Given quadratic equation:ax^2 + bx + c

Enter a:2

Enter b:3

Enter c:1

Roots are real and unequal


First root is:-0.5

Second root is:-1

You might also like