Home C Programming Language / Programs / Arithmetic Operations example in c programming langauage
🚀 Programming Example

Arithmetic Operations example in c programming langauage

👁 1,364 Views
💻 Practical Program
📘 Step Learning
C supports the four basic arithmetic operations such as addition, subtraction, multiplication, and division. The following is the code illustration for basic arithmetic:

📌 Information & Algorithm

The below code is a C program that demonstrates how to perform arithmetic operations on variables of integer and floating-point data types and print the results to the console using the printf() function.

Here's how it works:

  • The #include line includes the standard input/output library in the program.
  • The main() function is the entry point of the program.
  • Three integer variables a, b, and c are declared and initialized with values of 10, 6, and the result of a+b respectively.
  • The first printf() function prints the values of a, b, and c with the + operator to perform addition and the = operator to assign the result to c.
  • The next three statements use the -, * operators to perform subtraction, multiplication operations and assigns the results to c.
  • The next printf() function performs integer division of a/b and prints the result with two decimal places using the %f format specifier. Note that since a and b are both integers, integer division is performed and the result is truncated to an integer before it is assigned to d.
  • The next statement performs floating-point division of (float)a/b by explicitly casting a to a floating-point value before performing the division operation. The result is then assigned to e.
  • The final printf() function prints the value of e with two decimal places using the %f format specifier.

💻 Program Code

#include <stdio.h>
int main() {
	int a,b,c;
	a = 10;
	b = 6;
	c = a + b;
	printf("%d + %d = %d\n",a,b,c);
	c = a - b;
	printf("%d - %d = %d\n",a,b,c);
	c = a * b;
	printf("%d * %d = %d\n",a,b,c);
	float d = a / b;
	printf("%d / %d = %.2f\n",a,b,d);
	float e =(float)a / b;
	printf("%d / %d = %.2f\n",a,b,e);
	return 0;
}
                        

🖥 Program Output

10 + 6 = 16
10 - 6 = 4
10 * 6 = 60
10 / 6 = 1.00
10 / 6 = 1.67
Press any key to continue . . .
                            

📘 Explanation

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.