To display 7, 22, 11, 34, 17, 52, 26, 13, 40, 20 (Qbasic Code)

To display 7, 22, 11, 34, 17, 52, 26, 13, 40, 20.
  • CLS
    a = 7
    FOR i = 1 TO 10
        PRINT a;
        IF a MOD 2 = 0 THEN
            a = a / 2
        ELSE
            a = a * 3 + 1
        END IF
    NEXT i
    END
Explanation:
Here, the numbers are not in normal series / sequence. So, consider the first the first number, It is 7. For the preceding numbers, if the number is divisible by 2 then write the quotient for the next number. If it is not exactly divisible by 2 then multiply the number by 3 and add 1 which will be the next number. Repeat the same process for the next number and so on.

First number is 7
For second number:  7 / 2, the remainder is not 0, so multiply 7 by 3 and add 1. i.e, 7 x 3 + 1 = 22.
For third number: 22 / 2 , the remainder is 0, so just write the quotient. i.e, 11.
For next number: 11 / 2, the remainder is not 0, so multiply 11 by 3 and add 1. i.e, 11 x 3 + 1 = 34
And so on.....

Share :

Twitter
Back To Top

facebook main

counter

Powered by Blogger.