Arithmetic Operators in C program - CodeMub

Introduction: 

Arithmetic operations are fundamental to programming as they allow us to perform calculations and manipulate numerical values. In the C programming language, arithmetic operations can be implemented using various operators and techniques. In this blog, we will explore arithmetic operations in C and demonstrate their usage with code examples. 


1. Addition (+): 

Addition is a basic arithmetic operation that combines two numbers to produce their sum. In C, addition can be performed using the '+' operator. 

Here's an example:
#include <stdio.h>
int main() {
   int a = 5;
   int b = 10;
   int sum = a + b;
   printf("The sum is: %d\n", sum);
   return 0;
}
Output: The sum is: 15 

 2. Subtraction (-): 

Subtraction is another common arithmetic operation that finds the difference between two numbers. In C, subtraction can be performed using the '-' operator. Here's an example:
#include <stdio.h>
int main() { int a = 15; int b = 7; int difference = a - b; printf("The difference is: %d\n", difference); return 0; }
Output: The difference is: 8 

 3. Multiplication (*):

Multiplication involves the process of repeated addition and is used to find the product of two numbers. In C, multiplication can be performed using the '*' operator. Here's an example:
#include <stdio.h>
int main() {
int a = 6; int b = 4; int product = a * b; printf("The product is: %d\n", product); return 0; }
Output: The product is: 24 

 4. Division (/): 

Division is used to split a number into equal parts or find the quotient of two numbers. In C, division can be performed using the '/' operator. Here's an example:
#include <stdio.h> 
int main() {
   int a = 15;
   int b = 3;
   int quotient = a / b;
   printf("The quotient is: %d\n", quotient);
   return 0;
}
Output: The quotient is: 5 

 5. Modulo (%): 

The modulo operator calculates the remainder of a division operation. In C, the modulo operation can be performed using the '%' operator. Here's an example:
#include <stdio.h>
#include <stdio.h> 
int main() {
   int a = 15;
   int b = 7;
   int remainder = a / b;
   printf("The remainder is: %d\n", remainder);
return 0; }
Output: The remainder is: 1 


Arithmetic Operations using Switch Case: 

In some cases, you may want to implement arithmetic operations using a switch case statement to provide a menu-like experience to the user. Here's an example of performing arithmetic operations using switch case:

#include <stdio.h>
int main() {
   int operation;
   double a, b;
   printf("Enter the operation:\n");
   printf("1. Addition\n");
   printf("2. Subtraction\n");
   printf("3. Multiplication\n");
   printf("4. Division\n");
   scanf("%d", &operation);
   printf("Enter two numbers: ");
   scanf("%lf %lf", &a, &b);
   switch (operation) {
      case 1:
         printf("The sum is: %lf\n", a + b);
         break;
      case 2:
        

 printf("The difference is: %lf\n", a - b);
         break;
      case 3:
         printf("The product is: %lf\n", a * b);
         break;
      case 4:
         if (b == 0) {
            printf("Error: Division by zero.\n");
         } else {
            printf("The quotient is: %lf\n", a / b);
         }
         break;
      default:
         printf("Invalid operation.\n");
         break;
   }
   return 0;
}
Output: 

Enter the operation: 
1. Addition 
2. Subtraction 
3. Multiplication 
4. Division 
2 Enter two numbers: 15 7 
The difference is: 8.000000 

Conclusion: 

Arithmetic operations are vital in programming as they allow us to perform calculations and manipulate numerical values. In C, these operations can be implemented using operators such as addition, subtraction, multiplication, division, and modulo. By using switch case statements, we can provide a user-friendly interface to select and perform arithmetic operations. Understanding these fundamental concepts will help you build more complex programs and solve real-world problems efficiently. Happy coding!
Next Post Previous Post
No Comment
Add Comment
comment url