Write a C program that reads a number from the keyboard and then outputs the seq
ID: 673063 • Letter: W
Question
Write a C program that reads a number from the keyboard and then outputs the sequence of numbers generated as described above. If the Collatz conjecture is false, it is possible that the sequence does not terminate. Of course this is highly unlikely since no counterexample has been found to date. However, just to be on the safe side, your program should terminate in case the length of the sequence is very large, say more than 10000.
For example, if the input is 3, the output should be the following sequence of values: 3, 10, 5, 16, 8, 4, 2, 1
If the input is 7, the output should be: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Explanation / Answer
# include <stdio.h>
int main()
{
int n,count=0;
printf("Enter a number to get collatz: ");
scanf("%d",&n);
printf("%d,",n);
while(n!=1&&n>1&&count<10000){
if(n%2==1){
n=3*n+1;
printf("%d,",n);
count++;
}else{
n=n/2;
printf("%d,",n);
count++;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.