Back to C Programming
2026-04-227 min read

Basic Arithmetic

Learn Basic Arithmetic step by step with clear examples and exercises.

Why This Matters

Understanding basic arithmetic operations in C is essential for solving complex problems, acing interviews, and debugging common issues that might arise during coding. Arithmetic operations are the fundamental building blocks of programming, enabling us to perform calculations and manipulate data effectively. Mastering these concepts will empower you to write efficient and accurate code.

Prerequisites

Before diving into the core concept of basic arithmetic in C, it's essential to have a solid understanding of:

  1. Variables and data types
  2. Basic input/output operations using printf and scanf
  3. Control structures like if, else, for, and while loops
  4. Understanding the concept of operators in C, including arithmetic, relational, logical, and assignment operators.
  5. Familiarity with C programming syntax and semantics.

Core Concept

Basic arithmetic in C involves the usual binary operators of algebra: addition, subtraction, multiplication, division, and modulus. Let's explore each operator in detail.

Addition (+)

The addition operator adds two operands together. For example:

int a = 5;
int b = 3;
int sum = a + b; // sum will be equal to 8

In C, you can also use the += operator for shorthand assignment, which adds the right operand to the left one and assigns the result back to the left operand. For example:

int a = 5;
a += 3; // a is now equal to 8

Subtraction (-)

The subtraction operator subtracts the right operand from the left one. For example:

int a = 10;
int b = 3;
int difference = a - b; // difference will be equal to 7

In C, you can also use the -= operator for shorthand assignment, which subtracts the right operand from the left one and assigns the result back to the left operand. For example:

int a = 10;
a -= 3; // a is now equal to 7

Multiplication (*)

The multiplication operator multiplies two operands together. For example:

int a = 5;
int b = 3;
int product = a * b; // product will be equal to 15

In C, you can also use the *= operator for shorthand assignment, which multiplies the left operand by the right one and assigns the result back to the left operand. For example:

int a = 5;
a *= 3; // a is now equal to 15

Division (/)

The division operator divides the left operand by the right one. However, Note that that dividing integers may not give the exact result you expect. The value is an integer, which is not equal to the mathematical quotient when that is a fraction. For example:

int a = 10;
int b = 3;
int quotient = a / b; // quotient will be equal to 3 (not 3.333...)

To get the corresponding integer remainder when necessary, use the modulus operator (%).

Modulus (%)

The modulus operator returns the remainder of the division operation. For example:

int a = 10;
int b = 3;
int remainder = a % b; // remainder will be equal to 1

In C, you can also use the %= operator for shorthand assignment, which calculates the modulus of the left operand and the right one and assigns the result back to the left operand. For example:

int a = 10;
a %= 3; // a will be equal to 1

Unary Operators (+, -)

The unary operator + yields its operand unaltered, while the unary operator - changes the sign of a number. For example:

int num = -5;
num += 7; // num will be equal to 2 (negative sign removed)
num = -num; // num will be equal to -2 (sign changed back)

Worked Example

Let's consider a simple example of calculating the area and perimeter of a rectangle using basic arithmetic operations in C.

#include <stdio.h>

int main() {
int length, breadth;

printf("Enter the length: ");
scanf("%d", &length);

printf("Enter the breadth: ");
scanf("%d", &breadth);

// Calculate area
int area = length * breadth;
printf("The area of the rectangle is: %d\n", area);

// Calculate perimeter
int perimeter = 2 * (length + breadth);
printf("The perimeter of the rectangle is: %d\n", perimeter);

return 0;
}

Common Mistakes

  1. Forgetting semicolons: Semicolons are crucial in C, and forgetting them can lead to syntax errors. For example:
int a = 5 // Syntax error: missing semicolon
  1. Incorrect use of operators: Be careful with the order of operations, as C follows the standard algebraic precedence rule. For example:
int a = 3 * 4 + 1; // Correct: multiplication is performed first, then addition
int b = 3 * (4 + 1); // Correct: parentheses force the order of operations
int c = 3 * 4 + 1 * 2; // Incorrect: multiplication should be performed before division or modulus
  1. Ignoring integer division: Remember that dividing integers may not give the exact result you expect, and the value is an integer. For example:
