public class numPattern {
public static void main(String[] args) {
int digit1 = 2;
int digit2 = 7;
int tal = 0;
System.out.print(digit1 + " ");
System.out.print(digit2 + " ");
while (tal < 550) {
tal = digit1 + digit2;
System.out.print(tal + " ");
digit1 = digit2;
digit2 = tal;
}
}
}
This outputs 2, 7, 9, 16, 25, 41, 66......453 and 733
The issue is it should stop at 453 because 733 is way over 550.
What command would make sure the program ends at 453 to meet the the greater or equal to 550 I'm trying to seek?
Increase the value of tal in the while:
int digit1 = 2;
int digit2 = 7;
int tal = 0;
System.out.print(digit1 + " ");
System.out.print(digit2 + " ");
while((tal = digit1 + digit2)< 550)
{
System.out.print(tal + " ");
digit1 = digit2;
digit2 = tal;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With