Neha Java
Neha Java
OUTPUT:
Hello, World!
2. Sum of two numbers.
import java.util.Scanner;
OUTPUT:
Enter the first integer: 5
Enter the second integer: 10
The sum of 5 and 10 is 15
3. Print Prime number series.
OUTPUT:
Prime numbers up to 20 : 2 3 5 7 11 13 17
4. Check weather the given number is Even OR Odd.
import java.util.Scanner;
if (isEven(number)) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
OUTPUT:
Enter a number: 17
number is odd
5. Print Square pattern using for Loop.
import java.util.Scanner;
OUTPUT:
Enter the size of the square: 5
*****
*****
*****
*****
*****
6. Use of all Operators.
public class OperatorExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int addition = a + b;
boolean greaterThan = (a > b);
boolean andOperator = (true && false);
int x = 10;
x += 5;
int counter = 5;
counter++;
int bitwiseAnd = a & b;
int max = (a > b) ? a : b;
OUTPUT:
Addition: 15
Greater Than: true
And Operator: false
Incremented Counter: 6
Bitwise AND: 0
Max: a
7. Print Fibonacci Series .
public class FibonacciSeries {
public static void main(String[] args) {
int numTerms = 10;
int prev = 0, current = 1;
OUTPUT:
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
8. Write program to execute the Control Statements.
import java.util.Scanner;
// If statement
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
// Switch statement
System.out.print("Enter a day of the week (1-7): ");
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
// While loop
System.out.print("Enter a positive number: ");
int count = scanner.nextInt();
while (count > 0) {
System.out.println("Countdown: " + count);
count--;
}
// For loop
System.out.print("Enter a number for the multiplication table: ");
int tableNumber = scanner.nextInt();
System.out.println("Multiplication table for " + tableNumber + ":");
for (int i = 1; i <= 10; i++) {
System.out.print(tableNumber * i + “ “);
}
}
}
OUTPUT:
Enter the number: 4
The number is positive.
Enter a day of the week (1-7): 3
Wednesday
Enter a positive number: 4
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Enter a number for the multiplication table: 2
Multiplication table for 2 :
2 4 6 8 10 12 14 16 18 20
9. Find the K th odd number from the series provided by
user without Using array.
import java.util.Scanner;
int count = 0;
int kthOdd = -1;
int start = 0;
if (number % 2 != 0) {
count++;
if (count == k) {
kthOdd = number;
break;
}
}
start = i + 1;
}
}
if (kthOdd != -1) {
System.out.println("The " + k + "th odd number in the series is: " +
kthOdd);
} else {
System.out.println("There are less than " + k + " odd numbers in the
series.");
}
}
}
OUTPUT:
Enter the series of numbers (space-separated): 3 5 2 8 7 6 9
Enter the value of K: 3
The 3rd odd number in the series is: 7
10. Without using array find the 2 nd largest number
from the inputs Provided by user (end of inputs should be
with -1). *without using array.
import java.util.Scanner;
while (true) {
input = scanner.nextInt();
if (input == -1) {
break;
}
if (input > largest) {
secondLargest = largest;
largest = input;
} else if (input > secondLargest && input != largest) {
secondLargest = input;
}
}
if (secondLargest != Integer.MIN_VALUE) {
System.out.println("The second largest number is: " + secondLargest);
} else {
System.out.println("There is no second largest number.");
}
}
}
OUTPUT:
Enter a series of numbers (-1 to end): 7 4 9 2 6 -1
The second largest number is: 7
11. Output the number of distinct elements from the
shorted Sequence provided by user (end of inputs should
be with -1).
import java.util.Scanner;
public class CountDistinctElements {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sorted sequence of numbers (-1 to end):");
if (input == -1) {
break;
}
if (input != prev) {
distinctCount++;
}
prev = input;
}
System.out.println("The number of distinct elements is: " +
distinctCount);
}
}
OUTPUT:
Enter a sorted sequence of numbers (-1 to end): 2 2 3 5 5 5 7 7 7 7 7 –1
The number of distinct elements is: 4
12. Write a program of 2D and 3D array .
// 2D ARRAY
public class TwoDArrayExample {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 3D ARRAY
public class ThreeDArrayExample {
public static void main(String[] args) {
int[][][] cube = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
OUTPUT:
Element at (1, 2): 6
Element at (1, 0, 2): 9
13. Write a java code to explain single and mulƟple
inheritance .
// single inheritance
class Parent {
void display() {
System.out.println("This is the parent class.");
}
}
OUTPUT:
This is the parent class.
This is the child class.
Method A implementation.
Method B implementation.
14. Write a program of exception handeling (try catch
throw) .
OUTPUT:
An ArithmeticException occurred : java.lang.ArithmeticException: / by
zero
15. Write a program of Method overriding .
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
OUTPUT:
Animal makes a sound
Dog barks