Dremendo Tag Line

Input a number and find the largest digit in it using a function in C

Function - Question 3

In this question, we will see how to input a number and find the largest digit in it in C programming using a function. To know more about function click on the function lesson.

Q3) Write a program in C to input a number and find the largest digit in it using a function.

Program

#include <stdio.h>
#include <conio.h>

int largestdigit(int num)
{
    int ld=0,d;
    while(num>0)
    {
        d=num%10;
        if(d>ld)
        {
            ld=d;
        }
        num=num/10;
    }
    return ld;
}

int main()
{
    int n;
    printf("Enter a number ");
    scanf("%d",&n);
    printf("Largest Digit = %d",largestdigit(n));
    return 0;
}

Output

Enter a number 78261
Largest Digit = 8
video-poster