Write a C program (Rounding Numbers) Function floor may be used to round a numbe
ID: 3806995 • Letter: W
Question
Write a C program
(Rounding Numbers) Function floor may be used to round a number to a specific decimal place. The statement
y = floor( x * 10 + .5 ) / 10;
rounds x to the tenths position (the first position to the right of the decimal point). The statement
y = floor( x * 100 + .5 ) / 100;
rounds x to the hundredths position (the second position to the right of the decimal point). Write a program that defines four functions to round a number x in various ways
a) roundToInteger( number )
b) roundToTenths( number )
c) roundToHundreths( number )
d) roundToThousandths( number )
For each value read, your program should print the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth, and the number rounded to the nearest thousandth.
Explanation / Answer
For this program first we get input from user after that we create the 4 functrions
roundToInteger( number )
roundToTenths( number )
roundToHundreths( number )
roundToThousandths( number )
for find the nearest value.In this function we pass the input value and store the result in different variable.
Then print this value for output.
This is the program how this is work.
#include<stdio.h>
#include<Math.h>
float roundToInteger ( float x ) // function definition
{
float y ;
y= floor(x*1+0.5);
return ( y ) ;
}
float roundToTenths ( float x ) // function definition
{
float y ;
y= floor(x*10+0.5)/10;
return ( y ) ;
}
float roundToHundreths ( float x ) // function definition
{
float y ;
y= floor(x*100+0.5)/100;
return ( y ) ;
}
float roundToThousandths ( float x ) // function definition
{
float y ;
y= floor(x*1000+0.5)/1000;
return ( y ) ;
}
int main( )
{
float m, n ,a,b,c;
printf ( " Enter the number ");
scanf ( "%f", &m ) ;
n = roundToInteger( m );
a = roundToTenths( m );
b = roundToHundreths( m );
c = roundToThousandths( m );
printf ( " Interger of %f is %3.0f",m,n );
printf ( " Tenth of %f is %3.1f",m,a );
printf ( " Hundredth of %f is %3.2f",m,b );
printf ( " Thousandth of %f is %3.3f",m,c );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.