Thread: Hey there, me again, another simple program just bugging me, unable to complete

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    13

    Hey there, me again, another simple program just bugging me, unable to complete

    this program takes the input of the user (integer) and reverses the integer and then thats the output but the one problem is that for negatives the reverse output is meant to put the minus sign at the end of the reversed integer but i just cant seem to get the program to do so.
    for example:
    i enter in:
    9876
    output:
    6789
    i enter in negative integer:
    -9876
    output is suppose to be:
    6789-

    any help would be great thanks

  2. #2
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int  main ()
    {
        int number, right_digit;    
        printf("Enter your number. \n");
        scanf("%i", &number);
        
    
    
    do
    {
    
        if(number < 0)
        {
            
            right_digit = -number % 10;
            printf("%i", right_digit);
            number =  number / 10;
            
        }
            
    
        else
        {
        
        
            right_digit = number % 10;
            printf("%i", right_digit);
            number =  number / 10;
        }        
    }
            while(number != 0);
    
        
            
            printf("\n");
        
        
            return 0;
        }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you really must put the minus at the end, then I would just output a hyphen as the last thing.
    Code:
    int neg = number < 0;
    if (neg)
       number = -number;
    
    // do the reversing
    if (neg)
       puts("-");
    You could also just treat the numbers as a string, and reverse the string, but I guess that would be bad for the homework assignment.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    Its not really a homework its just an exercise from a book im trying to learn

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    If its possible how would i reverse a string i tryed to that but it jus wouldnt work

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Hezzuess View Post
    Its not really a homework its just an exercise from a book im trying to learn
    "Homework" is work meant to help you learn, whether it's assigned to you by a professor or by yourself.

    Quote Originally Posted by Hezzuess View Post
    If its possible how would i reverse a string i tryed to that but it jus wouldnt work
    Post your attempt and we can help you fix it.

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    Hey there, me again, another simple program just bugging me, unable to complete-img_1089[1]-jpgHey there, me again, another simple program just bugging me, unable to complete-img_1088[1]-jpgHey there, me again, another simple program just bugging me, unable to complete-img_1087[1]-jpg

  8. #8
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    alright i understand the homework policy, is it possible to reverse an integer if its positive and if negative reverse the positioning of the '-' at the end of the integer. for example if i enter '-567' i normally get '-7-6-5' but i want to get '765-' is this possible with the number being an integer

  9. #9
    Registered User
    Join Date
    Feb 2017
    Posts
    13
    or read question 5 on the book of pics i posted thank you

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    whiteflags addressed this in post #3 - was that advice not sufficient?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An Exercise i am unable to complete fully, Please help.
    By Hezzuess in forum C Programming
    Replies: 9
    Last Post: 02-01-2017, 04:21 PM
  2. Replies: 2
    Last Post: 01-31-2017, 09:31 AM
  3. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM
  4. Hints required to complete simple programs
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:20 PM
  5. Need some really simple help (complete Linux newbie)
    By Hannwaas in forum Linux Programming
    Replies: 11
    Last Post: 12-10-2001, 03:16 PM

Tags for this Thread