Hello there, in the previous tutorial of datatypes, we have discussed some data types in C. In this post, we are going to use some of those concepts here. Also, we are going to understand arithmetic operators and operation in C.

Problem Statement

The problem statement is, we have to create a program in which we have to perform some basic arithmetic operations on the number taken from the user. So, let’s get started,

The Program

Let’s look at the program first and understand.

#include <stdio.h>

int main()
{
	int num1, num2;		//input to take from user
	printf("Enter two numbers \n");  \n is for new line character, to shift to next line after printing
	scanf("%d %d",&num2,&num1); //taking two values from our user

	int sum = num2+num1;
	int diff = num2 - num1;
	int pro= num2*num1;
	float div= num2/num1;

	int remainder=num2 %num1;
	printf("Sum is %d\n",sum );
	printf("Difference is %d\n",diff );
	printf("Product is %d\n",pro );
	printf("Division is %f\n",div );
	printf("Remainder is %d\n",remainder );
	return 0;
}

 

In the main function, I have declared two variables which I have to take from the user in order to perform various arithmetic operations. I have printed a statement which prints the instruction to the user to enter two numbers. Then I used “scanf” function which scans the entered number to perform the operations.

Now, I have to store the value of the operation performed because I have to display the result to the user later. That’s why I have declared variables for each operation.

Please note that the %d in the “printf” prints the value of the variable written after the printing statement. The %d is used to display integer value. Similarly, %f is used for floating value or decimal value.

Arithmetic Operators in C

Operators are used for performing an operation. There are many types of operators C supports like arithmetic, logical, bitwise and many more.

The operators in the program used are the arithmetic operators used in C. However, I haven’t used all of them in the program. Here are all of them briefly explained.

+: adds up two values

: substracts up two values

*: Multiply two values

/: Divides two values

%: finds the remainder of the division.

++ : increments the value by one. For example- a++

: decrements the value by one. For example- a–

Execution of the Program

On compiling and executing, you must have seen the result something like the pic shown below.

Share This