Computer >> Computer tutorials >  >> Programming >> C programming

C program to find sum and difference of two numbers


Suppose we have two integer numbers a, b and two floating point numbers c, d. We shall have to find the sum of a and b as well as c and d. We also have to find the sum of a and c as well. So depending on the printf function style, output may differ.

So, if the input is like a = 5, b = 58 c = 6.32, d = 8.64, then the output will be a + b = 63 c + d = 14.960001 a + c = 11.320000

To solve this, we will follow these steps −

  • To print a + b, they both are integers, so printf("%d") will work

  • To print c + d, they both are floats, so printf("%f") will work

To print a + c, as one of them is integer and another one is float so we shall have to use printf("%f") to get correct result.

Example

Let us see the following implementation to get better understanding −

#include <stdio.h>
int main(){
    int a = 5, b = 58;
    float c = 6.32, d = 8.64;
    printf("a + b = %d\n", a + b);
    printf("c + d = %f\n", c + d);
    printf("a + c = %f\n", a + c);
}

Input

a = 5, b = 58;
c = 6.32, d = 8.64;

Output

a + b = 63
c + d = 14.960001
a + c = 11.320000