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 [Link];
public class GreatestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Enter third number: ");
int c = [Link]();
int greatest;
if (a >= b && a >= c)
greatest = a;
else if (b >= a && b >= c)
greatest = b;
else
greatest = c;
[Link]("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;
}
[Link]("Total odd numbers between 1-100: " + count);
[Link]("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 [Link];
public class MinMaxInArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
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];
[Link]("Largest number = " + max);
[Link]("Smallest number = " + min);
Output
Enter number of elements: 5
Enter 5 numbers:
10
56
78
22
Largest number = 78
Smallest number = 3