Find Largest Digit of a Number C Program

By | June 26, 2016

C Program To Find Largest Digit of a Number

Learn How To Find Largest Digit of a Number in C Programming Language. This C Program To Search the Largest Digit in an Integer makes use of Modulus and Division operators primarily. The Modulus operator is used to extract the digit at the end of a given number.

We have written two methods to Search for the Largest Digit in a Number which includes a For Loop and a While Loop.

C Program To Find Largest Digit of a Number using While Loop

#include<stdio.h>

int main()
{
    int num, temp = 0, large = 0;
    printf("\nEnter A Number:\t");
    scanf("%d", &num);
    while(num > 0)
    {
        temp = num % 10;
        if(temp > large)
        {
            large = temp; 
        }
        num = num / 10;               
    }       
    printf("\nLargest Digit in the Integer: \t%d\n", large);
    return 0;
}

C Code To Find The Largest Digit in an Integer using For Loop

#include<stdio.h>

int main()
{
    int num, temp = 0, large = 0;
    printf("\nEnter A Number:\t");
    scanf("%d", &num);
    for(; num > 0; num = num/10)
    {
        temp = num % 10;
        if(temp > large)
        {
            large = temp;   
        }               
    }       
    printf("\nLargest Digit in the Integer: \t%d\n", large);
    return 0;
}

Output

C Program To Find Largest Digit of a Number

In case you find any error in the above C Program Code To Find Largest Digit of a Number or if you have any doubts, let us know about it in the Comment Section below.

Recommended Programs
C Program For Tower of Hanoi Algorithm
Sum of Series: 1+ 1/2 + 1/3 + 1/4 + …. + 1/n
C Program To Display Current Date and Time
C Program To Convert Hexadecimal Number To Binary Value
C Program To Reverse a Number
100+ C Programs For Programming Interviews

One thought on “Find Largest Digit of a Number C Program

  1. Shashi Thakur

    I am not understanding the difference between Modulus and Division operator in this C Program to find smallest digit in an integer. Please help.

    Reply

Let's Discuss