Gurleen Java
Gurleen Java
8. Write program that gets a number from the user and generates an integer between
1 and 7 subsequently should display the name of the weekday as per that number
11. Design & execute a program in Java to sort a numeric array and a string array.
12. Calculate the average value of array elements through Java Program.
// Addition
double sum = num1 + num2;
System.out.println("Addition: " + sum);
// Subtraction
double difference = num1 - num2;
System.out.println("Subtraction: " + difference);
// Multiplication
double product = num1 * num2;
System.out.println("Multiplication: " + product);
// Division
if (num2 != 0) {
double quotient = num1 / num2;
System.out.println("Division: " + quotient);
} else {
System.out.println("Error: Division by zero is not allowed.");
}
scanner.close();
System.out.println("BY GURLEEN");
}
}
2.Write a Java program to print result of the following operations. 1. -15 +58 * 45 2. (35+8) % 6 3. 24 + -5*3 /
7 4. 15 + 18 / 3 * 2 - 9 % 3
public class OperationsResults {
public static void main(String[] args) {
System.out.println("-15 + 58 * 45 = " + (-15 + 58 * 45));
System.out.println("(35 + 8) % 6 = " + ((35 + 8) % 6));
System.out.println("24 + (-5 * 3) / 7 = " + (24 + (-5 * 3) / 7));
System.out.println("15 + 18 / 3 * 2 - 9 % 3 = " + (15 + 18 / 3 * 2 - 9 % 3)); System.out.println("by gurleen");
}
}
3.Write a Java program to compute area of: 1) Circle2) rectangle 3) triangle 4) square
import java.util.Scanner;
// Circle
// Rectangle
// Triangle
// Square
scanner.close();
}
4.Write a program to convert temperature from Fahrenheit to Celsius degree using Java.
import java.util.Scanner;
scanner.close();
}
}
6. Design a program in Java to solve quadratic equations using if, if else
import java.util.Scanner;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Roots are: " + root1 + " and " + root2); } else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Root is: " + root);
} else {
System.out.println("No real roots.");
}
scanner.close();
}
}
7. Write a Java program to determine greatest number
of three numbers.
import java.util.Scanner;
import java.util.Scanner;
switch (day) {
case 1: System.out.println("Sunday"); break;
case 2: System.out.println("Monday"); break;
case 3: System.out.println("Tuesday"); break;
case 4: System.out.println("Wednesday"); break;
case 5: System.out.println("Thursday"); break;
case 6: System.out.println("Friday"); break;
case 7: System.out.println("Saturday"); break;
default: System.out.println("Invalid number. Please enter a number between 1 and 7.");
}
System.out.println("By gurleen");
scanner.close();
}
}
9. Construct a Java program to find the number of days in a month.
import java.util.Scanner;
int days;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { days = 29;
} else {
days = 28;
}
break;
default:
days = 0; // Invalid month
}
if (days > 0) {
System.out.println("Number of days: " + days);
} else {
System.out.println("Invalid month.");
}
System.out.println("By gurleen");
scanner.close();
}
}
10. Write a program to sum values of a Single Dimensional array.
import java.util.Scanner;
int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
// Numeric Array
System.out.print("Enter the size of the numeric array: ");
int size = scanner.nextInt();
int[] numArray = new int[size];
Arrays.sort(numArray);
System.out.println("Sorted numeric array: " + Arrays.toString(numArray));
// String Array
System.out.print("Enter the size of the string array: ");
int strSize = scanner.nextInt();
scanner.nextLine(); // Consume newline
String[] strArray = new String[strSize];
Arrays.sort(strArray);
System.out.println("Sorted string array: " + Arrays.toString(strArray)); System.out.println("By gurleen");
scanner.close();
}
}
12. Calculate the average value of array elements through Java Program.
import java.util.Scanner;
int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
if (found) {
System.out.println("Value found in the array.");
} else {
System.out.println("Value not found in the array.");
}
System.out.println("By gurleen");
scanner.close();
}
}
14. Find the index of an array element by writing a program in Java.
import java.util.Scanner;
if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found.");
}
System.out.println("by gurleen");
scanner.close();
}
}
15. Write a Java program to remove a specific element from an array.
import java.util.Scanner;
System.out.println("\nby gurleen");
scanner.close();
}
}
16. Design a program to copy an array by iterating the array.
import java.util.Scanner;
System.out.println("Copied array:");
for (int i = 0; i < copiedArray.length; i++) {
System.out.print(copiedArray[i] + " ");
}
System.out.println("\nby gurleen");
scanner.close();
}
}
17. Write a Java program to insert an element (on a specific position) into Multidimensional array.
import java.util.Scanner;
System.out.println("by gurleen");
scanner.close();
}
}
18. Write a program to perform following operations on strings: 1) Compare two strings. 2) Count string length. 3) Convert
upper case to lower case & vice versa. 4) Concatenate two strings. 5) Print a substring.
import java.util.Scanner;
// 5) Print a substring
System.out.print("Enter start index for substring: ");
int startIndex = scanner.nextInt();
System.out.print("Enter end index for substring: ");
int endIndex = scanner.nextInt();
scanner.nextLine();
if (startIndex >= 0 && endIndex <= str1.length() && startIndex < endIndex) { System.out.println("Substring: " +
str1.substring(startIndex, endIndex)); } else {
System.out.println("Invalid indices.");
}
System.out.println("by gurleen");
scanner.close();
}
}
19. Developed Program & design a method to find the smallest number among three numbers.
import java.util.Scanner;