Fibonacci code looping and outputing 1

import java.util.Scanner;

public class FiboNacci {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int a = 0;
	int b = 1; 
	int sum = a+b;
	
	while(a<=n) {
		if(n==0) {
			System.out.println(a);
			a= a+1;
		} else if (n==1) {
			System.out.println(b);
		} else {
			System.out.println(sum);
			a = b;
			b = sum;
			
		}
		
	}
	
	scn.close();
}

}

My code is looping at 1 in console and just goes in. What is wrong with my approach. I wanted to try and form my own solution for this problem. Kindly let me know my mistake, why is it looping, and how I can correct it?

@sinuscoupon0s_4659768f9d9b587e Explain me your approach little bit bcoz the approach is totally different.And I have updated your code with comments explaination below :

import java.util.Scanner;

public class Main{

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int a = 0;
	int b = 1; 
	int sum = a+b;
    //printing 0th term 
    System.out.println(0);
	//changed condition of loop
	while(a <=n) {
        //no need for else if here
		System.out.println(sum);
			a = b;
			b = sum;
			
		
        //decrement n so that loop will be executed only n times
        n--;
         //update sum bcoz in every iteration sum becomes a + b(i.e. previous sum of two terms)
         sum = a + b;
	}
	
	scn.close();
}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.