luc033.c (1304B)
1 /* Write a program to find the range of a set of numbers entered 2 through the keyboard. Range is the difference between the smallest 3 and biggest number in the list. */ 4 /* Let Us C, Chap- 5, Page - 87, Qn No.: B(f) */ 5 6 #include <stdio.h> 7 int main() 8 { 9 int choice = 1, set_of_numbers[30], num, index = -1; 10 while (choice == 1) 11 { 12 printf("\nEnter the number (Type any character and press Enter to finish.) : "); 13 choice = scanf("%d", &num); // Checking whether the user has input any characters 14 if (choice != 1) 15 { 16 // If the user inputs any characters, then choice = 0, it means he doesn't want to give any more input; 17 choice = 0; 18 printf("\nCharacter received. Stopping input...\n"); 19 break; 20 } 21 index++; 22 set_of_numbers[index] = num; 23 } 24 int max = set_of_numbers[0], min = set_of_numbers[0]; 25 while (index >= 0) 26 { 27 if (max < set_of_numbers[index]) 28 max = set_of_numbers[index]; 29 if (min > set_of_numbers[index]) 30 min = set_of_numbers[index]; 31 index--; 32 } 33 int range = max - min; 34 printf("\nBiggest number in the set : %d", max); 35 printf("\nSmallest number in the set : %d", min); 36 printf("\nRange : %d", range); 37 return 0; 38 }