# question in rounding integer in c

• 04-12-2008
DrGreat
question in rounding integer in c
suppose we want to round all 4-digit integer into 3 digit integers, after checking the fourth digit on right. for example, integer 2343 to be
rounded into 234 while the integer 7887 into 789, since the fourth didgit on right is >=5. write the program needed to repeatdly get a 4 digits integer from the user. stop the processing in the main function when the use provides the integer 99999.

• 04-12-2008
laserlight
What have you tried?
• 04-13-2008
DrGreat
Code:

```int roundNearest (int numb)   { int mod;   mod = numb &#37; 10; numb = numb – mod; numb = numb / 10;     if (mod > 4) numb = numb+1;   return numb;   }     void main() { int input = 1; int output;   while input != 99999 { printf (“please enter an integer of four digits, enter 99999 to exit”) cin >>input if (input != 99999)             { output = roundNearest(input)             Printf (“the rounded number is “)output;             } } }```
• 04-13-2008
laserlight
There are a number of syntax errors in that code, and even if they were fixed, the program would still be incomplete since it is missing headers to be included.

While making your corrections, I suggest that you format your code. When you have done as much as you can, come back with your improvements, and state how does your program still not work.
• 04-13-2008
DrGreat
I asked that question seeking an answer ... I tried solving it and i wrote the code.
I tried hardly doing that coz actually I am a web developer and i dont know much about C.
I expect help from anyone to add corrections to my code ...

Thank you all
• 04-13-2008
Bokarinho
Code:

``` #include <stdio.h> #include <stdlib.h> #include <string.h> unsigned int RountInteger(unsigned int N) {         /* Last digit. */         unsigned int lastDigit = -1;         /* Go to the last digit and get it. */         lastDigit = N &#37; 10;         /* Reduce the last digit. */         N /= 10;         /* Case factors. */         if(lastDigit >= 5)                 return N+1;         return N; } int main(int argc, char* argv[]) {         unsigned int N = 7884;         printf("N = %d\nN Rounded: %d\n", N, RountInteger(N));         N = 7889;         printf("N = %d\nN Rounded: %d\n", N, RountInteger(N)); printf("Hit enter to continue...\n");         getchar();         return 0; }```