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

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) {
    double x, absoluteValue;

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

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

    printf("The absolute value of %f is %f\n", x, absoluteValue);
    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;
}

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 conditiond

#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;
}