// by Sabrina Rispin // Activity Section: 3 // Date: 22/3/15 // Description: The wonderous function prints out a string of number // following the rule that the next number in the sequence is: // a) the previous num/2 (if num was even) // b) the previous num * 3 + 1 (if num was odd) #include <stdio.h> #include <stdlib.h> #include <assert.h> int printWondrous (int start); int main (int argc, char * argv[]) { int startNum; int numOfTerms; printf("Pleas enter a starting number: "); scanf ("%d", &startNum); numOfTerms = printWondrous (startNum); printf("Number of Terms: %d\n", numOfTerms); return EXIT_SUCCESS; } int printWondrous (int start) { int numOfTerms = 1; int num = start; printf ("%d ", num); while (num != 1) { if ((num % 2) == 0) { num /= 2; printf ("%d ", num); } else { num = 3 * num + 1; printf ("%d ", num); } numOfTerms++; } printf ("\n"); return numOfTerms; }
Download file: wondrous.c
(1.0 KB)