commit 90068311636ca0566d53a6a0b5f2b7e636e76588 Author: Amneesh Singh Date: Mon Apr 10 05:54:43 2023 +0530 P1-P5: init Signed-off-by: Amneesh Singh diff --git a/P1.java b/P1.java new file mode 100644 index 0000000..57c0cd7 --- /dev/null +++ b/P1.java @@ -0,0 +1,8 @@ +class P1 { + public static void main(String[] _args) { + int a = 44; + int b = 56; + + System.out.println(a + " + " + b + " = " + (a + b)); + } +} \ No newline at end of file diff --git a/P2.java b/P2.java new file mode 100644 index 0000000..9275384 --- /dev/null +++ b/P2.java @@ -0,0 +1,11 @@ +class P2 { + public static void main(String[] _args) { + int a = 5, b, f; + b = f = a; + + while (b-- > 1) + f *= b; + + System.out.println(a + "! = " + f); + } +} \ No newline at end of file diff --git a/P3.java b/P3.java new file mode 100644 index 0000000..5941fc3 --- /dev/null +++ b/P3.java @@ -0,0 +1,14 @@ +class P3 { + public static void main(String[] _args) { + int a = 5, b = -5, c = 55, mx; + + if (a > b && a > c) + mx = a; + else if ( b > a && b > c) + mx = b; + else + mx = c; + + System.out.println("max(" + a + ", " + b + ", " + c + ") = " + mx); + } +} \ No newline at end of file diff --git a/P4.java b/P4.java new file mode 100644 index 0000000..b4e7746 --- /dev/null +++ b/P4.java @@ -0,0 +1,15 @@ +class P4 { + public static void main(String[] _args) { + int n = 10; + int a = 0, b = 1; + System.out.println("First " + n + " elements of the fibonacci series:"); + new P4().fibo(n, a, b); + } + + void fibo(int n, int a, int b) { + if (n == 0) + return; + System.out.println(a); + fibo(n - 1, b, a + b); + } +} \ No newline at end of file diff --git a/P5.java b/P5.java new file mode 100644 index 0000000..ebee742 --- /dev/null +++ b/P5.java @@ -0,0 +1,20 @@ +import java.util.function.Consumer; + +class P5 { + public static void main(String[] _args) { + int sub[] = { 64, 91, 45 }; + P5 obj = new P5(); + int sum = 0; + + for (int i = 0; i < sub.length; i++) { + obj.grade(sub[i], i + 1); + sum += sub[i]; + } + + System.out.println("Overall Percentage: " + (float) sum / 3 + "%"); + } + + void grade(int n, int s) { + System.out.println( "Subject " + s + " grade: " + (char)( n >= 90 ? 'A' : n < 50 ? 'F' : 'A' + (9 - n / 10))); + } +} diff --git a/file.org b/file.org new file mode 100644 index 0000000..1ed50e6 --- /dev/null +++ b/file.org @@ -0,0 +1,155 @@ +#+LATEX_CLASS_OPTIONS: [a4paper,12pt] +#+LATEX_HEADER: \usepackage[margin=0.5in]{geometry} +#+LATEX_HEADER: \usepackage{fontspec} +#+LATEX_HEADER: \setmainfont{LiberationSerif} +#+LATEX_HEADER: \date{} +#+OPTIONS: toc:nil + +|------------------------------------------------------------------------------------------------------------| +| Programs are followed by their respective inputs and outputs i.e, both stdin and stdout are shown together | +|------------------------------------------------------------------------------------------------------------| + +* Sum of two numbers +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src java :tangle P1.java :results output :exports both :wrap src text +class P1 { + public static void main(String[] _args) { + int a = 44; + int b = 56; + + System.out.println(a + " + " + b + " = " + (a + b)); + } +} +#+end_src + +Output: +#+RESULTS: +#+begin_src text +44 + 56 = 100 +#+end_src + +#+latex: \clearpage + +* Factorial of a number +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src java :tangle P2.java :results output :exports both :wrap src text +class P2 { + public static void main(String[] _args) { + int a = 5, b, f; + b = f = a; + + while (b-- > 1) + f *= b; + + System.out.println(a + "! = " + f); + } +} +#+end_src + +Output: +#+RESULTS: +#+begin_src text +5! = 120 +#+end_src + +#+latex: \clearpage + +* Greatest of 3 numbers +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src java :tangle P3.java :results output :exports both :wrap src text +class P3 { + public static void main(String[] _args) { + int a = 5, b = -5, c = 55, mx; + + if (a > b && a > c) + mx = a; + else if ( b > a && b > c) + mx = b; + else + mx = c; + + System.out.println("max(" + a + ", " + b + ", " + c + ") = " + mx); + } +} +#+end_src + +Output: +#+RESULTS: +#+begin_src text +max(5, -5, 55) = 55 +#+end_src + +#+latex: \clearpage + +* Fibbonaci series +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src java :tangle P4.java :results output :exports both :wrap src text +class P4 { + public static void main(String[] _args) { + int n = 10; + int a = 0, b = 1; + System.out.println("First " + n + " elements of the fibonacci series:"); + new P4().fibo(n, a, b); + } + + void fibo(int n, int a, int b) { + if (n == 0) + return; + System.out.println(a); + fibo(n - 1, b, a + b); + } +} +#+end_src + +max(5, -5, 55) = 55 +#+RESULTS: +#+begin_src text +First 10 elements of the fibonacci series: +0 +1 +1 +2 +3 +5 +8 +13 +21 +34 +#+end_src + +#+latex: \clearpage + +* Percentage marks and grades for 3 subjects +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src java :tangle P5.java :results output :exports both :wrap src text +import java.util.function.Consumer; + +class P5 { + public static void main(String[] _args) { + int sub[] = { 64, 91, 45 }; + P5 obj = new P5(); + int sum = 0; + + for (int i = 0; i < sub.length; i++) { + obj.grade(sub[i], i + 1); + sum += sub[i]; + } + + System.out.println("Overall Percentage: " + (float) sum / 3 + "%"); + } + + void grade(int n, int s) { + System.out.println( "Subject " + s + " grade: " + (char)( n >= 90 ? 'A' : n < 50 ? 'F' : 'A' + (9 - n / 10))); + } +} +#+end_src + +#+RESULTS: +#+begin_src text +Subject 1 grade: D +Subject 2 grade: A +Subject 3 grade: F +Overall Percentage: 66.666664% +#+end_src + +#+latex: \clearpage diff --git a/file.pdf b/file.pdf new file mode 100644 index 0000000..eafc221 Binary files /dev/null and b/file.pdf differ