Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Java Lab Record

The document describes three Java programs: 1) A program to display the default values of Java's primitive data types. It declares variables of each type and prints their default values. 2) Programs to find the discriminant and roots of a quadratic equation, and to determine qualifying racers in a race based on average speed. 3) Programs to print prime numbers from 1 to 100 using a while loop, calculate the sum of even Fibonacci terms up to a given range, and check if a number is Armstrong.

Uploaded by

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

Java Lab Record

The document describes three Java programs: 1) A program to display the default values of Java's primitive data types. It declares variables of each type and prints their default values. 2) Programs to find the discriminant and roots of a quadratic equation, and to determine qualifying racers in a race based on average speed. 3) Programs to print prime numbers from 1 to 100 using a while loop, calculate the sum of even Fibonacci terms up to a given range, and check if a number is Armstrong.

Uploaded by

Abhilash Abhi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

EXPERIMENT-1

1) Basic Programs
1.1) Write a Java program to display default value of all primitive data type of JAVA.

Aim: To Write a Java program to display default value of all primitive data type of JAVA.

Description: There are 8 Primitive Data types in Java. They are Integer, Long, Short, Double, Float, Byte, Char
and Boolean. They store different types of data types in Default. To print those values we use println() method.

Program:
class PrimDT
{
int i;
long l;
short s;
double d;
float f;
byte b;
char c;
boolean B;
void DefaultVal()
{
System.out.println("Integer: "+i);
System.out.println("Long: "+l);
System.out.println("Short: "+s);
System.out.println("Double: "+d);
System.out.println("Float: "+f);
System.out.println("Byte: "+b);
System.out.println("Char: "+c);
System.out.println("Boolean: "+B);
}
public static void main(String[] args)
{
PrimDT obj = new PrimDT();
obj.DefaultVal();
}
}
Output:
Integer: 0
Long: 0
Short: 0
Double: 0.0
Float: 0.0
Byte: 0
Char:
Boolean: false
1.2) Write a Java program to find the discriminant value D and find out the roots of the quadratic
equation of the form ax2+bx+c=0.
Aim: To Write a Java program to find the discriminant value D and find out the roots of the quadratic equation
of theformax2+bx+c=0.
Description:
Program:
public class Quadratic Equation
{
public static void main(String[] args)
{
double a =Double.parseDouble(String[0]);
double b =Double.parseDouble(String[1]) ;
double c =Double.parseDouble(String[2]);
double d =b*b-4.0*a*c;
System.out.println(“Discriminant value:”+math.pow(d,0.5));
if(double.isNan(d))
{
double r1=(-b+Math.pow(d,0.5)(2.0*a);
double r2=(-b-Math.pow(d,0.5)(2.0*a);
System.out.println(“the roots are”+r1+”and” +r2);
}
else if(d==0.0)
{
double r1=-b1(2.0*a);
System.out.println(“the roots is”+r1);
}
else
{
System.out.println(“roots are not real”);
}
}
}
Output:
1 3 2
Discriminant value 1.0
The roots are -1.0 and -2.0.
1.3) Five Bikers Compete in a race such that they drive at a constant speed which may or may not be the
same as the other. To qualify the race, the speed of a racer must be more than the average speed of all 5
racers. Take as input the speed of each racer and print back the speed of qualifying racers.

Aim: To Write a Java Program for the problem that Five Bikers Compete in a race such that they drive at a
constant speed which may or may not be the same as the other. To qualify the race, the speed of a racer must be
more than the average speed of all 5 racers. Take as input the speed of each racer and print back the speed of
qualifying racers.

Description: In this program we need to find who will be qualifying racers. In order to find that we need to
calculate the average speed of the 5 racers. To find the average speed we use the formula:

Average speed=(s1+s2+s3+s4+s5)/5
S1, s2, s3, s4, s5 are speeds of the racers.
Program:
import java.util.*;
class RaceDemo
{
public static void main(String[] args)
{
float s1,s2,s3,s4,s5,average;
Scanner s = new Scanner(System.in);
System.out.println("Enter speed of first racer:");
s1 = s.nextFloat();
System.out.println("Enter speed of second racer:");
s2 = s.nextFloat();
System.out.println("Enter speed of third racer:");
s3 = s.nextFloat();
System.out.println("Enter speed of fourth racer:");
s4 = s.nextFloat();
System.out.println("Enter speed of fifth racer:");
s5 = s.nextFloat();
average=(s1+s2+s3+s4+s5)/5;
if(s1>average)
System.out.println("First racer is qualified with speed:"+s1);
if(s2>average)
System.out.println("Second racer is qualified with speed:"+s2);
if(s3>average)
System.out.println("Third racer is qualified with speed:"+s3);
if(s4>average)
System.out.println("Fourth racer is qualified with speed:"+s4);
if(s5>average)
System.out.println("Fifth racer is qualified with speed:"+s5);
}
}
Output:
Enter speed of first racer:
10
Enter speed of second racer:
20
Enter speed of third racer:
30
Enter speed of fourth racer:
40
Enter speed of fifth racer:
50
Fourth racer is qualified with speed:40.0
Fifth racer is qualified with speed:50.0
EXPERIMENT-2
2) Control Flow Statements
2.1) Write a Java program to select all the prime numbers within the range of 1to100.
Aim: To Write a Java program to select all the prime numbers within the range of 1to100.
Description: Prime numbers are natural numbers that are divisible by only 1 and the number itself. In this
program we use while loop to print the prime numbers between 1 and 100.
Syntax: while(condition)
{
Statement to be executed
}

