Home / C / C Program to find the Largest Digit in the Number

C Program to find the Largest Digit in the Number

Write a Program in C to find the Largest Digit in the number. This is a simple code which will use a While loop or a for loop However while loop is the best option for this type of problem. The two methods are mentioned here but there can be many solutions for the same with each individual having its own programming Logic.

  •   #include <stdio.h>
    
      int main() {
            int num, largest = 0, digit = 0;
    
            /* get the input from the user */
            printf("Enter your input value:");
            scanf("%d", &num);
    
            /* find largest digit of the given number */
      
            while (num > 0) {
                    digit = num % 10;
    
                    if (digit > largest) {
                            largest = digit;
                    }
    
                    num = num / 10;
            }
    
            /* print the largest digit of the entered number */
            printf("Largest digit of the number is %d\n", largest);
            return 0;
      }
  • #include <stdio.h>
    
      int main() {
            int num, largest = 0, digit = 0;
    
            /* get the input from the user */
            printf("Enter your input value:");
            scanf("%d", &num);
    
            /* find largest digit of the given number using FOR Loop*/
      largest=0;
    for(i=num;i>0;i/=10)
    {
    if (i>largest)
    largest=i;
    }
    
            /* print the largest digit of the entered number */
            printf("Largest digit of the number is %d\n", largest);
            return 0;
      }

 

largest digit of a number

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

One comment

  1. Write a C Program to find the second Largest Digit in the Given Number

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.