i taking basic introductory class c. got overall program function, not producing required output should.
the task write program asks user integer value , modify input 1s , 100s place have same digit, whatever larger value.
for example:
1234 modified 1434
1969 modified 1969
2025 modified 2525
for reason, instead of taking larger value of 1s , 100s place, program chooses smaller value modify user input.
1234 -> 1232
2025 -> 2020
1962 -> 1262
any hints or ideas on might wrong?
#include <stdio.h> int main() { int myvalue; int tmp; int onedigit; int hundreddigit; int larger; int smaller; int factor; printf("\nenter int: "); scanf("%d", &myvalue); // getting absolute value tmp = (myvalue < 0) ? -myvalue : myvalue; // extracting digits onedigit = tmp % 10; hundreddigit = (tmp / 100) % 10; // grabbing larger , smaller integer larger = (onedigit > hundreddigit) ? onedigit : hundreddigit; smaller = (onedigit > hundreddigit) ? hundreddigit : onedigit; // checking factor factor = (onedigit > hundreddigit) ? 1 : 100; // new modified digit tmp = tmp - (larger - smaller) * factor; printf("\nthe modified value of %d %d\n", myvalue, tmp); return 0; }
the line of code:
// new modified digit tmp = tmp - (larger - smaller) * factor;
doesn't make sense.
in either case (onedigit
larger or hundreddigit
larger), need add value tmp
, , not subtract.
also, have calculated factor reverse. must be:
factor = (onedigit > hundreddigit) ? 100 : 1;