Program:
public class Prime
{
public static void main(String[] args)
{
int ct=0,n=0,i=1,j=1;
while(n<25)
{
j=1;
ct=0;
while(j<=i)
{
if(i%j==0)
ct++;
j++;
}
if(ct==2)
{
System.out.printf("%d ",i);
n++;
}
i++;
}
}
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 53 59 61 67 71 73 79 83 89 97
2.2) Write a Java program to Find the sum of all even terms in the Fibonacci sequence up to the given
range N.

Aim: To write a Java program to find the sum of all even terms in the Fibonacci sequence up to the given range
N.
Description: In Fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55 etc. The first two numbers of Fibonacci series are 0 and 1.

Program:

import java.util.Scanner;
import java.io.*;
class FibonacciSum
{
public static void main(String[] args)
{
fibonacci[0] = 0;
fibonacci[1] = 1;
sum = 0;
for (i = 2;i <= 2 * my_input; i++)
{
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
if (i % 2 == 0)
sum += fibonacci[i];
}
System.out.printf("Even sum of fibonacci series till range %d
is %d" , my_input, sum);
}
}

Output:
Enter the range for Fibonacci series:
6
The Fibonacci series is as follows:
0 1 2 3 5 8
Sum of even terms in Fibonacci series up to a range 6 is:10

2.3) Write a Java program to check whether a given number is Armstrong or not.
Aim: To write a Java program to check whether a given number is Armstrong or not.
Description: An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of
their digits.
153: 13 + 53 + 33 = 1 + 125+ 27 = 153(Armstrong Number)
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)

Program:
import java.util.Scanner;
import java.lang.Math;
public class ArmstrongNumber
{
static boolean isArmstrong(int n)
{
int temp, digits=0, last=0, sum=0;
temp=n;
while(temp>0)
{
temp = temp/10;
digits++;
}
temp = n;
while(temp>0)
{
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
}
if(n==sum)
return true;
else
return false;
}
public static void main(String args[])
{
int num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num=sc.nextInt();
if(isArmstrong(num))
{
System.out.print("Armstrong ");
}
else
{
System.out.print("Not Armstrong ");
}
}
}
Output 1:
Enter the number: 1634
Armstrong

Output 2:
Enter the number: 56
Not Armstrong
EXPERIMENT-3

3) Arrays

3.1) Write a Java program to implement binary search.

Aim: To Write a Java program to implement binary search.

Description: Binary search is used to search a key element from multiple elements. Binary search is faster than
linear search. In case of binary search, array elements must be in ascending order. If you have unsorted array,
you must sort the array first. Binary Search is a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half.

Program:

import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int i, n, key, arr[], low, high, mid;
Scanner input = new Scanner(System.in);
System.out.printf("Enter number of elements:");
n = input.nextInt();
arr = new int[n];
System.out.println("Enter the Values:");
for (i = 0; i < n; i++)
arr[i] = input.nextInt();
System.out.printf("Enter the Key value:");
key = input.nextInt();
low = 0;
high = n - 1;
mid = (low + high)/2;
while( low <= high )
{
if ( arr[mid] < key )
low = mid + 1;
else if ( arr[mid] == key )
{
System.out.println(key + " found at location " +
(mid + 1));
break;
}
else
{
high = mid - 1;
}
mid = (low + high)/2;
}
if( low > high )
System.out.println(key + " is not found.\n");
}
}
Output:
Enter number of elements:5
Enter the Values:
10
20
30
40
50
Enter the Key value:50
50 found at location 5

Output:
Enter number of elements:5
Enter the Values:
10
20
30
40
50
Enter the Key value:60
60 is not found.
3.2) Write a Java program to sort for an element in a given list of elements using bubble sort.

Aim: To Write a Java program to sort for an element in a given list of elements using bubble sort.

Description:

Program:
import java.util.*;
public class BubbleSort
{
public static void main(String[] args)
{
int n=6, i, j, x;
int[] array = new int[n];
Scanner s = new Scanner(System.in);
System.out.println("Enter Elements to sort:");
for(i=0; i<n; i++)
{
array[i] = s.nextInt();
}
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(array[j]>array[j+1])
{
x = array[j];
array[j] = array[j+1];
array[j+1] = x;
}
}
}
System.out.println("\nThe new sorted array is:");
for(i=0; i<n; i++)
System.out.print(array[i]+ " ");
}
}
Output:

Enter Elements to sort:


64
23
9
12
96
43

The new sorted array is:


9 12 23 43 64 96
3.3) Write a Java program to sort for an element in a given list of elements using merge sort.
Aim: To write a Java program to sort for an element in a given list of elements using merge sort.
Description:
Program:

You might also like