0% found this document useful (0 votes)
18 views9 pages

AIM: To Write A JAVA Program To Display Default Value of All Primitive Data Type of JAVA

The document outlines several Java programming exercises, including displaying default values of primitive data types, calculating the roots of a quadratic equation, and finding prime numbers between 1 and n. Each section includes the aim, description, program code, sample output, and results indicating successful execution. The exercises demonstrate fundamental programming concepts in Java, such as variable initialization, conditional statements, and loops.

Uploaded by

Praveena
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)
18 views9 pages

AIM: To Write A JAVA Program To Display Default Value of All Primitive Data Type of JAVA

The document outlines several Java programming exercises, including displaying default values of primitive data types, calculating the roots of a quadratic equation, and finding prime numbers between 1 and n. Each section includes the aim, description, program code, sample output, and results indicating successful execution. The exercises demonstrate fundamental programming concepts in Java, such as variable initialization, conditional statements, and loops.

Uploaded by

Praveena
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/ 9

DATE: EXP NO: PAGE NO:

1.Displaying default value of all primitive data types


AIM: To write a JAVA program to display default value of all primitive data type of JAVA
DESCRIPTION:
Java also provides primitive data type like int, float, double, boolean, bytes, etc. Whenever a user
creates a variable in java program but doesn't assign it any value then compiler assigns it certain
value which is referred to as default value.
PROGRAM:
public class DataType
{
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 :"+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 :"+bl);
}

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

}
OUTPUT:
The default values of primitive data types are:
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char : Boolean :false
RESULT:
To write a JAVA program to display default value of all primitive data type of JAVA was executed
successfully

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

2.Roots of a quadratic equation


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
DESCRIPTION:
In algebra, a quadratic equation is an equation that can be reordered in standard form. The
standard form of a quadratic equation is ax2+bx+c=0. It is also known as the second-degree
equation. In this section, first will discuss the quadratic equation after that we will create Java
programs to solve the quadratic equation by using different approaches.
In the equation ax2+bx+c=0, a, b, and c are unknown values and a cannot be 0. x is an unknown
variable. The formula to find the roots of the quadratic equation is known as the quadratic formula.

A quadratic equation has two roots and the roots depend on the discriminant. In the above
formula, (√b2-4ac) is called discriminant (d). The value of d may be positive, negative, or zero.
PROGRAM:
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);

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

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");
}
}
}
SAMPLE OUTPUT:
Given quadratic equation:ax^2 + bx + c
Enter a:2
Enter b:3

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

Enter c:1
Roots are real and unequal
First root is:-0.5
Second root is:-1.0
EXECUTED OUTPUT:
Given quadratic equation: ax^2 + bx + c
Enter a: 1
Enter b: -3
Enter c: 2
Roots are real and unequal
First root is: 2.0
Second root is: 1.0
RESULT:
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 was executed successfull

2.Write a java program to find prime numbers between 1 to n.


AIM:
To find prime numbers between 1 to n.
DESCRIPTION:
java program that finds and prints all prime numbers between 1 and a given number ( n ). The
program uses a basic method to check for prime numbers by testing divisibility.
PROGRAM:
package Week1;
import java.util.*;
public class PrimeNumbers

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

{
public static void main(String arg[])
{
int i,j,count;
System.out.print("Enter n value : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are ");
for(j=2;j<=n;j++)
{
count=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
{
count++;
}
}
if(count==2)
{
System.out.print(j+" ");
}
}
}
}
SAMPLE OUTPUT:
Enter n value: 10
Prime numbers between 1 to 10 are:
2357

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

EXECUTED OUTPUT:
Enter n value: 20
Prime numbers between 1 to 20 are:
2 3 5 7 11 13 17 19
RESULT:
To find prime numbers between 1 to n was executed successfully.

OOPS THROUGH JAVA LAB CSE DEPT


DATE: EXP NO: PAGE NO:

OOPS THROUGH JAVA LAB CSE DEPT


DATE: DATE: EXP NO:
PAGE NO:

OOPS THROUGH JAVA CSE DEPT

You might also like