Solution the 100 number : 3n +1 on UVA
Problem: UVA Question 100 in briefly
Input an integer number . The loop will moving by algorithm untill the number become 1.
The number become 1 by looping
How the times looping for number becoming 1 , print this number .
For example:
Input a number 22 . The 22 number becoming 1 the loop moving 16 times so the output is 16
Consider UVA problem number 100 algorithm
Step 1 : Input n
Step 2: print n
Step 3: if n=1 the program stop
Step 4: if n= ODD then n <------- 3n="" font="" nbsp="">------->
Step 5 : else n <---------------- font="" n="" nbsp="">---------------->
Step 6: Go to line 2
Code of 3*n + 1
#include
using namespace std;
int main(){
int i,j,n,count=1,max;
cin>>i>>j;
max=0;
cout< while(i<=j){
count=1;
n=i;
while(n!=1){
if(n%2==0){
n=n/2;
}
else{
n=3*n + 1;
}
count++;
}
if(count>max){
max=count;
}
i++;
}
cout< return 0;
}
using namespace std;
int main(){
int i,j,n,count=1,max;
cin>>i>>j;
max=0;
cout< while(i<=j){
count=1;
n=i;
while(n!=1){
if(n%2==0){
n=n/2;
}
else{
n=3*n + 1;
}
count++;
}
if(count>max){
max=count;
}
i++;
}
cout<
}