wondrous.c - OpenLearning
/*
 *This code is designed to implement the "Wonderous Sequence" defined 
 *here: 
 *https://www.openlearning.com/courses/99luftballons/Activities/CWondrousFunction
 *
 *Code by: Harry J.E Day <harry@dayfamilyweb.com>
 *Date: 25/04/14
 */

#include <stdio.h>
#include <stdlib.h>
 
#define TRUE 1
#define FALSE 0
 
 
int printWondrous (int start);


int main(int argc, char** argv) {
    //read an input and use it to test printWondrous
    int input;
    int value;
    value = scanf("%d", &input);
    value = printWondrous(input);
    
    printf("%d",value);
    
    return EXIT_SUCCESS;
}

 
/*
 * Takes a starting number and performs the wonderous sequenc
 */
int printWondrous (int start) {
    
    int running = TRUE;
    int count = 0;
    int currentNum = start;
    while(running) {
        //print it
        printf("%d ", currentNum);
        count++;
        
        if(currentNum == 1) {
            running = FALSE;       
        //even
        } else if((currentNum % 2) == 0) {
            currentNum /= 2;   
        //if it's not even it's odd
        } else { 
            currentNum = (currentNum * 3) + 1;
        }
            
    }
    
    printf("\n");
    
    return count;
} 

Download file: wondrous.c (1.2 KB)

Comments

Chat