Project 3 - Calculator ๐Ÿงฎ#

Now that we know how to use operators, lets try making a program that performs division and tells you the remainder.

The calculator should as the user to enter two space-separated integers and then print out the quotient (the result) and the remainder.

Ex.

> Enter two space-separated integers:
> 9 4
> 9 / 4 = 2 with a remainder of 1
> Enter two space-separated integers:
> 13 / 5
> 13 / 5 = 2 with a remainder of 3

Tasks ๐ŸŽฏ#

Print the question:

Hint โ“

You can print something by writing printf("the stuff I want to print");. If this seems unfamiliar to you, then check out the Printf() ๐Ÿ–จ๏ธ section.

Donโ€™t forget the \n at the end of the printf statement in order to let the scanf to detect user input on the next line!

Solution โœ…
#include <stdio.h>

int main() {
    printf("Enter two space-separated integers:\n");

    return 0;
}

Get the userโ€™s input for the two numbers.

Hint โ“

You can get two inputs at the same time from a user by writing scanf("%d %d", &first_integer_var, &second_integer_var);. If this seems unfamiliar to you, then check out the Scanf() โŒจ๏ธ section.

Solution โœ…
#include <stdio.h>

int main() {
    int dividend = 0;
    int divisor = 1;
    printf("Enter two space-separated integers:\n");
    scanf("%d %d", &dividend, &divisor);

    return 0;
}

Perform the division and remainder operations and print their results.

Hint โ“

You can divide two integers by doing first_integer / second_integer. To get the remainder, you can use the modulo operator and do first_integer % second_integer. See Integer Division โž— and Modulo ๐Ÿ”ข for more information.

Solution โœ…
#include <stdio.h>

int main() {
    int dividend = 0;
    int divisor = 1;
    printf("Enter two space-separated integers:\n");
    scanf("%d %d", &dividend, &divisor);
    printf("%d / %d = %d with a remainder of %d", dividend, divisor, dividend / divisor, dividend % divisor);

    return 0;
}