Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that reads a number from the keyboard and then outputs the seq

ID: 672591 • 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 Collatz(int num)
{
int count = 0;       /*Initialize count to 0*/
while(count != 10000) /*Loop repeats for 10000 times.*/
{
if(num == 1)       /*If num is 1.*/
{
printf("%i ",num);/*Print it.*/
return 0;           /*Return*/
}
else if(num % 2 == 0)/*If num is even.*/
{
printf("%i ",num); /*Print it.*/
num /= 2;           /*Half the number.*/
}
else               /*If neither condition satifies, i.e., if num is odd.*/
{
printf("%i ",num);   /*Print it.*/
num = num * 3 + 1;   /*Triple the number and add 1 to it.*/
}
count++;                 /*Increment the count.*/
}
return 0;
}

int main()
{
int num;
printf("Enter the value of n: "); /*Prompt the user to enter the value.*/
scanf("%i",&num);                   /*Read the value.*/
printf("The Collatz Conjecture is: ");/*Print a prompt.*/
Collatz(num);                           /*Call the function to print the sequence of numbers.*/
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote