0% found this document useful (0 votes)
31 views5 pages

Java Programming Lab Exercises

The document contains code snippets and outputs from several Java programs. It includes programs to print a welcome message, check if a number is prime, find the greatest of three numbers using ternary and nested if operators, and print the Fibonacci series up to a given number of terms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Java Programming Lab Exercises

The document contains code snippets and outputs from several Java programs. It includes programs to print a welcome message, check if a number is prime, find the greatest of three numbers using ternary and nested if operators, and print the Fibonacci series up to a given number of terms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Batch:4 CE B2 225

Enrollment no:IU2241050254

LAB 1

AIM: Write a program to display “Welcome To Java World.”


class prg1
{
public static void main(String[] args)
{
System.out.println("Welcome To Java World.");
}
}

Output:
Batch:4 CE B2 Enrollment no:IU2241050254
25

AIM: Write a program to find whether the number is prime or not.


import java.util.Scanner;
class PrimeExample3 {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
if (isPrime(n)) {
System.out.println(n + " is a prime number");
} else {
System.out.println(n + " is not a prime number");
}
}

public static boolean isPrime(int n) {


if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

Output:
Batch:4 CE B2 Enrollment no:IU2241050254
25

AIM: Write a program to find a greater number among given three numbers using
A) Ternary operator
B) Nested if
A) class prg3
{
public static void main(String args[])
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
System.out.println("Largest number among " + n1 + ", " + n2 + " and " + n3 +" is " + max + ".");
}
}

OUTPUT:
Batch:4 CE B2 25
Enrollment no:IU2241050254

B)class Max
{
public static void main(String[] args)
{
double n1 = -4.5, n2 = 3.9, n3 = 5.5;
if(n1 >= n2) {
if(n1 >= n3)
System.out.println(n1 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
} else {
if(n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}
}
}

OUTPUT:
Batch:4 CE B2 Enrollment no:IU2241050254
25

AIM:Write a program to print the fibonacci series.


class Main
{
public static void main(String[] args)
{
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i)
{
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}

OUTPUT:

You might also like