int a = 10 / 3; // a will be equal to 3 (not 3.333...)
  1. Misunderstanding unary operators: The unary operator + yields its operand unaltered, while the unary operator - changes the sign of a number. For example:
int num = -5;
num += 7; // num will be equal to 2 (negative sign removed)
num = -num; // num will be equal to -2 (sign changed back)

Common Mistakes (Continued)

  1. Incorrect use of shorthand assignment: Be careful when using shorthand assignment, as it can lead to unexpected results if the left operand is not an arithmetic variable. For example:
int a = 5;
char b = 'A';
a += b; // a will be equal to 68 (the ASCII value of 'A'), not 10 (expected from integer addition)
b += 3; // b will be equal to 'D' (the ASCII value of 'D')

Practice Questions

  1. Write a program that calculates the sum of two numbers using basic arithmetic operations in C.
  2. Write a program that calculates the average of three numbers using basic arithmetic operations in C.
  3. Write a program that checks whether a number is even or odd using basic arithmetic operations in C.
  4. Write a program that calculates the maximum and minimum values from an array of integers using basic arithmetic operations in C.
  5. Write a program that calculates the factorial of a given number using recursion and basic arithmetic operations in C.
  6. Write a program that finds the largest prime number among a given range using basic arithmetic operations in C.
  7. Write a program that calculates the square root of a given number using Newton's method and basic arithmetic operations in C.
  8. Write a program that solves quadratic equations (ax^2 + bx + c = 0) using the quadratic formula and basic arithmetic operations in C.
  9. Write a program that calculates the sum of the first n Fibonacci numbers using recursion and basic arithmetic operations in C.
  10. Write a program that finds the greatest common divisor (GCD) of two numbers using Euclid's algorithm and basic arithmetic operations in C.

FAQ

  1. Why does dividing integers not give the exact result?

Dividing integers in C results in integer division, meaning the decimal part of the quotient is discarded, and only the integer part remains. This can lead to unexpected results when dealing with fractions.

  1. What is the purpose of the unary operator + in C?

The unary operator + yields its operand unaltered. It's mainly used for clarity or when working with negative numbers, as it removes the negative sign without changing the actual value.

  1. How can I get the remainder of a division operation in C?

You can use the modulus operator % to calculate the remainder of a division operation in C. For example:

int a = 10;
int b = 3;
int remainder = a % b; // remainder will be equal to 1
  1. What is the difference between pre-increment (++) and post-increment (++) operators in C?

Pre-increment (++a) increments the value of a before using it in an expression, while post-increment (a++) increments the value of a after using it in an expression. For example:

int a = 5;
printf("%d\n", ++a); // Output: 6
printf("%d\n", a++); // Output: 6, then a becomes 7
  1. What is the difference between pre-decrement (--) and post-decrement (--) operators in C?

Pre-decrement (--a) decrements the value of a before using it in an expression, while post-decrement (a--) decrements the value of a after using it in an expression. For example:

int a = 5;
printf("%d\n", --a); // Output: 4
printf("%d\n", a--); // Output: 4, then a becomes 3
  1. What is the purpose of the %f format specifier in printf function?

The %f format specifier in the printf function is used to print floating-point numbers with decimal points. For example:

float pi = 3.14159;
printf("%f\n", pi); // Output: 3.141590 (approximately)
  1. What is the difference between float and double data types in C?

The main difference between float and double data types in C lies in their precision and memory usage. A float variable uses approximately 4 bytes of memory and can store single-precision floating-point numbers with a precision of about 7 digits, while a double variable uses approximately 8 bytes of memory and can store double-precision floating-point numbers with a precision of about 15 digits.

  1. What is the purpose of the scanf("%lf") function in C?

The scanf("%lf") function in C is used to read floating-point numbers from the standard input (keyboard) and store them in a variable of type float or double. For example:

float num;
scanf("%lf", &num); // Reads a floating-point number from the keyboard and stores it in num