Mini C program #2
Let's do some clever calculations!
Tutorial presented by: Jérémie F. Bellanger (Jay)
Date of publication: February 22, 2014
Last update: March 9, 2025
Well, after this first fairly easy mini-program, it's going to be your turn to play thanks to this new Activity. Your mission, if you accept it (I hope so!
), will be to create a "mini" calculator (if we can call it that...).
So you're going to first ask the user for 2 numbers using the scanf() and printf() functions, in this style:
// Ask the user for a number:
printf("Enter a number: ");
scanf("%d" , & number1);
Of course, to store these numbers, you will first need to create two variables, such as (let's be original: number1 and number2
).
Not to mention, you'll also need a variable for the result.
At first, you can limit yourself to whole numbers (int), but you can then continue with numbers with decimal points (float). ![]()
And there you have it, that's all for this second mini C program! Not very difficult, so I'll let you search (and don't cheat by looking at the walkthrough right away, I've got my eye on you!
).
Finally, if you want to practice more, you are free to improve this program, for example by asking the user what type of operation he wants, instead of giving him all of them.
You can also add some numbers.
Come on, let's go! ![]()
The code
And here is the solution with the comments:
#include <stdio.h>
#include <stdlib.h>
//Program that asks the user to enter 2 numbers
//and performs the 4 basic operations with.
int main(int argc, char *argv[ ])
{
//We create 3 variables (3 integers or int) that
//will contain the 2 numbers entered by the user
//as well as the result.
int result = 0, number1 = 0, number2 = 0;
// The user is asked for the numbers 1 and 2:
printf("Enter the number 1: ");
scanf("%d", &number1);
printf("Enter the number 2: ");
scanf("%d", &number2);
//1. We display the addition
// We do the calculation:
result = number1 + number2;
//And we display the result on the screen:
printf ("%d + %d = %d\n", number1, number2, result);
//2. We display the subtraction
// We do the calculation:
result = number1 - number2 ;
//And we display the result on the screen:
printf("%d - %d = %d\n", number1, number2, result);
//3. We display the multiplication
// We do the calculation:
result = number1 * number2;
//And we display the result on the screen:
printf("%dx %d = %d\n", number1, number2, result);
//4. We display the division
// We do the calculation:
result = number1 / number2;
//And we display the result on the screen
//Note: as we are using integers, the result
//after the decimal point will not be displayed
printf ("%d / %d = %d\n", number1, number2, result);
return 0;
}
You will notice in the previous code, that I recycle the result variable (it's more eco-friendly!
). Indeed, after each calculation, we directly display the result, and we therefore no longer need the variable. We can therefore rewrite over it without problem
.
If you want to further optimize this program, you can also add a while() loop so that it starts over from scratch each time (and so that we no longer have to restart it all the time
).
And here is the result in picture:

You will be able to download the project files in the Downloads section.

English
Français