0% found this document useful (0 votes)
0 views4 pages

Java Lab3

The document contains Java programming exercises for object-oriented programming. It includes code snippets for finding the greatest of three numbers, counting and summing odd numbers between 1-100, and finding the largest and smallest numbers in an array. Each exercise is accompanied by sample output demonstrating the functionality of the code.

Uploaded by

thakurajay8865
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)
0 views4 pages

Java Lab3

The document contains Java programming exercises for object-oriented programming. It includes code snippets for finding the greatest of three numbers, counting and summing odd numbers between 1-100, and finding the largest and smallest numbers in an array. Each exercise is accompanied by sample output demonstrating the functionality of the code.

Uploaded by

thakurajay8865
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/ 4

Name: Ajay Chauhan

Roll NO: 2400290119001

Ssubject: object oriented programming with java

Lab: 3

1. WAP to insert 3 numbers from the keyboard and find a greater


number among 3 numbers.

import java.util.Scanner;

public class GreatestOfThree {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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 greatest;

if (a >= b && a >= c)

greatest = a;

else if (b >= a && b >= c)


greatest = b;

else

greatest = c;

System.out.println("Greatest number is: " + greatest);

Output

Enter first number: 25

Enter second number: 41

Enter third number: 13

Greatest number is: 41

2. WAP to count the total number of odd numbers between 1-100,


and display the sum of them.

public class OddSumCount {

public static void main(String[] args) {

int count = 0;

int sum = 0;

for (int i = 1; i <= 100; i++) {

if (i % 2 != 0) {

count++;

sum += i;

}
System.out.println("Total odd numbers between 1-100: " + count);

System.out.println("Sum of odd numbers: " + sum);

Output

Total odd numbers between 1-100: 50

Sum of odd numbers: 2500

3. WAP to Find largest and smallest numbers in an array.

import java.util.Scanner;

public class MinMaxInArray {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of elements: ");

int n = sc.nextInt();

int[] arr = new int[n];

System.out.println("Enter " + n + " numbers:");

for (int i = 0; i < n; i++) {

arr[i] = sc.nextInt();

int max = arr[0];


int min = arr[0];

for (int i = 1; i < n; i++) {

if (arr[i] > max)

max = arr[i];

if (arr[i] < min)

min = arr[i];

System.out.println("Largest number = " + max);

System.out.println("Smallest number = " + min);

Output

Enter number of elements: 5

Enter 5 numbers:

10

56

78

22

Largest number = 78

Smallest number = 3

You might also like