C (programming language)

C program to print addition, subtraction, multiplication and division of two numbers

Understanding Basic Arithmetic Operations in C

C, a powerful and versatile programming language, allows you to perform various mathematical calculations using different operators. This guide explores a program in C that showcases the addition, subtraction, multiplication, and division of two user-defined numbers.

Setting Up the Stage

Before delving into the code, let’s understand the variables and operators involved:

  • Variables: We will use two integer variables (num1 and num2) to store the user-input numbers.
  • Operators:
    • +: Addition operator
    • -: Subtraction operator
    • *: Multiplication operator
    • /: Division operator (performs integer division by default)

Building the Program: Step-by-Step Breakdown

1. Library Inclusions and Variable Declarations


#include <stdio.h>

int num1, num2;
  • #include <stdio.h>: This line includes the Standard Input/Output library, enabling user interaction through functions like printf and scanf.
  • int num1, num2;: These lines declare two integer variables, num1 and num2, to store the user-input numbers.

2. User Input


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");
scanf("%d", &num2);
  • printf: This function prints a message to the user, prompting them to enter the first number.
  • scanf: This function reads user input and stores it in the specified variable (&num1 in this case). The %d format specifier indicates that we expect an integer input.
  • Similar steps are repeated for the second number input, storing it in num2.

3. Performing Arithmetic Operations


// Addition
int sum = num1 + num2;

// Subtraction
int difference = num1 - num2;

// Multiplication
int product = num1 * num2;

// Division (Note: Performs integer division)
int quotient = num1 / num2;
  • Each line performs a specific arithmetic operation on the user-defined variables.
  • sum stores the addition of num1 and num2.
  • difference stores the subtraction of num2 from num1.
  • product stores the multiplication of num1 and num2.
  • quotient stores the integer division of num1 by num2.

4. Displaying Results


printf("The sum of the numbers is: %d\n", sum);
printf("The difference of the numbers is: %d\n", difference);
printf("The product of the numbers is: %d\n", product);
printf("The quotient of the numbers is: %d\n", quotient);
  • Each printf statement displays a message along with the calculated result (stored in the respective variable).
  • The %d format specifier tells the function to print an integer value.

Putting it all Together

Compile and run the complete program to witness the magic unfold! Users will be prompted to enter two numbers, and the program will display their sum, difference, product, and quotient.

Remember This:

  • This program performs integer division by default. To handle decimal points, consider using float or double data types and appropriate operators.
  • You can modify the program to accept user input for the desired operation instead of performing all four calculations automatically.
  • Explore other arithmetic operators and functions available in C to expand your mathematical capabilities!

This guide provides a basic understanding of writing a C program for performing and displaying basic arithmetic operations. Keep practicing and experimenting to unlock the full potential of C and tackle more complex calculations!


Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button