Thread: Displaying the rightmost digit of a positive number

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Question Displaying the rightmost digit of a positive number

    Hi all,

    I recently received the following programming assignment and am a little stumped. The question is as follows:

    "Write a program to take a non-negative integer and display its rightmost (least significant) digit, and the number with its rightmost digit zeroed.

    Example input/output:

    Please enter a non-negative integer >> 128
    Right-most digit = 8
    Number with right-most digit zeroed = 120

    Hint: Consider using the following operators: / and % (divide and modulus)"

    As for the first part, displaying the rightmost digit, I have no idea how to approach it so if anyone could simply start me off that'd be great.

    To display the number with its rightmost digit zeroed I know how to do it if the user enters a number already zeroed eg 130 but I do not know how to change say 135 to 130. The only piece of code I have so far is as follows(and is only a rough draft):

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int pos_num, num_zeroed = 0;
    	printf("Please enter a positive integer:");
    
    	scanf("%d", &pos_num);
    	if (pos_num >= 0) 
    	{
    		if (pos_num % 10 == 0)
    		{
    			num_zeroed = pos_num;
    			printf("The number zeroed is %d\n", num_zeroed);
    		}
    
    	}
    	else 
    		printf("Number entered must be a positive integer\n");
    		printf("please enter a positive integer:");
    }
    Thanks in advance for your help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hint: Consider using %.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Then perhaps you should take some time out to brush up on basic math skills before diving into programming.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    I second the suggestion that you brush up on some basic arithmetic. I would also second the suggestion you look into modulus. Here's a very suggestive hint:

    122 mod 10 = 2

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Yep I get it thanks. Also how to zero the number:
    Code:
    if (x % 10 == 0)//if number is evenly divisible by ten the right most digit is already 0
    			{
    				num_zeroed = x;
    				printf("The number zeroed is %d\n", num_zeroed);
    			}
    
    			else 
    			{
    				num_zeroed = x - x%10;
    				printf("The number zeroed is %d\n", num_zeroed);
    			}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    More basic math. What happens when you divide by ten and then multiply by ten?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    It'll give me the same result as the code above.
    The code above works fine as far as I can tell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  3. A simple program about displaying number
    By sundar in forum C Programming
    Replies: 17
    Last Post: 08-08-2008, 10:07 AM
  4. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM