I’m trying to make a C function which will ask the user to enter in a number (44634329) and will scanf the number, save it to a variable , and will go through digit by digit, and figure out the largest number.
http://pastebin.com/tF7PVtvg – this is my project so far
void extractLargestDigit() {
int i;
i = 0;
int v;
v = 0;
int x;
x = 0;
printf("Enter an integer : ");
scanf("%d", &x);
i = x % 10;
x = x / 10 % 10;
if(i >= x) { i = i; x = x / 10 % 10;}
if(x >= i) { i = x; x = x / 10 % 10;}
if(x = 0) { i = i;}
Right here is where I’m trying to make the program loop so that it will continue to cycle through until x is equal to 0. I’m hoping that this will make i be the largest value in the number and will display it as such. I also have to display at what point the largest digit occurs , like in the number 542356976 , the right most 6 is in the 1st digit position, and the 9 is in the 3rd digit position, I need to display where the largest digit occurs in the number and have not quite figured that out
printf("\nThe largest digit : %d\n", i);
printf("\nIts position : );
return;
}
Any help or insight would be awesome
I would do it in a different way:
This way, you loop through each digit in turn, and the greatest on will be in
maxvarible.