UVa 100 – The 3n + 1 problem solution in C language

**Before watching this make sure that you’ve tried enough**

#include<stdio.h>

int func(int a,int b);

int main()
{
    int i,j,result;
    while(scanf("%d %d",&i,&j)!=EOF){
        result = func(i , j);
        printf("%d %d %d\n",i,j,result);
    }

    return 0;
}

int func(int a,int b){
    int x,count = 1,max =0;
    if(a > b){
        int temp = a;
        a = b;
        b = temp;
    }
    while(a <= b){
        x = a;
        while(x != 1){
            if(x % 2 == 0) x = x / 2;
            else x = (3 * x) + 1;
            count++;
            if(x == 1)break;
        }
        if(count > max)max = count;
        count = 1;
        a++;
    }
    return max;
}

 

If this solution helped you, please share this with your friends.
And if you have anything to say, let me know in the comment section.

Leave a Comment