1

I am taking a basic introductory class to C. I got the overall program to function, but it is not producing the required output as it should.

The task is to write a program that asks the user for an integer value and to modify the input so that the 1s and 100s place will have the same digit, whatever is the larger value.

For example:

1234 will be modified to 1434

1969 will be modified to 1969

2025 will be modified to 2525

For some reason, instead of taking the larger value of the 1s and 100s place, the program chooses the smaller value to modify the user input.

1234 -> 1232

2025 -> 2020

1962 -> 1262

Any hints or ideas on what might be wrong?

#include <stdio.h>
int main() {

    int myValue;
    int tmp;
    int oneDigit;
    int hundredDigit;
    int larger;
    int smaller;
    int factor;

    printf("\nEnter an int: ");
    scanf("%d", &myValue);

    // Getting absolute value
    tmp = (myValue < 0) ? -myValue : myValue;

    // Extracting digits
    oneDigit = tmp % 10;
    hundredDigit = (tmp / 100) % 10;

    // Grabbing larger and smaller integer
    larger = (oneDigit > hundredDigit) ? oneDigit : hundredDigit;
    smaller = (oneDigit > hundredDigit) ? hundredDigit : oneDigit;

    // Checking what to factor by
    factor = (oneDigit > hundredDigit) ? 1 : 100;

    // New modified digit
    tmp = tmp - (larger - smaller) * factor;

    printf("\nThe modified value of %d is %d\n", myValue, tmp);

    return 0;

}

3 Answers 3

1

The line of code:

// New modified digit
tmp = tmp - (larger - smaller) * factor;

doesn't make any sense.

In either case (oneDigit is larger or hundredDigit is larger), you need to add some value to tmp, and not subtract.

Also, you have calculated the factor as reverse. It must be:

factor = (oneDigit > hundredDigit) ? 100 : 1;
Sign up to request clarification or add additional context in comments.

Comments

1
  1. You need to reverse the factor in this statement.

    factor = (oneDigit > hundredDigit) ? 1 : 100;
    

    Use

    factor = (oneDigit > hundredDigit) ? 100 : 1;
    
  2. You need to add (larger - smaller) * factor; to tmp, not subtract.

    Instead of:

    tmp = tmp - (larger - smaller) * factor;
    

    use:

    tmp = tmp + (larger - smaller) * factor;
    

1 Comment

Thank you for your quick response. I've tried your method, but when I input 1234, it gives me 1236, which is not the expected output However, when I enter in 1232, it returns 1232, which is the expected output. Do you know why this is happening? Even after modifying the two sections of code that you pointed out was incorrect. Thank you again.
1

change this:

factor = (oneDigit > hundredDigit) ? 1 : 100;

to

factor = (oneDigit > hundredDigit) ? 100 : 1;

and also change

 tmp = tmp -(larger - smaller) * factor;

to

  tmp = tmp +(larger - smaller) * factor;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.