Thread: question in rounding integer in c

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Question 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.


    I need your help please :-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Code:
    int roundNearest (int numb)
    
     
    
    { int mod;
    
     
    
    mod = numb % 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;
    
                }
    
    }
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    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

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 08:31 PM
  2. Rounding Question
    By Daesom in forum C++ Programming
    Replies: 5
    Last Post: 10-31-2006, 08:19 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Rounding a floating point #
    By john_murphy69 in forum C Programming
    Replies: 4
    Last Post: 01-22-2003, 09:47 PM
  5. Very large signed integer math question
    By Criz in forum C Programming
    Replies: 8
    Last Post: 12-01-2002, 12:43 PM