# rounding program...s.o.s.

• 02-11-2002
pancho
rounding program...s.o.s.
here is the problem..
The function floor is one of the functions available in the C math library (floor(x) rounds the decimal number x to the largest integer not greater than x). The function floor can be used to round a number to to a specific decimal place. For example, the statement
y = floor( x*100 + .5)/100;
rounds x to the hundredths position.
Write a program that defines a function called roundtohundreths that rounds a number to the nearest hundredth. For each value read, your program should display the original value (a decimal number) and the number rounded. Your program should have a loop prompting the user to enter a "Y," if a number is to be entered, or a "N" if no new number will be entered.

here is my code so far.. which is not working very well----

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

int roundtohundreths (int, int);
int main ()
{
int t, b, N, Y;
float x, y;

printf( "Type Y for entering a number ( N to end ): " );

while( ( t = getchar() )!= N ) {
switch ( t ) {

case 'Y': case 'y':
printf( "Enter number: " );
scanf( "%f", &x);
y = ( x * 100 + .5 ) / 100;
printf( "Original value is %.4f\n", y );
printf( "Rounded number is %d\n", roundtohundreths(x, y ) );
}

printf( "Type Y for entering a number ( N to end ): " );
scanf( "%d", &b );
}
return 0;
}
int roundtohundreths (int a, int b )

{
int roundto = a;
a = floor( b * 100 + .5 ) / 100;

return roundto;
}

help would be greatly appreciated!!!!!!!!!
• 02-11-2002
Prelude
Try this:
Code:

```#include <stdio.h> #include <math.h> double roundtohundreths (double); int main () {   int t = 'y';   double x;    while( t != 'N' && t != 'n' ) {     if ( t == 'Y' || t == 'y' ) {       printf( "Enter number: " );       scanf( "%lf", &x); while ( getchar() != '\n' );        printf( "Original value is %.4lf\n", x );       printf( "Rounded number is %.2lf\n", roundtohundreths( x ) );     }     printf( "Type Y for entering a number ( N to end ): " );     t = getchar();   }   return 0; } double roundtohundreths (double a) {   double roundto = floor( a * 100 + .5 ) / 100;   return roundto; }```
-Prelude
• 02-11-2002
pancho
thanks a lot!!!
:D

thanks man....the program works pretty well....
even though is 100 percent right i am going to try to recode mine based on yours...its because i dont like easy things, i like to figure out...

thanks again---
• 02-11-2002
Prelude
>i like to figure out
Someone after my own heart. My motto is 'What good is asking for help if you learn better by muddling through on your own?'

-Prelude