How would I go about adding a new line on the 10th term on this zybooks question
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
if (n % 2 == 1) {
n = 3 * n 1;
}
else {
n = n / 2;
}
printf("%d\t",n);
}
printf("\n");
return 0;
}
CodePudding user response:
This solution uses a counter variable i
which increments until 10 is reached. Once it is reached a newline character (\n
) is printed.
#include <stdio.h>
int main(void)
{
int n, i = 0;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
i ;
(n % 2 == 1) ? (n = 3*n 1) : (n = n/2);
if (i == 10) {
i = 0;
printf("\n");
}
else {
printf("%d\t", n);
}
}
printf("\n");
return 0;
}