From 0be20d83855ce29afd08e303ef075d3c11add868 Mon Sep 17 00:00:00 2001 From: Nadia1325 Date: Sun, 27 Oct 2024 22:42:14 +0200 Subject: [PATCH] 223012252 Nadia UMUMARARUNGU --- 223012252 | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 223012252 diff --git a/223012252 b/223012252 new file mode 100644 index 0000000..2ab6ec5 --- /dev/null +++ b/223012252 @@ -0,0 +1,201 @@ +Q1-- +import java.util.Scanner; + +public class EvenOdd { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a number: "); + int num = scanner.nextInt(); + + if (num % 2 == 0) { + System.out.println(num + " is even."); + } else { + System.out.println(num + " is odd."); + } + } +} + + +Q2-- + +import java.util.Scanner; + +public class GradeSwitch { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a grade (A, B, C, D, F): "); + char grade = scanner.next().charAt(0); + + switch(grade) { + case 'A': + System.out.println("Excellent"); + break; + case 'B': + System.out.println("Good"); + break; + case 'C': + System.out.println("Average"); + break; + case 'D': + System.out.println("Poor"); + break; + case 'F': + System.out.println("Fail"); + break; + default: + System.out.println("Invalid grade"); + } + } +} + +Q3-- +import java.util.Scanner; + +public class LeapYear { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a year: "); + int year = scanner.nextInt(); + + if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { + System.out.println(year + " is a leap year."); + } else { + System.out.println(year + " is not a leap year."); + } + } +} + +Q4-- + +import java.util.Scanner; + +public class LargestNumber { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter three numbers: "); + int num1 = scanner.nextInt(); + int num2 = scanner.nextInt(); + int num3 = scanner.nextInt(); + + if (num1 >= num2 && num1 >= num3) { + System.out.println(num1 + " is the largest."); + } else if (num2 >= num1 && num2 >= num3) { + System.out.println(num2 + " is the largest."); + } else { + System.out.println(num3 + " is the largest."); + } + } +} +Q5-- +import java.util.Scanner; + +public class PosNegZero { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a number: "); + int num = scanner.nextInt(); + + if (num > 0) { + System.out.println(num + " is positive."); + } else if (num < 0) { + System.out.println(num + " is negative."); + } else { + System.out.println(num + " is zero."); + } + } +} + +Q6-- +public class TrianglePattern { + public static void main(String[] args) { + for (int i = 1; i <= 7; i++) { + for (int j = 1; j <= i; j++) { + System.out.print(j + " "); + } + System.out.println(); + } + } +} + +Q7-- +import java.util.Scanner; + +public class DiamondPattern { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of rows: "); + int rows = scanner.nextInt(); + + // Upper triangle + for (int i = 1; i <= rows; i++) { + for (int j = i; j < rows; j++) { + System.out.print(" "); + } + for (int j = 1; j <= (2 * i - 1); j++) { + System.out.print("*"); + } + System.out.println(); + } + + // Lower triangle + for (int i = rows - 1; i >= 1; i--) { + for (int j = rows; j > i; j--) { + System.out.print(" "); + } + for (int j = 1; j <= (2 * i - 1); j++) { + System.out.print("*"); + } + System.out.println(); + } + } +} + +Q8--public class ArrayAverage { + public static void main(String[] args) { + int[] arr = {10, 12, 34, 23, 56, 78}; + int sum = 0; + + for (int i = 0; i < arr.length; i++) { + sum += arr[i]; + } + + double average = sum / (double) arr.length; + System.out.println("The average is: " + average); + } +} + +Q9--public class ReverseArray { + public static void main(String[] args) { + int[] arr = {3, 4, 6, 1, 9, 7, 5, 8}; + + System.out.println("Original array:"); + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + + System.out.println("Reversed array:"); + for (int i = arr.length - 1; i >= 0; i--) { + System.out.print(arr[i] + " "); + } + } +} + +Q10-- + +public class LargestInArray { + public static void main(String[] args) { + int[] arr = {34, 23, 12, 45, 67, 89, 234, 26, 10, 30, 43}; + + int max = arr[0]; // Assume the first element is the largest initially + + for (int i = 1; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; // Update max if the current element is larger + } + } + + System.out.println("The largest element in the array is: " + max); + } +} +