Create New Number Removing First and Last Digit

The program must accept an integer N as the input. The task is to form a new number by removing the first and the last digit of N. If N is a single-digit number, print -1.

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

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the new number formed by removing the first and the last digit of N or -1.

Example Input/Output 1:
Input:
12345

Output:
234

Explanation:
After removing the first and last digit from 12345, we get the number 234.

Example Input/Output 2:
Input:
8

Output:
-1

Explanation:
Since 8 is a single-digit number, the output is -1.

n = input().strip()
print(n[1:-1] if len(n) > 1 else -1)
#include<stdio.h>
#include<string.h>

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

int main() {
    string n;
    cin >> n;
    if(n.length() > 1) {
        cout << n.substr(1, n.length() - 2);
    } else {
        cout << "-1";
    }
    return 0;
}
import java.util.Scanner;

public class RemoveEdges {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        if(n.length() > 1) {
            System.out.println(n.substring(1, n.length() - 1));
        } else {
            System.out.println("-1");
        }
    }
}

Leave a Reply

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

More posts. You may also be interested in.