Computer Science, asked by chatalapallymadhuri2, 2 months ago

Write a program that accepts two numbers from the Command Line and prints them out. Then use a for loop to print the next 13 numbers in the sequence where each number is the sum of the previous two. For example:​

Answers

Answered by sohail020803
1

Explanation:

The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Here we will write three programs to print fibonacci series 1) using for loop 2) using while loop 3) based on the number entered by user

To understand these programs, you should have the knowledge of for loop and while loop.

If you are new to java, refer this java programming tutorial to start learning from basics.

Example 1: Program to print fibonacci series using for loop

public class JavaExample { public static void main(String[] args) { int count = 7, num1 = 0, num2 = 1; System.out.print("Fibonacci Series of "+count+" numbers:"); for (int i = 1; i <= count; ++i) { System.out.print(num1+" "); /* On each iteration, we are assigning second number * to the first number and assigning the sum of last two * numbers to the second number */ int sumOfPrevTwo = num1 + num2; num1 = num2; num2 = sumOfPrevTwo; } } }

Output:

Fibonacci Series of 7 numbers:0 1 1 2 3 5 8

Example 2:

Answered by arkadyutidutta2007
0

Explanation:

The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Here we will write three programs to print fibonacci series 1) using for loop 2) using while loop 3) based on the number entered by user

To understand these programs, you should have the knowledge of for loop and while loop.

If you are new to java, refer this java programming tutorial to start learning from basics.

Example 1: Program to print fibonacci series using for loop

public class JavaExample { public static void main(String[] args) { int count = 7, num1 = 0, num2 = 1; System.out.print("Fibonacci Series of "+count+" numbers:"); for (int i = 1; i <= count; ++i) { System.out.print(num1+" "); /* On each iteration, we are assigning second number * to the first number and assigning the sum of last two * numbers to the second number */ int sumOfPrevTwo = num1 + num2; num1 = num2; num2 = sumOfPrevTwo; } } }

Output:

Fibonacci Series of 7 numbers:0 1 1 2 3 5 8

Example 2:

Similar questions