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;
}