Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions 223012252
Original file line number Diff line number Diff line change
@@ -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);
}
}