C Arithmetic Operators Example

C programming arithmetic operators; In this tutorial, i am going to show you arithmetic operators in the c programming language.

C Arithmetic Operators Example

The Arithmetic operators are some of the C Programming Operator, which are used to perform arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division and Modulus.

The below table shows all the Arithmetic Operators in C Programming:

OperatorMeaning of Operator
+addition or unary plus
subtraction or unary minus
*multiplication
/division
%remainder after division (modulo division)

Example 1 – C Programming Arithmetic Operators

// Working of arithmetic operators
#include <stdio.h>
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d \n",c);
    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    c = a/b;
    printf("a/b = %d \n",c);
    c = a%b;
    printf("Remainder when a divided by b = %d \n",c);
    
    return 0;
}

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

In the above c program, variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and display output as an integer value.

Don’t get confused, let’s see one next example for a more useful; as shown below:

Example 2 – C Programming Arithmetic Operators using Float values

/* C Program to Perform Division and Modulus on Float data type */
#include<stdio.h> 
int main()
{
 int a = 7, b = 3;
 int integerdiv, modulus;
 float floatdiv;
 
 integerdiv = a / b; // dividing 7 by 3
 modulus = a % b; // calculation the remainder
 floatdiv = (float)a / b; // Converting int to float
 
 printf("Division of two numbers a, b is : %d\n", integerdiv);
 printf("Modulus of two numbers a, b is : %d\n", modulus);
 printf("---------Correct Results is------- \n");
 printf("Division of two numbers a, b is : %f\n", floatdiv);
}

Output

Division of two numbers a, b is : 2
Modulus of two numbers a, b is : 1
---------Correct Results is------- 
Division of two numbers a, b is : 2.333333

More C Programming Tutorials

Be First to Comment

Leave a Reply

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