Menu
Coddy logo textTech

Arithmetic Operators

Part of the Fundamentals section of Coddy's C journey — lesson 16 of 63.

Arithmetic operators in C are used to perform basic mathematical operations. The most common arithmetic operators are:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)

Here's how you can use these operators:

int a = 10;
int b = 5;
int sum = a + b;       // sum is 15
int difference = a - b; // difference is 5
int product = a * b;   // product is 50
int quotient = a / b;  // quotient is 2

Note that when dividing two integers, the result will be an integer (fractional part is truncated). For example:

int result = 7 / 2;  // result is 3, not 3.5

To get a floating-point result, at least one of the operands should be a float:

float result = 7.0 / 2;  // result is 3.5
challenge icon

Challenge

Easy

Write a C program that performs the following operations:

  1. Declare two integer variables num1 and num2 and initialize them with values 15 and 4 respectively.
  2. Calculate and store the sum of num1 and num2 in a variable called sum.
  3. Calculate and store the difference between num1 and num2 in a variable called difference.
  4. Calculate and store the product of num1 and num2 in a variable called product.
  5. Calculate and store the quotient of num1 divided by num2 in a variable called quotient.
  6. Print all results using printf() in the following format:
Sum: [value of sum]
Difference: [value of difference]
Product: [value of product]
Quotient: [value of quotient]

Cheat sheet

Arithmetic operators in C perform basic mathematical operations:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
int a = 10;
int b = 5;
int sum = a + b;       // sum is 15
int difference = a - b; // difference is 5
int product = a * b;   // product is 50
int quotient = a / b;  // quotient is 2

Integer division truncates the fractional part:

int result = 7 / 2;  // result is 3, not 3.5

For floating-point division, use at least one float operand:

float result = 7.0 / 2;  // result is 3.5

Try it yourself

#include <stdio.h>

int main() {
    // Declare and initialize variables here
    
    // Perform calculations
    
    // Print results
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals