This blog is under construction

Tuesday 9 July 2013

C program to find largest digit of a number

Write a C program to find largest digit of a number.


  #include <stdio.h>

  int main() {
        int num, large = 0, rem = 0;

        /* get the input from the user */
        printf("Enter your input value:");
        scanf("%d", &num);

        /* finding the largest digit of the given input */
        while (num > 0) {
                rem = num % 10;

                if (rem > large) {
                        large = rem;
                }

                num = num / 10;
        }

        /* print the largest digit of the number */
        printf("Largest digit of the number is %d\n", large);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input value:1234589
  Largest digit of the number is 9






See Also:

5 comments:

  1. i need explanation of this above program.

    ReplyDelete
  2. Need explanation for this program

    ReplyDelete
  3. Need explanation for this program

    ReplyDelete
  4. Thanka alot, its helphelpfor me :)

    ReplyDelete
  5. This doesn't work for negative numbers.

    ReplyDelete