Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number Pattern cutoff

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?

like image 417
hobo Avatar asked Jun 26 '26 05:06

hobo


1 Answers

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;
    }
like image 90
ortis Avatar answered Jun 27 '26 19:06

ortis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!