Ad
Code
Diff
  • import java.util.Scanner;
    class Fibonacci
    {
        public static void main(String args[])
        {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter the number of Iterations:");
            int inputNum = scanner.nextInt();
            int firstNum = 0,secondNum = 1;
            System.out.println(firstNum+"\n"+secondNum);
            //for loop is faster than functional loop such as range
            for(int i = 1;i <=inputNum; i++)
            {
                int nextNum = firstNum + secondNum;
                System.out.println(nextNum);
                firstNum = secondNum;
                secondNum = nextNum;
            }
        }
    }
    • import java.util.Scanner;
    • class Fibonacci
    • {
    • public static void main(String args[])
    • {
    • Scanner sc = new Scanner(System.in);
    • Scanner scanner = new Scanner(System.in);
    • System.out.println("Enter the number of Iterations:");
    • int n = sc.nextInt();
    • int a = 0;
    • int b = 1;
    • System.out.println(a);
    • System.out.println(b);
    • for(int i = 1;i <=n; i++)
    • int inputNum = scanner.nextInt();
    • int firstNum = 0,secondNum = 1;
    • System.out.println(firstNum+"\n"+secondNum);
    • //for loop is faster than functional loop such as range
    • for(int i = 1;i <=inputNum; i++)
    • {
    • int c = a + b;
    • System.out.println(c);
    • a=b;
    • b=c;
    • int nextNum = firstNum + secondNum;
    • System.out.println(nextNum);
    • firstNum = secondNum;
    • secondNum = nextNum;
    • }
    • }
    • }