Largest & Smallest Digits Difference
Largest & Smallest Digits Difference
PROBLEM STATEMENT :
A number N is passed as the input. The program must print the difference between the largest and the smallest digits in the number.
Input Format: The first line will contain the number N.
Boundary Conditions: 10 <= N <= 99999999
Output Format: The difference between the largest and the smallest digits in the number N.
Example Input/Output 1:
Input: 2501
Output: 5
Explanation: The largest digit is 5. The smallest digit is 0. The difference is 5-0 = 5 is printed as the output.
Example Input/Output 2:
Input: 22
Output: 0
Explanation: Both the largest and the smallest digit is 2. Hence the difference 2-2 = 0 is printed as the output.
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,min,max,u;
scanf("%d",&n);
min=n%10;
max=n%10;
while(n)
{
u=n%10;
if(u>max) max=u;
if(u<min) min=u;
n/=10;
}
printf("%d",max-min);
}
C++ (CPP)
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,min,max,u;
cin>>n;
min=n%10;
max=n%10;
while(n)
{
u=n%10;
if(u>max) max=u;
if(u<min) min=u;
n/=10;
}
cout<<max-min;
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),min,max,u;
min=n%10;
max=n%10;
while(n>0)
{
u=n%10;
if(u>max) max=u;
if(u<min) min=u;
n/=10;
}
System.out.println(max-min);
}
}
PYTHON
n=input()
print(int(max(n))-int(min(n)))
Comments
Post a Comment