Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Arithmetic operator in C

August 25, 2013 Posted by: GPA Views: 665 0 comments

Most C programs use arithmetic calculations. In algebra, if we want to multiply a times b, we can simply write ab. But C, if we want to do that we must write a*b, if we write ab it show error or string "ab". Therefore, C (and other programming languages) require that using the * operator as in a * b

The arithmetic operators are all binary operators. For example, the expression 1 + 2 has the binary operator + and the operands 1 and 2.

The remainder operator is an integer operator that is used only with integer operands.

In table you can find all arithmetic operators:

C operation Arithmetic operator C expression
Addition + a+b
Subtraction - a-b
Multiplication * a*b
Division / a/b
Remainder % a%b

Example:

 

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){ 
int number1;//first number inputed by user
int number2;//second number inputed by user
int sum;//variable for addition of two numbers
int sub;//variable for subtraction of two numbers
int mult;//variable for multiplication of two numbers
float div;//variable for division of two numbers
int remainder;//variable for remainder of two number
printf("Enter first number: ");
scanf("%d",&number1);//Read first number
printf("Enter second number: ");
scanf("%d",&number2);//Read second number
//Addition two numbers
sum=number1+number2;
//subtraction two numbers
sub=number1-number2;
//multiplication two number
mult=number1*number2;
//division two numbers
div=(float)number1/(float)number2; //(float) it is use for converting int to float
//remainder two numbers
remainder=number1%number2;
//show result
printf("Sum = %d",sum);
printf("\nSub = %d",sub);
printf("\nMult = %d",mult);
printf("\nDiv = %f",div);
printf("\nRemainder = %d\n",remainder);
system("PAUSE");  //delay
return 0;
}


Output

Arithmetic operator in C example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe