C program to perform basic arithmetic operations which are addition, subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

 

[ad type=”banner”]

C programming code

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20int%20first%2C%20second%2C%20add%2C%20subtract%2C%20multiply%3B%0A%20%20%20float%20divide%3B%0A%20%0A%20%20%20printf(%22Enter%20two%20integers%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%20%26first%2C%20%26second)%3B%0A%20%0A%20%20%20add%20%3D%20first%20%2B%20second%3B%0A%20%20%20subtract%20%3D%20first%20-%20second%3B%0A%20%20%20multiply%20%3D%20first%20*%20second%3B%0A%20%20%20divide%20%3D%20first%20%2F%20(float)second%3B%20%20%20%2F%2Ftypecasting%0A%20%0A%20%20%20printf(%22Sum%20%3D%20%25d%5Cn%22%2Cadd)%3B%0A%20%20%20printf(%22Difference%20%3D%20%25d%5Cn%22%2Csubtract)%3B%0A%20%20%20printf(%22Multiplication%20%3D%20%25d%5Cn%22%2Cmultiply)%3B%0A%20%20%20printf(%22Division%20%3D%20%25.2f%5Cn%22%2Cdivide)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

 

[ad type=”banner”]

Download Arithmetic operations program.

 

[ad type=”banner”]

Output of program:

 

[ad type=”banner”]

In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in numerator. This explicit conversion is known as typecasting.

Categorized in: