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

anyone help me ? Write a program that request a positive integer as input and th

ID: 3886937 • Letter: A

Question


anyone help me ?

Write a program that request a positive integer as input and then follows this procedure: If the number is even, divide it by 2. If not, multiply the number by 3 and then add 1. Repeat this process with the resulting number and continue repeating until the 1 is reached. After the 1 is reached, the program should display the number of iterations required to reach the number 1 When the program requests input, it should check that the user inputs a positive integer greater than 1. If not, an error message should be generated.

Explanation / Answer

#include <stdio.h>

int main() {

//code

int n, count = 0;

printf("Enter the number : ");

scanf("%d",&n);

if(n <= 1)

printf("Please enter valid number");

else

{

while(n > 1)

{

if(n % 2 == 0)

n = n/2;

else

n = n*3 + 1;

if(n == 1)

break;

else

count++;

}

}

if(count != 0)

printf("Total iterations : %d", count+1);

}

OUTPUT:

Enter the number : 13

Total iterations : 9