COMP1511 17s1 Code Examples from Lectures on while Introduction to Programming

A silly program which prints first 1000 multiples of 13

#include <stdio.h>

int main(void) {
    printf("%d\n", 13);
    printf("%d\n", 26);
    printf("%d\n", 39);
    printf("%d\n", 52);
    printf("%d\n", 65);
    printf("%d\n", 78);
    printf("%d\n", 91);
    printf("%d\n", 104);
    printf("%d\n", 117);
    printf("%d\n", 130);
    printf("%d\n", 143);
    printf("%d\n", 156);
    printf("%d\n", 169);
    printf("%d\n", 182);
    printf("%d\n", 195);
    printf("%d\n", 208);
    printf("%d\n", 221);
    printf("%d\n", 234);
    printf("%d\n", 247);
    printf("%d\n", 260);
    printf("%d\n", 273);
    printf("%d\n", 286);
    printf("%d\n", 299);
    printf("%d\n", 312);
    printf("%d\n", 325);
    printf("%d\n", 338);
    printf("%d\n", 351);
    printf("%d\n", 364);
    printf("%d\n", 377);
...

A silly program which prints first 1000 multiples of 13

#include <stdio.h>

int main(void) {
    if (1 % 13 == 0) {
        printf("%d\n", 1);
    }
    if (2 % 13 == 0) {
        printf("%d\n", 2);
    }
    if (3 % 13 == 0) {
        printf("%d\n", 3);
    }
    if (4 % 13 == 0) {
        printf("%d\n", 4);
    }
    if (5 % 13 == 0) {
        printf("%d\n", 5);
    }
    if (6 % 13 == 0) {
        printf("%d\n", 6);
    }
    if (7 % 13 == 0) {
        printf("%d\n", 7);
    }
    if (8 % 13 == 0) {
        printf("%d\n", 8);
    }
    if (9 % 13 == 0) {
        printf("%d\n", 9);
    }
    if (10 % 13 == 0) {
        printf("%d\n", 10);
...

A silly program which prints first 1000 multiples of 13

Note the same 4 lines are repeated 13000 times.

This means these lines could be replaced by a while loop which executes 13000 times.

#include <stdio.h>

int main(void) {
    int i;
    i = 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
    i = i + 1;
    if (i % 13 == 0) {
        printf("%d\n", i);
    }
...

A simple program which uses a while loop to print first 1000 multiples of 13

#include <stdio.h>

int main(void) {
    int i;
    i = 1;
    while (i <= 13000) {
        if (i % 13 == 0) {
            printf("%d\n", i);
        }
        i = i + 1;
    }

    return 0;
}

Another simple program which uses a while loop to print first 1000 multiples of 13

#include <stdio.h>

int main(void) {
    int i;
    i = 13;
    while (i <= 13000) {
        printf("%d\n", i);
        i = i + 13;
    }
    return 0;
}

Another simple program which uses a while loop to print first 1000 multiples of 13

#include <stdio.h>

int main(void) {
    int i;
    i = 1;
    while (i <= 1000) {
        printf("%d\n", i*13);
        i = i + 1;
    }
    return 0;
}

A simple program demonstrating the use of a while loop

#include <stdio.h>

int main(void) {
    int n, upperBound, sum;
    sum = 0;
    upperBound = 10;
    n = 1;
    while (n <= upperBound) {
        sum = sum + n;
        n = n + 1;
    }
    printf("Sum of integers 1..%d is %d\n", upperBound, sum);
    return 0;
}

Read 43 numbers and then print the sum of the numbers

A simple program demonstrating while & scanf

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#define N_NUMBERS 42

#include <stdio.h>

int main(void) {
    int x, sum, n;

    sum = 0;
    printf("Enter %d numbers:\n", N_NUMBERS);

    n = 0;
    while (n < N_NUMBERS) {
        scanf("%d", &x);
        sum = sum + x;
        n = n + 1;
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Read n numbers and then print the sum of the numbers

A simple program demonstrating while & scanf

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int x, sum, n, nNumbers;

    sum = 0;
    printf("How many numbers do you wish to sum: ");
    scanf("%d", &nNumbers);
    printf("Enter %d numbers:\n", nNumbers);

    n = 0;
    while (n < nNumbers) {
        scanf("%d", &x);
        sum = sum + x;
        n = n + 1;
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Read 5 numbers and print the largest

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

#define N_NUMBERS 5

int main(void) {
    int howManyNumbers, number, maximum;
    scanf("%d", &maximum);
    howManyNumbers = 1;
    while (howManyNumbers < N_NUMBERS) {
        scanf("%d", &number);
        if (number > maximum) {
            maximum = number;
        }
        howManyNumbers = howManyNumbers + 1;
    }
    printf("Largest is %d\n", maximum);
    return 0;
}

Read numbers until a negative number is read then print the sum of the numbers (not including the negative number)

Version 1

A simple program demonstrating stopping a while loop when a particular value is read by scanf

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int x, sum;

    sum = 0;
    x = 0;
    printf("Enter numbers, terminate with a negative number:\n");

    while (x >= 0) {
        scanf("%d", &x);
        if (x > 0) {
            sum = sum + x;
        }
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Read numbers until a negative number is read then print the sum of the numbers (not including the negative number)

Version 2

A simple program demonstrating stopping a while loop when a particular value is read by scanf

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int x, sum;

    sum = 0;
    printf("Enter numbers, terminate with a negative number:\n");

    scanf("%d", &x);
    while (x >= 0) {
        sum = sum + x;
        scanf("%d", &x);
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Sum the series 1 + 1/2 + 1/3 + 1/4 + ...

#include <stdio.h>


#define N_SERIES_TERMS 1000

int main(void) {
    int n;
    double sum;

    n = 1;
    sum = 0;
    while (n <= N_SERIES_TERMS) {
        sum = sum + 1.0 / n;
        n = n + 1;
    }

    printf("1 + 1/2 + 1/3 + + ... + 1/%d = %f\n", N_SERIES_TERMS, sum);
    return 0;
}

Calculate the mathematical constant e by summing the series 1 + 1/(1*2) + 1/(1*2*3) + 1/(1*2*3*4) + ... http://en.wikipedia.org/wiki/E_%28mathematical_constant%29

#include <stdio.h>


#define N_SERIES_TERMS 20

int main(void) {
    int n;
    double sum, factorial;

    n = 1;
    sum = 0;
    factorial = 1;
    while (n <= N_SERIES_TERMS) {
        sum = sum + 1.0 / factorial;
        factorial = factorial * n;
        n = n + 1;
    }

    printf("1/1 + 1/1 + 1/(1*2) + 1/(1*2*3) + 1/(1*2*3*4)  + ... + 1/%.0f  = %.12f\n", factorial, sum);
    return 0;
}

Calculate the mathematical constant pi to 6 decimal places by summing the series 4 - 4/3 + 4/5 - 4/7 + 4/9 + ...

#include <stdio.h>


#define N_SERIES_TERMS 1000000

int main(void) {
    int n;
    double sum;

    n = 0;
    sum = 0;
    while (n < N_SERIES_TERMS) {
        if (n % 2 == 0) {
            sum = sum + 4.0 / (2 * n + 1);
        } else {
            sum = sum - 4.0 / (2 * n + 1);
        }
        n = n + 1;
    }

    printf("4 - 4/3 + 4/5 - 4/7 + 4/9 + ... = %f\n", sum);
    return 0;
}

Calculate the the series 1 + 1/2 + 1/4 + 1/8 + ... until it converges to within 10 decimal places

#include <stdio.h>

#define ACCURACY  0.000000000001

int main(void) {
    double sum, term;

    sum = 0;
    term = 1;

    while (term > ACCURACY) {
        sum = sum + term;
        term = term / 2.0;
    }

    printf("1 + 1/2 + 1/4 + 1/8 + ...  = %.10f\n", sum);
    return 0;
}

Read numbers printing whether they are even or odd illustrate suse of a sentinal variable (stop_loop)

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int stop_loop, number;

    printf("Enter numbers, 0 to stop\n");

    stop_loop = 0;
    while (stop_loop != 1) {
        scanf("%d", &number);
        if (number == 0) {
            stop_loop = 1;
        } else if (number % 2 == 1) {
            printf("%d is odd.\n", number);
        } else {
            printf("%d is even.\n", number);
        }
    }

    return 0;
}

A simple program which prints a square

#include <stdio.h>

#define SIDE_LENGTH 13

int main(void) {
    int row, column;
    row = 0;
    while (row < SIDE_LENGTH) {
        column = 0;
        while (column <= SIDE_LENGTH) {
            printf("*");
            column = column + 1;
        }
        printf("\n");
        row = row + 1;
    }
    return 0;
}

A simple program which prints a triangle

#include <stdio.h>

#define SIDE_LENGTH 13

int main(void) {
    int row, column;
    row = 0;
    while (row < SIDE_LENGTH) {
        column = 0;
        while (column <= row) {
            printf("*");
            column = column + 1;
        }
        printf("\n");
        row = row + 1;
    }
    return 0;
}

A simple program which reads integers and and if a composite number is read exists printing the factor

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int n, possibleFactor, keepLooping;
    keepLooping = 1;
    while (keepLooping == 1) {
        printf("Enter a number: ");
        scanf("%d", &n);
        possibleFactor = 2;
        while (possibleFactor < n && keepLooping == 1) {
            if (n % possibleFactor == 0) {
                printf("%d is composite %d is a factor\n", n, possibleFactor);
                keepLooping = 0;
            }
            possibleFactor = possibleFactor + 1;
        }
    }
    return 0;
}

A simple program demonstrating nested while loops

It prints all prime numbers < 10000

#include <stdio.h>

int main(void) {
    int n, possibleFactor, nFactors;
    // loop through numbers 1..1000
    n = 1;
    while (n < 1000) {
        // loop through numbers 1..n counting factors
        possibleFactor = 1;
        nFactors = 0;
        while (possibleFactor <= n) {
            if (n % possibleFactor == 0) {
                nFactors = nFactors + 1;
            }
            possibleFactor = possibleFactor + 1;
        }
        if (nFactors <= 2) {
            printf("%d is prime\n", n);
        }
        n = n + 1;
    }
    return 0;
}

A simple program which reads integers and prints snap and exits if the same number is read twice in a row

Note for simplicity we are assuming scanf succeeds in reading an integer.

A robust program would check that scanf returns 1 to indicate an integer read.

#include <stdio.h>

int main(void) {
    int n, previousN;

    printf("Enter a number: ");
    scanf("%d", &previousN);

    printf("Enter a number: ");
    scanf("%d", &n);

    while (n != previousN) {
        previousN = n;
        printf("Enter a number: ");
        scanf("%d", &n);
    }

    printf("Snap!\n");
    return 0;
}

Read numbers until end of input (or a non-number) is reached then print the sum of the numbers

Version 1

A simple program demonstrating stopping a while loop when scanf fails to read a number e.g. because end-of-input is reached

#include <stdio.h>

int main(void) {
    int sum, x, numbersRead;

    sum = 0;
    numbersRead = 1;
    printf("Enter numbers, indicate end-of-input with control-D:\n");

    while (numbersRead == 1) {

        // if scanf can read an integer it will place it in x and it will return 1
        // if scanf can't read an integer it will not change x and it will return 0 or -1
        numbersRead = scanf("%d", &x);

        if (numbersRead == 1) {
            sum = sum + x;
        }
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Read numbers until end of input (or a non-number) is reached then print the sum of the numbers

Version 1

A simple program demonstrating stopping a while loop when scanf fails to read a number e.g. because end-of-input is reached

#include <stdio.h>

int main(void) {
    int sum, x, numbersRead;

    sum = 0;
    numbersRead = 1;
    printf("Enter numbers, indicate end-of-input with control-D:\n");

    // if scanf can read an integer it will place it in x and it will return 1
    // if scanf can't read an integer it will not change x and it will return 0 or -1
    numbersRead = scanf("%d", &x);
    while (numbersRead == 1) {
        sum = sum + x;
        numbersRead = scanf("%d", &x);
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Read numbers until end of input (or a non-number) is reached then print the sum of the numbers

Version 2

A simple program demonstrating stopping a while loop when scanf fails to read a number e.g. because end-of-input is reached

#include <stdio.h>

int main(void) {
    int sum, x;

    sum = 0;

    printf("Enter number: ");
    while (scanf("%d", &x) == 1) {
        sum = sum + x;
        printf("Enter number: ");
    }

    printf("Sum of the numbers is %d\n", sum);
    return 0;
}

Print first 20 fibonacci numbers

#include <stdio.h>

int main(void) {
    int howManyNumbers, n, previousN, w;
    n = 1;
    previousN = 0;
    howManyNumbers = 0;
    while (howManyNumbers < 20) {
        w = previousN + n;
        previousN = n;
        n = w;
        howManyNumbers = howManyNumbers + 1;
        printf("%d\n", n);
    }

    return 0;
}

A simple program which searches for pythagorean triples integers which form form the sides of a right triangle http://en.wikipedia.org/wiki/Pythagorean_triple

#include <stdio.h>

#define N 100

int main(void) {
    int x, y, z;
    x = 1;
    while (x < N) {
        y = 1;
        while (y <= N) {
            z = 1;
            while (z <= N) {
                if (x * x + y * y == z * z) {
                    printf("%d^2 + %d^2 = %d^2\n", x, y, z);
                }
                z = z + 1;
            }
            y = y + 1;
        }
        x = x + 1;
    }
    return 0;
}