Create Two Numbers from Odd and Even Digits

The program must accept an integer N as the input. The program should create two new numbers – one using the odd digits and another using the even digits from N. If there are no odd digits, then the output for the odd number should be -1. Similarly, if there are no even digits, then the output for the even number should be -1.

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

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the number formed from odd digits of N.
The second line contains the number formed from even digits of N.

Example Input/Output 1:
Input:
12345

Output:
135
24

Explanation:
The odd digits in 12345 are 1, 3, and 5. So the first number is 135.
The even digits are 2 and 4. So the second number is 24.

Example Input/Output 2:
Input:
24680

Output:
-1
24680

Explanation:
There are no odd digits in 24680. So the first number is -1.
All digits are even in 24680. So the second number is 24680.

n = input().strip()
odd_digits = ''.join([digit for digit in n if int(digit) % 2 != 0])
even_digits = ''.join([digit for digit in n if int(digit) % 2 == 0])

print(odd_digits if odd_digits else "-1")
print(even_digits if even_digits else "-1")
#include<stdio.h>
#include<string.h>

int main() {
    char n[10];
    char odd_digits[10] = "";
    char even_digits[10] = "";
    scanf("%s", n);
    for(int i = 0; n[i] != ''; i++) {
        if((n[i] - '0') % 2 == 0) {
            strncat(even_digits, &n[i], 1);
        } else {
            strncat(odd_digits, &n[i], 1);
        }
    }
    printf("%sn", strlen(odd_digits) ? odd_digits : "-1");
    printf("%s", strlen(even_digits) ? even_digits : "-1");
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n, odd_digits = "", even_digits = "";
    cin >> n;
    for(char ch : n) {
        if((ch - '0') % 2 == 0) {
            even_digits += ch;
        } else {
            odd_digits += ch;
        }
    }
    cout << (odd_digits.length() ? odd_digits : "-1") << endl;
    cout << (even_digits.length() ? even_digits : "-1");
    return 0;
}
import java.util.Scanner;

public class OddEvenDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        StringBuilder oddDigits = new StringBuilder();
        StringBuilder evenDigits = new StringBuilder();
        for(char ch : n.toCharArray()) {
            if((ch - '0') % 2 == 0) {
                evenDigits.append(ch);
            } else {
                oddDigits.append(ch);
            }
        }
        System.out.println(oddDigits.length() > 0 ? oddDigits : "-1");
        System.out.println(evenDigits.length() > 0 ? evenDigits : "-1");
    }
}
Previous Article

Division of Largest and Smallest Digits

Next Article

Create Two Numbers Using Higher and Lower Halves of Each Digit

Write a Comment

Leave a Comment

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