2

write a program that repeatedly asks a user to enter numbers between 0 to 100, until the user enters a value outside that range. Then output the number closest to 50.

For example:

Enter a number: 91.3
Enter a number: 64.4
Enter a number: 38.12
Enter a number: 46.9
Enter a number: 99.45
Enter a number: 103.

The number closest to 50 is 46.900000

I have only learnt do,while,for loops, if and else if selection statements.

I understand I would need an if statement that checks the difference between each number with 50, the smaller the difference, the closer the number to 50. I included stdlib.h because I figured I would need to take the absolute value of the difference to avoid negative number. Yet I do not know how to incorporate this thought into the codes. How should the code be written?

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

int main (void)
{
  float num;
  printf("Enter a number:");
  scanf("%f",&num);

  while (num>=0 && num<=100)
  {
    printf("Enter a number:");
    scanf("%f",&num);
    //I think I would need an if statement here to check the difference
  }
  printf("\nThe closest is %.2f",/*I think I would need a new variable here.*/ );
  return 0;
}
3
  • All you need is fabs(50-num), this lives in math.h though. May 16, 2014 at 6:03
  • Initialize the closest number with first input and keep updating this number with every next input. Print the final result. If input is > 50, new difference would be (num - 50.0) otherwise new difference would be (5.0 - num)
    – Mohit Jain
    May 16, 2014 at 6:07
  • If you don't want to include math.h, you could define your own abs: #define ABS(a) (a) > 0.0 ? (a) : -(a), but best to just include math.h. By the way, the ? and : form a ternary operator.
    – ilent2
    May 16, 2014 at 6:17

3 Answers 3

4

It is a math problem hidden in a programming problem.

To find the closest number, you would have the smallest absolute value of the difference between two numbers, so you need to take the difference of all the numbers and keep in mind which difference (and the number it corresponds to) has the smallest absolute value.

1

After reading each number, find the difference between that number and 50. Keep track of the minimum difference found till now and compare it with current difference. Update the minimum difference

float result;
float mindiff = 100; // minimum difference, initialize with any value greater than 50
scanf("%f", &num);
while(num>=0 && num<=100)
{
    //check with minimum till now and update accordingly
    if( fabs(num-50) < mindiff)
    {
        mindiff = fabs(num-50);
        result = num;
    }

    //read next number
    printf("Enter a number:");
    scanf("%f",&num);
}

Now result holds the value you need

0

You would need an additional variable float ClosestToFifty which would be updated with a statemet like the following.

if (fabs(num - 50.0f) < fabs(ClosestToFifty - 50.0f) )
{
    ClosestToFifty = num:
}

ClosestToFifty could be initialized with some value outside the valid range, say 200.0f.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.