extras: add P16-P23, P25

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-06-07 03:37:31 +05:30
parent 14898356a8
commit cc83093210
9 changed files with 153 additions and 0 deletions

5
P16.java Normal file
View File

@@ -0,0 +1,5 @@
public class P16 {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

9
P17.java Normal file
View File

@@ -0,0 +1,9 @@
public class P17 {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = a + b;
System.out.print(c);
}
}

11
P18.java Normal file
View File

@@ -0,0 +1,11 @@
public class P18 {
public static void main(String[] _args) {
int a = 5, b, f;
b = f = a;
while (b-- > 1)
f *= b;
System.out.println(a + "! = " + f);
}
}

17
P19.java Normal file
View File

@@ -0,0 +1,17 @@
import java.util.Scanner;
public class P19 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = scanner.nextInt();
System.out.print("Enter the second number: ");
int b = scanner.nextInt();
System.out.println("Sum: " + (a + b));
scanner.close();
}
}

14
P20.java Normal file
View File

@@ -0,0 +1,14 @@
public class P20 {
public static void main(String[] args) {
int n = 10;
System.out.print("Fibonacci Series of length " + n + ": ");
fibonacci(0, 1, n);
}
public static void fibonacci(int a, int b, int n) {
if (n == 0)
return;
System.out.print(a + " ");
fibonacci(b, a + b, n - 1);
}
}

23
P21.java Normal file
View File

@@ -0,0 +1,23 @@
import java.util.Scanner;
public class P21 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a: ");
int a = scanner.nextInt();
System.out.print("Enter b: ");
int b = scanner.nextInt();
System.out.print("Enter c: ");
int c = scanner.nextInt();
if ( a > b && a > c)
System.out.print("a is the greatest integer");
else if ( b > a && b > c)
System.out.print("b is the greatest integer");
else
System.out.print("c is the greatest integer");
}
}

25
P22.java Normal file
View File

@@ -0,0 +1,25 @@
import java.util.Scanner;
public class P22 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
System.out.println(n + " is " + (prime(n) ? "" : "not ") + "a prime number.");
scanner.close();
}
public static boolean prime(int number) {
if (number <= 1)
return false;
for (int i = 2; i <= Math.sqrt(number); i++)
if (number % i == 0)
return false;
return true;
}
}

24
P23.java Normal file
View File

@@ -0,0 +1,24 @@
public class P23 {
private String parentVariable = "Parent";
public void parentMethod() {
System.out.println("Parent method");
Child child = new Child();
child.childMethod();
}
public class Child {
private String childVariable = "Child";
public void childMethod() {
System.out.println("Child method");
System.out.println("Accessing parent variable: " + parentVariable);
System.out.println("Accessing child variable: " + childVariable);
}
}
public static void main(String[] args) {
P23 parent = new P23();
parent.parentMethod();
}
}

25
P25.java Normal file
View File

@@ -0,0 +1,25 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class P25 {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Batu");
names.add("Amba");
names.add("John");
names.add("Quandangle");
names.add("Harikrishan");
System.out.println("Names before sorting:");
for (String name : names)
System.out.print(name + " ");
Collections.sort(names);
System.out.println("\nNames after sorting:");
for (String name : names)
System.out.print(name + " ");
}
}