C Program that prints out the maximum and minimum values from user input involving precision decimals

c programming exercises with solutions pdf
write ac program to print a big 'c
how to input data in c
input output functions in c language
write a c program to print the following characters in a reverse way
how can the assignment of an input data item to its corresponding argument be suppressed
c programming exercises online
formatted output in c language

I am a newbie programmer and just started to teach myself C then decided to tackle some simple problems taken from the internet. Specifically with this one: Problem

And my solution is:

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <limits.h>

int main() {
    double min = DBL_MAX;
    double max = DBL_MIN;
    double n;

    do {
        scanf("%lf", &n);

        if (n < 0.0001) {
            break;
        }

        if (n > max) {
            max = n;
        }

        if (n < min) {
            min = n;
        }
    } while (n > 0.0000);

    printf("Min: %.4f Max: %.4f\n", min, max);
    return 0;
}

However, I need to run my program exactly as the input/output specified by the problem. For example, consider I am inputting in different lines:

1.1 1.2 1.3 3 6 9 11 -2 7
2.2
2.3 12
0

The program should output 12 as the max and -2 as the min, the program ends when the 0 was inputted.


You need to set min and max BOTH to the first value set by scanf and then check for greater and less than on later iterations. The way it works now, min starts at 0 and will never change as nothing can be lower than that without the program exiting.

int main(){
  double min = 0; 
  double max = 0; 
  float n; 
  int count = 0; 

  do{
    scanf("%f", &n);

    if(n < 0.0001){
      break;
    }  
    if( count ) {
        if(n > max){
            max = n; 

        } else if(n < min){
            min = n; 
        }  
    } else {
        max = n; 
        min = n; 
        count = 1; 
    }  

  }while(n > 0); 

  printf("Min: %.4f Max: %.4f\n", min, max);

  return 0; 
}

Also, the proper type for float is %f.

Data Input and Output in C, Part 2, C Program that prints out the maximum and minimum values from user input involving precision decimals I am a newbie programmer and just started to teach myself C then decided to tackle some simple problems taken from the internet. Write a c++ program that initially takes an integer value from the user as the loop’s limit. It means that the loop will execute the same times as the input from the user. The loop will then take integer values as input from the user until it reaches loop limit.


Make sure you read the problem description again, your logic in the code is tainted in comparison to what's being attempted.

According to the problem description, n can be an int as you're using it to define the number of values to be given to the program.

You can then use a for loop (for(i=0; i < n; i++){CODE HERE}) to gather more input from the user. The first number given should be your base value for the min and max values.

After you have a base value you can compare min and max to each input thereafter. (ie. if(max < input) {max = input} and if(min > input) {min = input})

If you have anymore questions feel free to inbox me and I'll try to help you work out the problem :)

C Basics - C Programming Tutorial, The previous article on input and output functions dealt with the The following program illustrates the use of the minimum field width feature The maximum number of decimal places that can be printed define the precision of the field value has been rounded off to conform to the precision specification. For example, 5.48958123 should be printed as 5.4895 if given precision is 4. For example, below program sets the precision for 4 digits after the decimal point: In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf (). Below is program to demonstrate the same.


  1. Using math.h you can use INFINITY and -INFINITY. http://pubs.opengroup.org/onlinepubs/007904975/basedefs/math.h.html
  2. Comparing doubles with literals in order to make decisions is tricky at best. Besides it would be better to have positive as well as negative numbers in your program.
  3. fflush(stdin) gets rid of any previous input in the buffer.
  4. Notice that max starts at -INFINITY and min starts at +INFINITY.
  5. Comparing a float with a double will result in the calculation being performed using the double type anyways. Try not to mix these without a good reason.

#include <stdio.h>
#include <math.h>

int main()
{
    double min = +INFINITY;
    double max = -INFINITY;
    double n;
    char ans = 'n';

    do
    {
        printf("Enter a number: ");
        scanf("%lf", &n);

        if(n > max)
        {
            max = n;
        }

        if(n < min)
        {
            min = n;
        }
        fflush(stdin);  // gets rid of previous input
        printf("Another number? (y/n): ");
        scanf("%c", &ans);
    }
    while(ans == 'y');

    printf("Min: %.4f Max: %.4f\n", min, max);
    return 0;
}

Output:

Enter a number: -45.6
Another number? (y/n): y
Enter a number: 23.0
Another number? (y/n): y
Enter a number: 92.3
Another number? (y/n): y
Enter a number: -100.22
Another number? (y/n): n
Min: -100.2200 Max: 92.3000

Process returned 0 (0x0)   execution time : 15.805 s
Press any key to continue.

C Programming basic - Exercises, Practice, Solution, This is because period crashes with decimal point - it is hard for the dumb computer to into our program, so as to use the IO library function to carry out input/output operations int max = 2147483647 int min = -2147483648 unsigned int max C programs use function printf() of library stdio to print output to the console. Assignments » Flow Of Control » Set2 » Solution 10. Write a program to enter the numbers till the user wants and at the end it should display the maximum and minimum number entered.


C Exercises: Print out the difference of the maximum and minimum , Practice with solution of exercises on C programming basic: Examples Write a C program that accepts two integers from the user and Input the second integer​: 38 Print the employee's ID and salary (with two decimal places) of a print out the difference of the maximum and minimum values of these  In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. Example 5: Integer Input/Output


Learn C programming language Simply: Learn C programming language , C programming, exercises, solution: Write a C program that accepts 4 real numbers from the keyboard and print out the difference of the maximum and minimum values of these four numbers. Note: Use 4-decimal places. a2, a3, a4; double max, min; printf("Input four numbers: \n"); scanf("%lf%lf%lf%lf",  The function of this program is repeatedly prompting a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. Suppose all input is proper, so we do not need to check whether the input value is illegal or not.


Printf format strings, For the floating-point variable, flt_num, line 13 prints out the floating-point value in by the statement in line 14 with the precision specifier for two decimal places​. stdin, stdout, and stderr are three file streams that are pre-opened for you to use. and getchar() can be used to read in one character from the standard input. You can't use [code]if( a < b < c )[/code], because this won't do what you think it will. The short answer is that you need to make two separate comparisons and join