Saturday, July 16, 2016

A program to find the range of a set of numbers.

Write a program to find the range of a set of numbers. Range is the difference between the smallest and biggest number in the list
Let Us C Page [B] (i)

Woh! This bad ass took a lot of time! First -  the loop would not execute properly. Then the logic would crumble! But finally a little push from one of the helpful member CBoards, I realized my mistake and managed to get this program working. To be honest I started working on this bit code last evening at around 07.00 PM and finally managed to get it working perfectly at about 11.00 AM Morning (obviously with 7 hours of sleep at night :p)

Now, I would really not like to bore you to death with my boring daily routine, would I? So without much delay, here's the shining piece of program in C for finding the range of set of numbers. This question is yet again from the book Let Us C by Yashvant Kanetkar.


/* Write a program to find the range of a set of numbers. Range
is the difference between the smallest and biggest number in
the list

Author : Viks Von Doom
Date : 16/07/2016  Time : 6.51 PM IST */

#include<stdio.h>

int main()

{
    int num_1;
    int max_num=0;
    int min_num=0;
    char choice = 'Y';

    printf("Enter a number : ");
    scanf("%d", &num_1);
    max_num = min_num = num_1;

    while(choice == 'Y')
        {
        if(num_1 >= max_num)
            {
            max_num = num_1;
            }

        else if (num_1 <min_num)
            {
            min_num=num_1;
            }

        printf("Do you want to enter a new number? ( Press  Y for Yes):");
        scanf(" %c",&choice);

        if (choice=='Y')
            {
            printf("Enter another number : ");
            scanf("%d", &num_1);
            }

        else printf(" \n Max Number : %d | Min Number : %d | Difference : %d", max_num,min_num,max_num-min_num);
        }
return 0;
}

The Output :  The program runs and prompts the user to enter a number. The it prompts the user to enter if he wants more numbers to be inputted. The user can input as many numbers as he wants, and then the computer displays the highest and the lowest number and the diffence between the two extremes. 

No comments:

Post a Comment