0% found this document useful (0 votes)
1 views7 pages

Java Lab Practicals

The document contains a series of Java programming exercises that cover various concepts such as printing odd numbers, calculating series summation, comparing numbers, generating random numbers, using arrays, user-defined functions, constructors, string manipulation, input streams, sorting arrays, and control flow statements like loops and switch cases. Each exercise includes a code snippet and its corresponding output. The exercises are designed to help learners practice and understand fundamental Java programming skills.

Uploaded by

sakshamsingla295
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)
1 views7 pages

Java Lab Practicals

The document contains a series of Java programming exercises that cover various concepts such as printing odd numbers, calculating series summation, comparing numbers, generating random numbers, using arrays, user-defined functions, constructors, string manipulation, input streams, sorting arrays, and control flow statements like loops and switch cases. Each exercise includes a code snippet and its corresponding output. The exercises are designed to help learners practice and understand fundamental Java programming skills.

Uploaded by

sakshamsingla295
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/ 7

Java Lab

Practicals
1) Write a program to Print Odd numbers from 1 to 9 :
class odd { public static void main(String[] args)
{
System.out.println("Odd table is ");
for (int i=1;i<=10;i=i+2)
{ System.out.println(i);
} }}
OUTPUT: 1
3
5
7
9
2) Write a program to print series summation program that uses a Java variable n :
public class SeriesSum
{
public static void main (String [] args)
{
int n = 9;
System.out.println((n * (n + 1)) / 2);
}
}
OUTPUT: 45

3) Write a program to compare two numbers using if else if statements:


public class CompareTwoNumbers
{
public static void main(String[] args)
{
int num1 = 324;
int num2 = 234;
if(num1 > num2)
{
System.out.println(num1 + " is greater than " + num2);
}
else if(num1 < num2)
{
System.out.println(num1 + " is less than " + num2);
}
Else
{ System.out.println(num1 + " is equal to " + num2);
}}}
OUTPUT: 324 is greater than 234
4) Write a program to generate 5 Random nos. between 1 to 100, and it should not
follow with decimal point :
class RandomDemo
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
System.out.println((int)(Math.random()*100));
}}}
5) Write a program to declare, Initialize and print an Array :
class ArrayProgram
{
public static void main(String args[])
{
double[] marks = {346, 144, 103, 256.5, 387.5};
for (int i = 0; i<marks.length ; i++)
{
System.out.println(marks[i]);
}
OUTPUT: 346
144
103
256.5
387.5
6) Write a program to show the use of User Defined Function :
class MethodProgram
{
static double rectangle_area (double length, double breadth)
{
return (length * breadth);
}
public static void main(String args[])
{
double a = 0;
a = rectangle_area(45.5, 78.5);
System.out.println("Area of the rectangle = "+ a);
}
}
OUTPUT: Area of the rectangle = 3571.75
7) Write a program to show the use of Constructor :
class Book
{
Book()
{
title = "";
author = "";
publisher = "";
price = 0;
System.out.println(“Title=”+title);
System.out.println(“Author=”+author);
System.out.println(“Publisher=”+publisher);
System.out.println(“Price=”+price);
}
public static void main(String[] args)
{
Book book1 = new Book( );
Book1.title="Game of Thrones";
Book1.author="George R Martin";
Book1.publisher= "Harper Collins";
Book1.price= 320.0;
book1.display();
}
OUTPUT: title="Game of Thrones";
author="George R Martin";
publisher= "Harper Collins";
price= 320.0;

8) Write a java program to create a String :


class Book
{
public static void main(String[] args)
{
String first =”Java”;
String second=”C++”;
System.out.println(first);
System.out.println(second);
}}
OUTPUT: Java
C++
9) Write a java program to use Input Stream:
import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}
OUTPUT: Enter first number-10
Enter second number-20
Enter first number-30
Total= 60
10) Write a Java Program to have taken string input:
import java.util.*;
class UserInputDemo1
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter a string: ");
String str= sc.nextLine(); //reads string
System.out.print("You have entered: "+str);
}
}
11) Write a Java Program to sort an array:
import java.util.Arrays;
public class SortArrayExample1
{
public static void main(String[] args)
{
//defining an array of integer type
int [] array = new int [] {90, 23, 5, 109, 12, 22, 67, 34};
//invoking sort() method of the Arrays class
Arrays.sort(array);
System.out.println("Elements of array sorted in ascending order: ");
//prints array using the for loop
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}}}
OUTPUT : Array elements in ascending order:
5
12
22
23
34
67
90
109

12) Write a Java Program to show the use of DoWhile loop :


public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}} OUTPUT : 1
2
3
4
5
6
7
8
9
10
13) Write a Java Program to show the use of While loop :
public class WhileExample {
public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++;
}} }
14) Write a Java Program to show the use of break statement :
public class BreakExample {
public static void main(String[] args) {
//using for loop
for(int i=1;i<=10;i++){
if(i==5){
//breaking the loop
break;
}
System.out.println(i);
} } }

15) Write a Java Program to show the use of Switch :


public class SwitchExample {
public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
//Default case statement
default:System.out.println("Not in 10, 20 or 30");
} }}

OUTPUT : 20

You might also like