## Read the description of sum or difference program

Here in this sum or difference program we have to take two integers as input from user. The we have to check if the first number is greater than we have to print their difference. Otherwise we have to print their sum. One thing should take into account that if the two number is equal then it automatically print the sum as the first number is not greater than the second one.

We have taken this problem from CodeChef for practice and learning purpose only of them who want to become a problem solver in future. Here we will discuss the solution this problem using C language. You can use other language too to write the solution to this problem.

You should try to invent other ways to make the solution of this problem. So, let’s see the solution first.

## Solution of this sum or difference problem using C

At first we will take two integer from user and store them in two variable a and b. Then we will use if else condition to check if the first number is greater than second number or not.

If the first number is greater than second, then we will subtract second number from first number and print the result. Otherwise we will print the sum of the two numbers.

**If a user give two number**,

aÂ =Â 10;

bÂ =Â 8;

**result**Â =Â aÂ –Â bÂ =Â (10Â –Â 8)Â =Â 2;

Because **a > b** here.

**Again if he gives two number**,

aÂ =Â 20;

bÂ =Â 25;

**then result**Â =Â aÂ +Â bÂ =Â (20Â +Â 25)Â =Â 45;

Because **a <= b** here.

Now, let’s go to the code bellow to implement this situation.

```
// solution of sum or difference problem using C
#include <stdio.h>
int main(){
int a, b;
scanf("%d", &a);
scanf("%d", &b);
if(a > b){ // check if a > b
printf("%d\n", a - b);
}else{
printf("%d\n", a + b);
}
return 0;
}
```

### Output of this sum or difference program

You will see the following output when you compile and run the above program.

```
82
28
54
```

Here, the user gives 82 as the first number and 28 as the second number. As **82 > 28**, so the program will print their difference which is in third line 54. But run this program and give input 28 as first number and 82 as second number. The output will be as like as bellow.

```
28
82
110
```

However if you give two same number as input, then the program also add them and print the sum of them.

```
70
70
140
```

**Previous page**:Â **Find square root**

**Next page**: Â **Chef and operators**

**Recommended for you**: