Find the second largest digit in a number
Answers
Answered by
1
int main()
{ int n, ld, sld; printf("Enter a positive number number : "); scanf("%d",&n); if ( n <= 0 ) { printf("no cheating, please\n"); // handling of non-positive numbers left as exercise return -1; } ld = -1; sld = -1; while (n) { int r = n % 10; if ( ld < r ) { if ( sld < ld) sld = ld;; ld = r; } else if ( ld > r && sld < r) { sld = r; } n /= 10; } if (ld != -1) printf("largest digit is %d\n", ld); if (sld != -1) printf("second largest digit is %d\n", sld); return 0; }
{ int n, ld, sld; printf("Enter a positive number number : "); scanf("%d",&n); if ( n <= 0 ) { printf("no cheating, please\n"); // handling of non-positive numbers left as exercise return -1; } ld = -1; sld = -1; while (n) { int r = n % 10; if ( ld < r ) { if ( sld < ld) sld = ld;; ld = r; } else if ( ld > r && sld < r) { sld = r; } n /= 10; } if (ld != -1) printf("largest digit is %d\n", ld); if (sld != -1) printf("second largest digit is %d\n", sld); return 0; }
Similar questions