Q:

# C program to find the difference of two numbers

This program will find the difference of two integer numbersDifference is quite different from subtraction, in subtraction we just subtract second number from first number and here to get difference we will subtract smallest number from largest number, so that we can get correct difference of them.

## Difference of two integer numbers program

``````/*C program to find difference of two numbers.*/

#include <stdio.h>

int main()
{

int a,b;
int diff;

printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);

// check condition to identify which is largest number
if( a>b )
diff=a-b;
else
diff=b-a;

printf("\nDifference between %d and %d is = %d",a,b,diff);
return 0;
}``````

Output

```First Run:
Enter first number: 120
Enter second number: 30

Difference between 120 and 30 is = 90

Second Run:
Enter first number: 30
Enter second number: 120

Difference between 30 and 120 is = 90
```

### Using abs() – A Shortest way to find difference of two numbers

By using abs() function we can get the difference of two integer numbers without comparing them, abs() is a library function which is declared in stdlib.h – This function returns the absolute value of given integer.

Consider the example

``````/*C program to find difference of two numbers using abs().*/

#include <stdio.h>
#include <stdlib.h>

int main()
{

int a,b;
int diff;

printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);

diff=abs(a-b);

printf("\nDifference between %d and %d is = %d",a,b,diff);
return 0;
}``````

Output

```First Run:
Enter first number: 120
Enter second number: 30

Difference between 120 and 30 is = 90

Second Run:
Enter first number: 30
Enter second number: 120

Difference between 30 and 120 is = 90```