Arithmetic Operations example in c programming langauage
C Programming Language Operators and Enums in C Language (Article) Operators and Enums in C Language (Program)
1338
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 <stdio.h>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, andcare declared and initialized with values of 10, 6, and the result ofa+brespectively. - The first
printf()function prints the values ofa,b, andcwith the+operator to perform addition and the=operator to assign the result toc. - The next three statements use the
-,*operators to perform subtraction, multiplication operations and assigns the results toc. - The next
printf()function performs integer division ofa/band prints the result with two decimal places using the%fformat specifier. Note that sinceaandbare both integers, integer division is performed and the result is truncated to an integer before it is assigned tod. - The next statement performs floating-point division of
(float)a/bby explicitly castingato a floating-point value before performing the division operation. The result is then assigned toe. - The final
printf()function prints the value ofewith two decimal places using the%fformat specifier.
Program:
#include 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; }
Output:
10 + 6 = 16 10 - 6 = 4 10 * 6 = 60 10 / 6 = 1.00 10 / 6 = 1.67 Press any key to continue . . .
Explanation:
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.