0% found this document useful (0 votes)
9 views

Fibonacci Codingg ZAIN

The document contains code to calculate the factorial of a positive integer input by the user and generate the Fibonacci sequence up to a positive integer value input by the user. It uses a Scanner to get user input, calculates factorials using a for loop and multiplication, and generates the Fibonacci sequence by adding the two previous values in the series in a for loop.

Uploaded by

sethkartikeya08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Fibonacci Codingg ZAIN

The document contains code to calculate the factorial of a positive integer input by the user and generate the Fibonacci sequence up to a positive integer value input by the user. It uses a Scanner to get user input, calculates factorials using a for loop and multiplication, and generates the Fibonacci sequence by adding the two previous values in the series in a for loop.

Uploaded by

sethkartikeya08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

public class FactorialAndFibonacci {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Factorial Calculation
System.out.print("Enter a positive integer to calculate its factorial: ");
int number = scanner.nextInt();

if (number < 0) {
System.out.println("Invalid input for factorial. Please enter a
positive integer.");
} else {
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + " is: " + factorial);
}

// Fibonacci Generation
System.out.print("Enter the number of Fibonacci numbers to generate: ");
int n = scanner.nextInt();

if (n <= 0) {
System.out.println("Invalid input for Fibonacci series. Please enter a
positive integer.");
} else {
long[] fibonacciNumbers = new long[n];
fibonacciNumbers[0] = 0;
fibonacciNumbers[1] = 1;

for (int i = 2; i < n; i++) {


fibonacciNumbers[i] = fibonacciNumbers[i - 1] + fibonacciNumbers[i
- 2];
}

System.out.println("Fibonacci Series:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacciNumbers[i] + " ");
}
}

scanner.close();
}
}

You might also like