Loops in Java (Exercise 7)

preview_player
Показать описание
Java Programming: Programming Question on Loops in Java Programming
Topics Discussed:
1. Writing a program that reads an integer n and displays the nth Fibonacci number.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #Loops #Fibonnacci
Рекомендации по теме
Комментарии
Автор

these exercises are very helpful in learning how to use the loops and variables, thanks for adding them !

kostavsheoran
Автор

you can also change result to be =1 and if the for loop will not be ecsecuted (n=1 ||n=2) that it will print 1

blackpanda
Автор

Here is my solution:

Scanner s = new Scanner(System.in);
System.out.print("enter a number: ");
int n = s.nextInt();

if (n == 1 || n == 2)
System.out.println("Fibonaccy number is 1");

else {
int Result1 = 0;
int Result2 = 1;
int Result3 = 1;

for(int i = 2; i < n ; i++) {
Result1 = Result2;
Result2 = Result3;
Result3 = Result1 + Result2;
}
System.out.println("Fibonacci number is " + Result3);
}

hikmathasanov
Автор

public static void main(String args[]) {

Scanner num = new Scanner(System.in);
int a = 0, b = 1, c = 0;
int n = num.nextInt();
for (int i = 2; i < n; i++) {
c = a + b;
a = b;
b = c;

}
System.out.println(c);

}
}

thivagarmurugan
Автор

Awesome videos man I love Neso Academy <3
BTW I used the long data type for the result, v1, and v2 variables to get the larger fibonacci numbers (e.g. n = 50)

boooost
Автор

The explanation is too good. But I have a question in many books fibonacci series is 0, 1, 1, 2, 3, 5, .... But in your example it is starting from 1 and it has a missing element which is 0. For 0 the serial of finding fibonacci nth number is different from yours.Any explanation for that please?

NakibAkash
Автор

The problem that was happening in this example can also be solved if we simply print v2 instead of printing the result.

khushiagarwal
Автор

sir the Syllabus covered is not complete.
kindly update it fast if possible

chiragbudhiraja
Автор

i've done it with a nested for loop. it looks messier but it doesn't have the problem with 1'st and 2'nd fib. numbers

Scanner scan = new Scanner(System.in);
System.out.print("Enter n'th fibbonacci number:");
int nth = scan.nextInt();
int fibo = 1;
int fibo1 = 1;
for (int i = 0; i <= nth; i++) {
fibo=fibo1 - fibo;
for (int j = i; j==i; j++)
fibo1 += fibo;

}
scan.close();
" + fibo);

theripper
Автор

Sir, your explanation is good.When I executed the same program it is printing all the series upto to the entered number.What to do to print the only nth fibonocci number?

jeevithaswamy
Автор

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("enter a message: ");

int input = s.nextInt();

int value = 1;
int value2 = 1;
int result = 0;

for (int i = 2 ; i < input ; i++) {
result = value + value2;
value = value2;
value2 = result;
}
System.out.print(result);
}
}

Bibingka
Автор

public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number: ");
int n = s.nextInt();
int i; int fb1 = 1; int fb2 = 1; int sum;
for(i = 2; i < n ; i += 1) {
sum = fb1 + fb2;
fb1 = fb2;
fb2 = sum;
}
System.out.println("FB Number of your input " + n + " Is: " + fb2);
}
}

madasmsm
Автор

Kinda different way:

Scanner input = new Scanner(System.in);
System.out.print("Tell me your number: ");

int n = input.nextInt(), a=0, b=1, result=0;

for (int i=1; i != n ; i++){
result = a + b ;
a = b;
b = result;


}
System.out.println("Number in the position " +n+ " of the Fibonacci is: " +result);

_W.S_CEO
Автор

The variable has to be declared as long.

elqbano
Автор

int a=1, b=0, c=0;
for(int i=1;i<=n;i++)
{
b=a;
a=c;
c=a+b;
}
System.out.println(c+" ");

HOPE_-lomz
Автор

import java.util.Scanner;
public class LoopsEx7 {
public static void main(String[] args) {

Scanner x = new Scanner(System.in);
int n = x.nextInt();
int a = 1, b = 1, c = 0;
if (n == 1||n == 2) {
System.out.println("1");
}else
for (int i = 3;i <= n;i++) {
c = a+b;
a = b;
b = c;
}
System.out.println(c);
}
}

gakobako
Автор

import java.util.*;
public class Fibonacci{
static int a=1;
static int b=1;
static int c=0;

public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("enter a nth number of fibonacci series to find nth term");
int n=sc.nextInt();
if(n==1||n==2){
System.out.println(c=1);
}
for(int i=1;i<=n-2;i++){
c=a+b;
a=b;
b=c;
}
System.out.println(n+"th term of fibbonaci is :"+c);
}
}

wasimpatel
Автор

Great video. I initialized the sum and the "V1" (named "prev" in my code) to be zero. And I did not have a problem with the first and second fibonacci values.
My code:

import java.util.Scanner;

class Exercise7 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);
System.out.print("Enter nth value: ");

int n = s.nextInt();
int sum=0;
int prev=0;
int next=1;

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

prev=next;
next=sum;

sum = prev + next;
}
System.out.print("The nth fibonacci value is: " + sum);
}


}

JimmyRamokgopa
Автор

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); //program displays the nth number of the Fibonacci sequence int a, b, n; a = b = 1; int sum = 0; System.out.print("Enter n: "); n = input.nextInt(); System.out.println("Fibonacci sequence: "); System.out.print("1 1 "); for(int i = 1; i <= n - 2; i++){ sum = a + b; System.out.print(sum + " "); a = b; b = sum; } System.out.println(); System.out.println("The n number of the Fibonacci sequence: " + b); } }

caesaaar
Автор

I think fibonacci series starts with 0
i.e.

Anurag_Mishra