Create Two Numbers Using Higher and Lower Halves of Each Digit

The program must accept an integer N as the input. The task is to form two new numbers by using the upper half and lower half of each digit of N. If N has an odd number of digits, then consider the central digit’s upper half and lower half as a whole digit for the two new numbers respectively.

Boundary Condition(s):
1 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains two integers separated by a space, representing the two numbers formed by the upper half and the lower half of the digits of N.

Example Input/Output 1:
Input:
258

Output:
203 805

Explanation:
For the digit 2, the upper half is 2 and the lower half is 0.
For the digit 5 (central digit), the upper half and lower half are 5 and 5.
For the digit 8, the upper half is 0 and the lower half is 8.
So, the two numbers are 203 and 805.

Example Input/Output 2:
Input:
13492

Output:
10302 90402

n = input().strip()
upper_half = ''
lower_half = ''
for digit in n:
    if digit in '0123':
        upper_half += digit
        lower_half += '0'
    else:
        upper_half += '0'
        lower_half += digit
print(upper_half, lower_half)
#include<stdio.h>
#include<string.h>

int main() {
    char n[10];
    scanf("%s", n);
    int len = strlen(n);
    char upper_half[len+1], lower_half[len+1];
    for(int i = 0; i < len; i++) {
        if(n[i] >= '0' && n[i] <= '3') {
            upper_half[i] = n[i];
            lower_half[i] = '0';
        } else {
            upper_half[i] = '0';
            lower_half[i] = n[i];
        }
    }
    upper_half[len] = '';
    lower_half[len] = '';
    printf("%s %s", upper_half, lower_half);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    string upper_half = "", lower_half = "";
    for(char digit : n) {
        if(digit >= '0' && digit <= '3') {
            upper_half += digit;
            lower_half += '0';
        } else {
            upper_half += '0';
            lower_half += digit;
        }
    }
    cout << upper_half << " " << lower_half;
    return 0;
}
import java.util.Scanner;

public class HalvesOfDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        StringBuilder upper_half = new StringBuilder();
        StringBuilder lower_half = new StringBuilder();
        for(char digit : n.toCharArray()) {
            if(digit >= '0' && digit <= '3') {
                upper_half.append(digit);
                lower_half.append('0');
            } else {
                upper_half.append('0');
                lower_half.append(digit);
            }
        }
        System.out.println(upper_half + " " + lower_half);
    }
}
Previous Article

Create Two Numbers from Odd and Even Digits

Next Article

Count of Even Digits in Odd Positions

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *