Question #343967

Instructions:

  1. Input a non-zero positive integer.
  2. Using the same concept as the previous problem, figure out how to separate the digits of a number and determine which of the digits is the largest one, using a while. Afterwards, print the largest digit.
  3. Tip #1: Create another variable that will hold the largest digit. Initial its value to a negative integer, like -1, outside the loop.
  4. Tip #2: Everytime you get the rightmost digit, check if it is greater than the current largest digit. If it is, set it as the new largest digit.


Input


1. An integer

Output

The first line will contain a message prompt to input the integer.

The second line contains the largest digit of the integer.

Enter·n:·214
Largest·digit·=·4

Expert's answer

#include <stdio.h>


int main()
{
	int n,d;
	int max = -1;

	printf("Enter n: ");
	scanf("%d", &n);
	while (n != 0)
	{
		d = n % 10;
		if (d > max)
			max = d;
		n /= 10;
	}
	printf("Largest digit = %d", max);

	return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS