recursion – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 16 Aug 2022 15:58:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg recursion – Programmerbay https://programmerbay.com 32 32 Factorial Program in Java https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/ https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/#respond Fri, 12 Aug 2022 17:19:45 +0000 https://programmerbay.com/?p=5103 The program simply prints factorial of a given number.  A factorial is calculated by multiplying  all the numbers starting from 1 to all the way nth number. For example, factorial of 5 would be, 1 X 2 X 3 X 4 X 5, which is 120.

Screenshot from 2022 08 14 15 58 07

The same logic will be used in this article to calculate the same. However, there are various approaches to achieve this functionality.

Below are some approaches to calculate factorial of a number :

  1. Using Loop
  2. Using recursion
  3. Using ternary operator
  4. Using Java 8 reduce method

Example 1. Java program to calculate factorial of a number using Loop

In this example, we have simply used “for loop” and on each iteration, we’re incrementing the counter by 1 until it reaches to nth number.  Within the looping body, we’re multiplying the counter with the factorial variable which is having an initial value of 1, and storing the result in factorial variable itself. 

Program :

import java.util.Scanner;
public class FactorialProgram {
    public static void main(String[] args) {
        int number;
        int factorial = 1;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a number :: ");
        number = input.nextInt();
        for (int i = 1 ; i <= number; i++){
            factorial = factorial * i;
        }
        System.out.println("Factorial of "+number+" is :: "+ factorial);
    }
}

Output:

Please enter a number :: 5
Factorial of 5 is :: 120

Example 2. Java program to calculate factorial of a number using Recursion

Recursion function refers to a method which calls itself and terminates when some condition is triggered. Below is the example to calculate the factorial with the help of recursion function

Program:

public class FactorialProgram {
    static long getFactorial(int number) {
        if (number == 1) {
            return 1;
        } else {
            return number * getFactorial(number - 1);
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number;
        long factorial;
        System.out.print("Please enter a number :- ");
        number = input.nextInt();
        factorial = getFactorial(number);
        System.out.println("Factorial of " + number + " = " + factorial);
    }
}

Output:

]]>
https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/feed/ 0
Java program to print Fibonacci series https://programmerbay.com/java-program-to-print-fibonacci-series/ https://programmerbay.com/java-program-to-print-fibonacci-series/#respond Tue, 07 Jul 2020 06:42:38 +0000 https://www.programmerbay.com/?p=4146 The program prints Fibonacci Series as the output . The functionality can be achieved in two ways by simply using a loop or recursion. Lets first get into Loops

  • Fibonacci Series Using For Loop
  • Fibonacci Series Using While Loop

A series is said to be Fibonacci series if each and every number in the given series is figured out by adding two immediate previous numbers. Here’s the sequence,  0, 1, 1, 2, 3, 5, 8, 13, 21 and so on.

Java program to print Fibonacci series using For Loop

Program:

public class FibonacciSeries {
    public static void main(String[] args) {
        int sum, firstNum = 0, secondNum, range = 10;
        System.out.println(" Fibonacci Series : ");
        secondNum = 1;
        for (int i = 1; i <= range; i++) {
            System.out.print(firstNum+" ");
            sum = firstNum + secondNum;
            firstNum = secondNum;
            secondNum = sum;
        }
}
}

Java program to print Fibonacci series using While Loop

program :

public class FibonacciSeries {
    public static void main(String[] args) {
        int sum, firstNum = 0, secondNum, range = 10, i=0;
        System.out.println(" Fibonacci Series : ");
        secondNum = 1;
        while(i <= range) {
            System.out.print(firstNum + " ");
            sum = firstNum + secondNum;
            firstNum = secondNum;
            secondNum = sum;
            i++;
        }
    }
}

Output:

Fibonacci Series : 
0 1 1 2 3 5 8 13 21 34

 

Another approach is to print the Fibonacci series recursively. In this, a method calls itself until a condition turns to false. Here’s is the code that does the same.

Java program to print Fibonacci series using recursion 

program: 

public class FibonacciSeries {
    public static void main(String[] args) {
        int firstNum = 0, secondNum = 1, range = 10;
        System.out.println(" Fibonacci Series : ");
        printFibonacciSeriesRecursively(firstNum, secondNum, range);
    }

    static void printFibonacciSeriesRecursively(int num1, int num2, int range) {
        if (range < 1) {
            return;
        } else {
            System.out.print(num1 + " ");
        }
        printFibonacciSeriesRecursively(num2, (num1 + num2), range - 1);
    }
}

 

Output:

Fibonacci Series : 
0 1 1 2 3 5 8 13 21 34

]]>
https://programmerbay.com/java-program-to-print-fibonacci-series/feed/ 0