In this program, we are going to share C program to find the minimum distance between two numbers. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to find the minimum distance between two numbers with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with the help of c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> #include <stdlib.h> #include <limits.h> int minDist(int arr[], int n, int x, int y) { int i, j; int min_dist = INT_MAX; for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if( (x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > abs(i-j)) { min_dist = abs(i-j); } } } return min_dist; } //program start from here int main() { int arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}; int n = sizeof(arr)/sizeof(arr[0]); int x = 2; int y = 5; printf("Minimum distance between %d and %d is %d\n", x, y, minDist(arr, n, x, y)); return 0; } |
Minimum distance between 2 and 5 is 2
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.