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 theprintf
statement in order to let thescanf
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", ÷nd, &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 dofirst_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", ÷nd, &divisor); printf("%d / %d = %d with a remainder of %d", dividend, divisor, dividend / divisor, dividend % divisor); return 0; }