From cc83093210d7b3bc948205ed4ed6285bf74929a7 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Wed, 7 Jun 2023 03:37:31 +0530 Subject: [PATCH] extras: add P16-P23, P25 Signed-off-by: Amneesh Singh --- P16.java | 5 +++++ P17.java | 9 +++++++++ P18.java | 11 +++++++++++ P19.java | 17 +++++++++++++++++ P20.java | 14 ++++++++++++++ P21.java | 23 +++++++++++++++++++++++ P22.java | 25 +++++++++++++++++++++++++ P23.java | 24 ++++++++++++++++++++++++ P25.java | 25 +++++++++++++++++++++++++ 9 files changed, 153 insertions(+) create mode 100644 P16.java create mode 100644 P17.java create mode 100644 P18.java create mode 100644 P19.java create mode 100644 P20.java create mode 100644 P21.java create mode 100644 P22.java create mode 100644 P23.java create mode 100644 P25.java diff --git a/P16.java b/P16.java new file mode 100644 index 0000000..c31ee26 --- /dev/null +++ b/P16.java @@ -0,0 +1,5 @@ +public class P16 { + public static void main(String[] args) { + System.out.println("Hello World"); + } +} diff --git a/P17.java b/P17.java new file mode 100644 index 0000000..7c2fd1d --- /dev/null +++ b/P17.java @@ -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); + } +} diff --git a/P18.java b/P18.java new file mode 100644 index 0000000..cda3adf --- /dev/null +++ b/P18.java @@ -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); + } +} diff --git a/P19.java b/P19.java new file mode 100644 index 0000000..fd4bc55 --- /dev/null +++ b/P19.java @@ -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(); + } +} diff --git a/P20.java b/P20.java new file mode 100644 index 0000000..217c6a2 --- /dev/null +++ b/P20.java @@ -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); + } +} diff --git a/P21.java b/P21.java new file mode 100644 index 0000000..ef412e8 --- /dev/null +++ b/P21.java @@ -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"); + } +} diff --git a/P22.java b/P22.java new file mode 100644 index 0000000..ca04757 --- /dev/null +++ b/P22.java @@ -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; + } +} diff --git a/P23.java b/P23.java new file mode 100644 index 0000000..acce013 --- /dev/null +++ b/P23.java @@ -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(); + } +} diff --git a/P25.java b/P25.java new file mode 100644 index 0000000..1ae6820 --- /dev/null +++ b/P25.java @@ -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 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 + " "); + } +}