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 <iostream>
#include <conio.h>

using namespace std;

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;
    cout<<"Enter a number ";
    cin>>n;
    cout<<"Largest Digit = "<<largestdigit(n);
    return 0;
}

Output

Enter a number 78261
Largest Digit = 8
video-poster