Question:I have to write a program that takes one integer and follows these rules. First, print this number. When the number is even, change the number to itself divided by 2. Print the resulting number. When the number is odd, change the number to itself multiplied by 3. Then add 1 to the number. Print the result. Continue following these rules until the number you have left is 1 (which will be printed as well).
e.g. given 22 print:
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

ANSWER:
#include <stdio.h>
#include<conio.h>
int main() {
    int x;
    printf("Enter a number: ");
    scanf("%d", &x);
    printf("%d\n", x);
    do {
        if (x % 2 == 0)
            x /= 2;
        else {
            x *= 3;
            x++;
        }
        printf("%d\n", x);
    } while (x != 1);

    getchar ();
    return 0;
}
 


SOURCE:http://cboard.cprogramming.com/c-programming/134435-loop-problem.html

0 মন্তব্য(গুলি):