Files
javalab/file.org
Amneesh Singh 9006831163 P1-P5: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-04-10 05:54:43 +05:30

3.4 KiB

Programs are followed by their respective inputs and outputs i.e, both stdin and stdout are shown together

Sum of two numbers

class P1 {
  public static void main(String[] _args) {
      int a = 44;
      int b = 56;

      System.out.println(a + " + " + b + " = " + (a + b));
  }
}

Output:

44 + 56 = 100

Factorial of a number

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);
  }
}

Output:

5! = 120

Greatest of 3 numbers

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);
  }
}

Output:

max(5, -5, 55) = 55

Fibbonaci series

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);
    }
}

max(5, -5, 55) = 55

First 10 elements of the fibonacci series:
0
1
1
2
3
5
8
13
21
34

Percentage marks and grades for 3 subjects

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)));
    }
}
Subject 1 grade: D
Subject 2 grade: A
Subject 3 grade: F
Overall Percentage: 66.666664%