COMP1511 18s1 (webcms)
Code Examples from Lectures on if
COMP1511 18s1 (flask)

A simple program demonstrating the use of scanf and if statements
    #include <stdio.h>

int main(void) {
    int a, b;
    printf("Enter a: ");
    scanf("%d", &a);
    printf("Enter b: ");
    scanf("%d", &b);
    if (a > b) {
        printf("a is greater than b\n");
    } else if (a < b) {
        printf("a is less than b\n");
    } else {
        printf("a is equal to b\n");
    }
    return 0;
}


A simple program demonstrating the use of scanf and if statements
    #include <stdio.h>

int main(void) {
    int a, b;
    printf("Enter a: ");
    scanf("%d", &a);
    printf("Enter b: ");
    scanf("%d", &b);
    if (a > b) {
        printf("%d is greater than %d\n", a, b);
    } else if (a < b) {
        printf("%d is less than %d\n", a, b);
    } else {
        printf("%d is equal to %d\n", a, b);
    }
    return 0;
}



Convert a measurement in feet to metres

A simple program demonstrating the use of scanf, printf and an if statement

    #include <stdio.h>

int main(void) {
    int x, absoluteValue;

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

    absoluteValue = x;
    if (x < 0) {
        absoluteValue = -1 * x;
    }

    printf("The absolute value of %d is %d\n", x, absoluteValue);
    return 0;
}



Print the result of an integer division

    #include <stdio.h>

int main(void) {
    int x, y;

    printf("Enter x: ");
    scanf("%d", &x);
    printf("Enter y: ");
    scanf("%d", &y);

    if (y != 0) {
        printf("%d/%d=%d\n", x, y, x/y);
    } else {
        printf("Can't divide by zero sorry\n");
    }
    return 0;
}


A simple program demonstrating the use of scanf and if statements with complex conditions
    #include <stdio.h>

#include <stdio.h>

int main(void) {
    int x;
    printf("Enter x: ");
    scanf("%d", &x);

    printf("%d has ", x);
    if (x < 10 && x > -10) {
        printf("1 digit");
    }
    if ((x >= 10 && x < 100) || (x <= -10 && x > -100)) {
        printf("2 digits");
    }
    if (x >= 100 || x <= -100) {
        printf("more than 2 digits");
    }
    printf("\n");

    return 0;
}


A simple program demonstrating the use of scanf and nested if statements
    #include <stdio.h>

int main(void) {
    int a;
    printf("Enter a: ");
    scanf("%d", &a);

    printf("%d is a ", a);
    if (a < 0) {
        if (a < -100) {
            printf("big");
        } else {
            printf("small");
        }
        printf(" negative");
    } else {
        if (a > 100) {
            printf("big");
        } else {
            printf("small");
        }
        printf(" positive");
    }
    printf(" number.\n");

    return 0;
}



A simple program to check Pythagorean identity

Demonstrates danger of use == with doubles e.g. enter 1 for theta
    #include <stdio.h>
#include <math.h>

int main(void) {
    double theta, identity;

    printf("Enter theta: ");
    scanf("%lf", &theta);

    identity = 1 - (sin(theta) * sin(theta) + cos(theta) * cos(theta));

    if (identity == 0.0) {
        printf("Pythagorean identity true for %lf\n", theta);
    } else {
        printf("Pythagorean wrong by %g for %lf\n", identity,  theta);
    }

    return 0;
}



Calculate relativistic-mass of an object
    #include <stdio.h>
#include <math.h>

#define SPEED_OF_LIGHT 299792458.0

int main(void) {
    double mass, rest_mass;
    double velocity;
    double ratio;

    printf("Enter rest mass: ");
    scanf("%lf", &rest_mass);

    printf("Enter velocity in metres/second: " );
    scanf("%lf", &velocity);

    // compute velocity as a fraction of speed of light
    ratio = velocity / SPEED_OF_LIGHT;

    if (ratio >= 1.0) {
        printf("Error: velocity exceeds speed of light.\n");
    } else {
        // compute observed mass using Einstein's equation
        mass = rest_mass / sqrt(1.0 - ratio*ratio);
        printf("Observed mass = %1.6f\n", mass);
    }

    return 0;
}


A simple program demonstrating the use of scanf and chained if statements

Notionally this program is to assist an inept male straight engineer named George in his interaction with the opposite sex

    #include <stdio.h>

int main(void) {
    int herAge;
    printf("Hi George\n");
    printf("Enter her age: ");
    scanf("%d", &herAge);
    printf("George say this: \"");

    if (herAge < 17) {
        printf("Do you have an older sister?");
    } else if (herAge < 42) {
        printf("Would you like to go to a movie Saturday?");
    } else if (herAge < 65) {
        printf("Do you have a daughter?");
    } else {
        printf("Do you have a grand-daughter?");
    }

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