Create Number from Outer to Center Digits

The program must accept an integer N as the input. The program should create a new number by rearranging the digits of N from outer to center.

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

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the new number formed from the outer to the center digits of N.

Example Input/Output 1:
Input:
12345

Output:
15243

Explanation:
The digits are rearranged from outer to center as follows 1 2 3 4 5 => 1 5 2 4 3

Example Input/Output 2:
Input:
123456

Output:
162345

Explanation:
The digits are rearranged from outer to center as follows 1 2 3 4 5 6 => 1 6 2 3 4 5

n = input().strip()
length = len(n)
result = []
i, j = 0, length - 1
while i <= j:
    result.append(n[i])
    if i != j:
        result.append(n[j])
    i += 1
    j -= 1
print(''.join(result))
#include<stdio.h>
#include<string.h>

int main() {
    char n[10];
    scanf("%s", n);
    int length = strlen(n);
    int i = 0, j = length - 1;
    while(i <= j) {
        printf("%c", n[i]);
        if(i != j) {
            printf("%c", n[j]);
        }
        i++;
        j--;
    }
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    int length = n.length();
    int i = 0, j = length - 1;
    while(i <= j) {
        cout << n[i];
        if(i != j) {
            cout << n[j];
        }
        i++;
        j--;
    }
    return 0;
}
import java.util.Scanner;

public class OuterToCenter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int length = n.length();
        int i = 0, j = length - 1;
        StringBuilder result = new StringBuilder();
        while(i <= j) {
            result.append(n.charAt(i));
            if(i != j) {
                result.append(n.charAt(j));
            }
            i++;
            j--;
        }
        System.out.println(result);
    }
}
Previous Article

Create New Number Removing First and Last Digit

Next Article

Compare Sum of Odd Positioned and Even Positioned Digits

Write a Comment

Leave a Comment

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