Pages

Saturday, August 16, 2025

Factorial in Java

 public class Main

{

    public static int fact(int n) { // void -> int (My Mistake)-01

        if(n == 0 || n == 1) {

            return 1;

        }

        

        

        // recursion

        return n * fact(n-1);

    }

    

    

    

public static void main(String[] args) {

System.out.println(fact(5));

}

}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...