Why in the code in while loop it is n>a and not n>0. I tried n>0 but then the pattern was not printed. By making n>a , the condition will eventually go false then how is the program still running. Please explain this since I’m not able to get this part of the code.
Doubt in while loop, code works fine
@rg361 Don’t think about (n > a) or (n > 0). If you remove while loop like shown below then also answer is correct . Its bcoz a gets changed according to n in the for loops thats why No need to use a while loop.
import java.util.Scanner;
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 c;
if(0 < n && n < 100)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(a+"\t");
c=a+b;
a=b;
b=c;
}
System.out.println();
}
}
else
{
System.out.println("try again");
}
}
}
okay sir, got it. Thanks
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.