Question

to ask user to input a float number, then round it up to the second digit 4. Write a C program after decimal point and output it. (6 points)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

// round to 2 decimal places

float _round(float x)

{

    // 36.66666 * 100 =3666.66

    // 0.5 is added for rounding off value

    // then type cast to int to convert to nearest integer

    float ans = (int)(x * 100 + .5);

   

    // then divided by 100

    return (float)ans / 100;

}

int main()

{

    float x;

   

    printf("Enter a float number : ");

   

    // get user input

    scanf("%f", &x);

   

    // print digit upto 2 decimal places

    printf("%f", _round(x));

   

   return 0;

}

Sample Output

a float number : 2.3657 2.370000

Add a comment
Know the answer?
Add Answer to:
to ask user to input a float number, then round it up to the second digit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. Write a  C program to ask user  to input a float number, then round it up to...

    4. Write a  C program to ask user  to input a float number, then round it up to the second digit after decimal point and output it.

  • Write a C program that counts the number of each digit entered by the user (Input...

    Write a C program that counts the number of each digit entered by the user (Input process should be ended using EOF character). You should use switch to compute the number of each digits. For each digit your program should print a line containing that number of adjacent asterisks. Sample Input1: 18218342850056D855599 Sample Outputi: 0** 3° 5*** 6° 7 g*** Sample Input: 77777445521056 Sample Output2: 0* 1 2° 3 45 S. 6° 8 9 Sample Output 3: Sample Input3: 782222231567799988001332092555...

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should...

    Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.ser to input a number, and then using a loop control statement (While, For, Do-While), output to the console the numbers beginning from 1, up to and including the number input by the user.

  • Question 4 (3 mark) : Write a Java program to ask the user to input an...

    Question 4 (3 mark) : Write a Java program to ask the user to input an integer number from the keyboard. The output should indicate whether it is positive, zero, or negative, an even number or an odd number. REQUIREMENTS • Your code should ask user to input an integer number, then decide its sign and parity based on this input. • Your code should use if-else statement. Your code must work exactly as the specification and the output should...

  • Python: Write a code to ask a number (one digit) from the user and prints the...

    Python: Write a code to ask a number (one digit) from the user and prints the respective row of the multiplication table. For example, if the user enters 5, the following lines will be printed. Note that, if the user enters any number outside the range of 1-9, the program should display an error message. 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT