Arithmetic operators in c




Arithmetic Operators in C



Arithmetic Operators in C



Arithmetic operators in C are used to perform basic arithmetic calculations on operands. These operators allow you to perform addition, subtraction, multiplication, division, and modulus operations. In this blog post, we will explore each arithmetic operator in detail, along with examples and C programs to illustrate their usage.



Table of Contents



  1. Introduction to Arithmetic Operators

  2. Addition (+)

  3. Subtraction (-)

  4. Multiplication (*)

  5. Division (/)

  6. Modulus (%)

  7. Examples



1. Introduction to Arithmetic Operators



Arithmetic operators are used to perform basic mathematical operations in C. These operators work on numeric operands and produce a single numeric result. The following are the arithmetic operators available in C:




























Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)


2. Addition (+)



The addition operator (+) is used to add two operands.



Syntax:


result = operand1 + operand2;


Example:


<!-- C code example -->
#include <stdio.h>

int main() {
int num1 = 10, num2 = 20, sum;
sum = num1 + num2;
printf("Sum: %d\\n", sum);
return 0;
}


3. Subtraction (-)



The subtraction operator (-) is used to subtract the second operand from the first operand.



Syntax:


result = operand1 - operand2;


Example:


<!-- C code example -->
#include <stdio.h>

int main() {
int num1 = 30, num2 = 15, difference;
difference = num1 - num2;
printf("Difference: %d\\n", difference);
return 0;
}


4. Multiplication (*)



The multiplication operator (*) is used to multiply two operands.



Syntax:


result = operand1 * operand2;


Example:


<!-- C code example -->
#include <stdio.h>

int main() {
int num1 = 5, num2 = 8, product;
product = num1 * num2;
printf("Product: %d\\n", product);
return 0;
}


5. Division (/)



The division operator (/) is used to divide the first operand by the second operand.



Syntax:


result = operand1 / operand2;


Example:


<!-- C code example -->
#include <stdio.h>

int main() {
int num1 = 20, num2 = 4, quotient;
quotient = num1 / num2;
printf("Quotient: %d\\n", quotient);
return 0;
}


6. Modulus (%)



The modulus operator (%) is used to find the remainder when the first operand is divided by the second operand.



Syntax:


result = operand1 % operand2;


Example:


<!-- C code example -->
#include <stdio.h>

int main() {
int num1 = 20, num2 = 3, remainder;
remainder = num1 % num2;
printf("Remainder: %d\\n", remainder);
return 0;
}


7. Examples



Now, let's see some more examples to understand the behavior of arithmetic operators:



Example 1:


<!-- C code example -->
#include <stdio.h>

int main() {
int a = 10, b = 3, c;

c = a + b;
printf("Sum: %d\\n", c);

c = a - b;
printf("Difference: %d\\n", c);

c = a * b;
printf("Product: %d\\n", c);

c = a / b;
printf("Quotient: %d\\n", c);

c = a % b;
printf("Remainder: %d\\n", c);

return 0;
}

Output:



Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1


Example 2:


<!-- C code example -->
#include <stdio.h>

int main() {
int num = 25;

num += 5; // Equivalent to num = num + 5;
printf("num after addition: %d\\n", num);

num -= 10; // Equivalent to num = num - 10;
printf("num after subtraction: %d\\n", num);

num *= 2; // Equivalent to num = num * 2;
printf("num after multiplication: %d\\n", num);

num /= 4; // Equivalent to num = num / 4;
printf("num after division: %d\\n", num);

num %= 3; // Equivalent to num = num % 3;
printf("num after modulus: %d\\n", num);

return 0;
}

Output:



num after addition: 30
num after subtraction: 20
num after multiplication: 40
num after division: 10
num after modulus: 1


In conclusion, arithmetic operators play a crucial role in C programming by allowing you to perform basic mathematical calculations. Understanding how to use these operators is essential for any C programmer. By using the examples and explanations provided in this blog post, you should now have a solid understanding of arithmetic operators in C and be ready to apply them in your own programs.



Happy coding!





Post a Comment

0 Comments