The program must accept an integer N as the input. The task is to find the largest and smallest digit in the number and then print the difference between their squares. If the number contains only a single digit, the output should be the square of that digit.
Boundary Condition(s):
0 <= N <= 10^18
Input Format:
The first line contains the integer N.
Output Format:
A single line contains the difference between the squares of the largest and smallest digits.
Example Input/Output 1:
Input:
42715
Output:
44
Explanation:
The largest digit in the number is 7 and its square is 49.
The smallest digit in the number is 1 and its square is 1.
Hence the difference is 49-1 = 48.
Example Input/Output 2:
Input:
9
Output:
81
Explanation:
Since the number contains only one digit, the output is the square of that digit, which is 9*9 = 81.
N = input().strip()
max_digit = max(N)
min_digit = min(N)
print(int(max_digit)**2 - int(min_digit)**2)
#include<stdio.h>
int main() {
char N[20];
scanf("%s", N);
char max_digit = '0', min_digit = '9';
for(int i = 0; N[i] != ' '; i++) {
if(N[i] > max_digit) max_digit = N[i];
if(N[i] < min_digit) min_digit = N[i];
}
printf("%d", (max_digit - '0') * (max_digit - '0') - (min_digit - '0') * (min_digit - '0'));
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string N;
cin >> N;
char max_digit = '0', min_digit = '9';
for(char digit : N) {
if(digit > max_digit) max_digit = digit;
if(digit < min_digit) min_digit = digit;
}
cout << (max_digit - '0') * (max_digit - '0') - (min_digit - '0') * (min_digit - '0');
return 0;
}
import java.util.Scanner;
public class DigitSquareDifference {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String N = sc.next();
char max_digit = '0', min_digit = '9';
for(char digit : N.toCharArray()) {
if(digit > max_digit) max_digit = digit;
if(digit < min_digit) min_digit = digit;
}
System.out.println((max_digit - '0') * (max_digit - '0') - (min_digit - '0') * (min_digit - '0'));
}
}
Leave a Reply