Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wrote a program that prints the largest digit of a number

#include <stdio.h>
int main(){
    int num ,  large = 0, rem = 0;
    scanf("%d" , &num);
    while (num > 0) {
                rem = num % 10;

                if (rem > large) {
                        large = rem;
                }

                num = num / 10;
        }

        printf("the biggest digit is %d\n", large);
        return 0;
  }


if working fine but if i input an negative number lets say -432 it prints me 0 , instead of 4. How can i correct that ?

What I have tried:

.........................................
Posted
Updated 18-Nov-22 22:52pm

1 solution

C++
while (num > 0)

This will fail for numbers less than 0. Convert to an absolute number before testing will fix it:
C++
num = abs(num);
while (num > 0)
 
Share this answer
 
Comments
CPallini 19-Nov-22 5:44am    
5.
Graeme_Grant 19-Nov-22 5:45am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900