Home » C solved programs » C basic programs

# C program to find the difference of two numbers

This program will find the **difference of two integer numbers**. **Difference** 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 = 90Second 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 = 90Second Run:Enter first number: 30 Enter second number: 120 Difference between 30 and 120 is = 90

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.