Program to delete Nth digit of a Number
Last Updated :
11 Jul, 2025
Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.
Examples:
Input: num = 1234, n = 3
Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134
Input: num = 4516312, n = 2
Output: num_after_deleting_from_starting = 416312, num_after_deleting_from_end = 451632
Approach:
- To delete nth digit from starting:
- Get the number and the nth digit to be deleted.
- Count the number of digits
- Loop number of digits time by counting it with a variable i.
- If the i is equal to (number of digits - n), then skip, else add the ith digit as [ new_number = (new_number * 10) + ith_digit ].
- To delete nth digit from ending:
- Get the number and the nth digit to be deleted.
- Loop number of digits time by counting it with a variable i.
- If the i is equal to (n), then skip, else add the ith digit as [ new_number = (new_number * 10) + ith_digit ].
Implementation:
C++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to delete nth digit
// from starting
int deleteFromStart(int num, int n)
{
// Get the number of digits
int d = log10(num) + 1;
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 0; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == (d - n)) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Function to delete nth digit
// from ending
int deleteFromEnd(int num, int n)
{
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 1; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Driver code
int main()
{
// Get the number
int num = 1234;
cout << "Number: " << num << endl;
// Get the digit number to be deleted
int n = 3;
cout << "Digit to be deleted: " << n << endl;
// Remove the nth digit from starting
cout << "Number after " << n
<< " digit deleted from starting: "
<< deleteFromStart(num, n) << endl;
// Remove the nth digit from ending
cout << "Number after " << n
<< " digit deleted from ending: "
<< deleteFromEnd(num, n) << endl;
return 0;
}
Java
// Java implementation of above approach
class GFG
{
// Function to delete nth digit
// from starting
static int deleteFromStart(int num, int n)
{
// Get the number of digits
int d = (int)Math.log10(num) + 1;
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 0; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == (d - n)) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Function to delete nth digit
// from ending
static int deleteFromEnd(int num, int n)
{
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 1; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Driver code
public static void main(String []args)
{
// Get the number
int num = 1234;
System.out.println("Number: " + num );
// Get the digit number to be deleted
int n = 3;
System.out.println("Digit to be deleted: " + n );
// Remove the nth digit from starting
System.out.println("Number after " + n
+ " digit deleted from starting: "
+ deleteFromStart(num, n));
// Remove the nth digit from ending
System.out.println( "Number after " + n
+ " digit deleted from ending: "
+ deleteFromEnd(num, n));
}
}
// This code is contributed by ihritik
Python3
# Python3 implementation of above approach
# Function to delete nth digit
# from starting
import math;
def deleteFromStart(num, n):
# Get the number of digits
d = (math.log10(num) + 1);
# Declare a variable to form
# the reverse resultant number
rev_new_num = 0;
# Loop with the number
i = 0;
while (num != 0):
digit = num % 10;
num = int(num / 10);
if (i != (int(d) - n)):
rev_new_num = ((rev_new_num * 10) +
digit);
i += 1;
# Declare a variable to form the
# resultant number
new_num = 0;
# Loop with the number
i = 0;
while (rev_new_num != 0):
new_num = ((new_num * 10) +
(rev_new_num % 10));
rev_new_num = int(rev_new_num / 10);
i += 1;
# Return the resultant number
return new_num;
# Function to delete nth digit
# from ending
def deleteFromEnd(num, n):
# Declare a variable to form
# the reverse resultant number
rev_new_num = 0;
# Loop with the number
i = 1;
while (num != 0):
digit = num % 10;
num = int(num / 10);
if (i != n):
rev_new_num = ((rev_new_num * 10) +
digit);
i += 1;
# Declare a variable
# to form the resultant number
new_num = 0;
# Loop with the number
i = 0;
while (rev_new_num != 0):
new_num = ((new_num * 10) +
(rev_new_num % 10));
rev_new_num = int(rev_new_num / 10);
i += 1;
# Return the resultant number
return new_num;
# Driver code
# Get the number
num = 1234;
print("Number:", num);
# Get the digit number to be deleted
n = 3;
print("Digit to be deleted:", n);
# Remove the nth digit from starting
print("Number after", n,
"digit deleted from starting:",
deleteFromStart(num, n));
# Remove the nth digit from ending
print("Number after", n,
"digit deleted from ending:",
deleteFromEnd(num, n));
# This code is contributed by chandan_jnu
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to delete nth digit
// from starting
static int deleteFromStart(int num, int n)
{
// Get the number of digits
int d = (int)Math.Log10(num) + 1;
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 0; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == (d - n)) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Function to delete nth digit
// from ending
static int deleteFromEnd(int num, int n)
{
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 1; num != 0; i++) {
int digit = num % 10;
num = num / 10;
if (i == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Driver code
public static void Main()
{
// Get the number
int num = 1234;
Console.WriteLine("Number: " + num );
// Get the digit number to be deleted
int n = 3;
Console.WriteLine("Digit to be deleted: " + n );
// Remove the nth digit from starting
Console.WriteLine("Number after " + n
+ " digit deleted from starting: "
+ deleteFromStart(num, n));
// Remove the nth digit from ending
Console.WriteLine( "Number after " + n
+ " digit deleted from ending: "
+ deleteFromEnd(num, n));
}
}
// This code is contributed by ihritik
PHP
<?php
//PHP implementation of above approach
// Function to delete nth digit
// from starting
function deleteFromStart($num, $n)
{
// Get the number of digits
$d = (log10($num) + 1);
// Declare a variable
// to form the reverse resultant number
$rev_new_num = 0;
// Loop with the number
for ($i = 0; $num != 0; $i++) {
$digit = $num % 10;
$num = (int)$num / 10;
if ($i == ($d - $n)) {
continue;
}
else {
$rev_new_num = ($rev_new_num * 10) + $digit;
}
}
// Declare a variable
// to form the resultant number
$new_num = 0;
// Loop with the number
for ($i = 0; $rev_new_num != 0; $i++) {
$new_num = ($new_num * 10)
+ ($rev_new_num % 10);
$rev_new_num = (int)$rev_new_num / 10;
}
// Return the resultant number
return $new_num;
}
// Function to delete nth digit
// from ending
function deleteFromEnd($num, $n)
{
// Declare a variable
// to form the reverse resultant number
$rev_new_num = 0;
// Loop with the number
for ($i = 1; $num != 0; $i++) {
$digit = $num % 10;
$num = (int)$num / 10;
if ($i == $n) {
continue;
}
else {
$rev_new_num = ($rev_new_num * 10) + $digit;
}
}
// Declare a variable
// to form the resultant number
$new_num = 0;
// Loop with the number
for ($i = 0; $rev_new_num != 0; $i++) {
$new_num = ($new_num * 10)
+ ($rev_new_num % 10);
$rev_new_num = (int)$rev_new_num / 10;
}
// Return the resultant number
return $new_num;
}
// Driver code
// Get the number
$num = 1234;
echo "Number: " , $num ,"\n";
// Get the digit number to be deleted
$n = 3;
echo "Digit to be deleted: " ,$n ,"\n";
// Remove the nth digit from starting
echo "Number after " , $n,
" digit deleted from starting: ",
deleteFromStart($num, $n),"\n";
// Remove the nth digit from ending
echo "Number after " , $n,
" digit deleted from ending: ",
deleteFromEnd($num, $n) ,"\n";
?>
// This code is contributed by jit_t.
JavaScript
<script>
// Javascript implementation of
// the above approach
// Function to delete nth digit
// from starting
function deleteFromStart(num, n)
{
// Get the number of digits
let d = parseInt(Math.log10(num), 10) + 1;
// Declare a variable
// to form the reverse resultant number
let rev_new_num = 0;
// Loop with the number
for (let i = 0; num != 0; i++) {
let digit = num % 10;
num = parseInt(num / 10, 10);
if (i == (d - n)) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
let new_num = 0;
// Loop with the number
for (let i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = parseInt(rev_new_num / 10, 10);
}
// Return the resultant number
return new_num;
}
// Function to delete nth digit
// from ending
function deleteFromEnd(num, n)
{
// Declare a variable
// to form the reverse resultant number
let rev_new_num = 0;
// Loop with the number
for (let i = 1; num != 0; i++) {
let digit = num % 10;
num = parseInt(num / 10, 10);
if (i == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a variable
// to form the resultant number
let new_num = 0;
// Loop with the number
for (let i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = parseInt(rev_new_num / 10, 10);
}
// Return the resultant number
return new_num;
}
// Get the number
let num = 1234;
document.write("Number: " + num + "</br>");
// Get the digit number to be deleted
let n = 3;
document.write("Digit to be deleted: " + n + "</br>");
// Remove the nth digit from starting
document.write("Number after " + n
+ " digit deleted from starting: "
+ deleteFromStart(num, n) + "</br>");
// Remove the nth digit from ending
document.write( "Number after " + n
+ " digit deleted from ending: "
+ deleteFromEnd(num, n));
</script>
Output: Number: 1234
Digit to be deleted: 3
Number after 3 digit deleted from starting: 124
Number after 3 digit deleted from ending: 134
Complexity Analysis:
Time complexity: O(log(N)).
Auxiliary Space: O(1), since no extra space has been taken.
Another Approach: (To convert number into string)
C++
// C++ implementation to delete nth digit
// from starting with O(logN) time complexity.
#include<bits/stdc++.h>
using namespace std;
// function to delete nth number from starting
static string fromStart(string inp, int del)
{
string inp1 = inp.substr(0, del - 1);
string inp2 = inp.substr(del, inp.length());
return inp1 + inp2;
}
// function to delete nth number from ending
static string fromEnd(string inp, int del)
{
string inp1 = inp.substr(0, inp.length() - del);
string inp2 = inp.substr(inp.length() - del + 1,
inp.length());
return inp1 + inp2;
}
// Driver Code
int main()
{
int in = 1234;
// type cast input number to string
stringstream ss;
ss << in;
string inp = ss.str();
int del = 3;
cout << "num_after_deleting_from_starting "
<< fromStart(inp, del) << endl;
cout << "num_after_deleting_from_ending "
<< fromEnd(inp, del) << endl;
return 0;
}
// This code is contributed by chandan_jnu
Java
// Java implementation to delete nth digit
// from starting with O(logN) time complexity.
public class DeleteN {
public static void main(String args[]) {
int in = 1234;
// type cast input number to string
String inp = Integer.toString(in);
int del = 3;
System.out.println("num_after_deleting_from_starting " + fromStart(inp, del));
System.out.println("num_after_deleting_from_ending " + fromEnd(inp, del));
}
// function to delete nth number from starting
static String fromStart(String inp, int del) {
try {
String inp1 = inp.substring(0, del - 1);
String inp2 = inp.substring(del, inp.length());
return inp1 + inp2;
}
catch (Exception e) {
return "Check Input";
}
}
// function to delete nth number from ending
static String fromEnd(String inp, int del) {
try {
String inp1 = inp.substring(0, inp.length() - del);
String inp2 = inp.substring(inp.length() - del + 1, inp.length());
return inp1 + inp2;
}
catch (Exception e) {
return "Check Input";
}
}
}
Python3
# Python3 implementation to delete nth digit
# from starting with O(logN) time complexity.
# function to del1ete nth number
# from starting
def fromStart(inp, del11):
inp1 = inp[0:del1 - 1];
inp2 = inp[del1:len(inp)];
return inp1 + inp2;
# function to delete nth number
# from ending
def fromEnd(inp, del1):
inp1 = inp[0:len(inp) - del1];
inp2 = inp[len(inp) - del1 + 1:len(inp)];
return inp1 + inp2;
# Driver Code
in1 = 1234;
# type cast input number to string
inp = str(in1);
del1 = 3;
print("num_after_deleting_from_starting",
fromStart(inp, del1));
print("num_after_deleting_from_ending",
fromEnd(inp, del1));
# This code is contributed by chandan_jnu
C#
// C# implementation to delete nth digit
// from starting with O(logN) time complexity.
using System ;
public class DeleteN {
public static void Main() {
int num = 1234;
// type cast input number to string
string inp = Convert.ToString(num) ;
int del = 3;
Console.WriteLine("num_after_deleting_from_starting "
+ fromStart(inp, del));
Console.WriteLine("num_after_deleting_from_ending "
+ fromEnd(inp, del));
}
// function to delete nth number from starting
static String fromStart(string inp, int del) {
try {
string inp1 = inp.Substring(0, del - 1);
string inp2 = inp.Substring(del, inp.Length - del);
return inp1 + inp2;
}
catch (Exception ) {
return "Check Input";
}
}
// function to delete nth number from ending
static String fromEnd(string inp, int del) {
try {
string inp1 = inp.Substring(0, inp.Length - del);
string inp2 = inp.Substring(inp.Length - del + 1, del - 1);
return inp1 + inp2;
}
catch (Exception e) {
Console.WriteLine(e) ;
return "Check Input";
}
}
}
// This code is contributed by Ryuga
PHP
<?php
// PHP implementation to delete nth digit
// from starting with O(logN) time complexity.
// function to delete nth number from starting
function fromStart($inp, $del)
{
$inp1 = substr($inp, 0, $del - 1);
$inp2 = substr($inp, $del, strlen($inp));
return $inp1.$inp2;
}
// function to delete nth number from ending
function fromEnd($inp, $del)
{
$inp1 = substr($inp, 0, strlen($inp) - $del);
$inp2 = substr($inp, strlen($inp) - $del + 1,
strlen($inp));
return $inp1.$inp2;
}
// Driver Code
$in = 1234;
// type cast input number to string
$inp = strval($in);
$del = 3;
print("num_after_deleting_from_starting " .
fromStart($inp, $del) . "\n");
print("num_after_deleting_from_ending " .
fromEnd($inp, $del));
// This code is contributed by chandan_jnu
?>
JavaScript
<script>
// Javascript implementation to delete nth digit
// from starting with O(logN) time complexity.
// function to delete nth number from starting
function fromStart(inp, del) {
let inp1 = inp.substring(0, del - 1);
let inp2 = inp.substring(del, inp.length);
return inp1 + inp2;
}
function fromEnd(inp, del) {
let inp1 = inp.substring(0, inp.length - del);
let inp2 = inp.substring(inp.length - del + 1, inp.length - del + 1 + inp.length);
return inp1 + inp2;
}
let In = 1234;
// type cast input number to string
let inp = In.toString();
let del = 3;
document.write("num_after_deleting_from_starting " + fromStart(inp, del) + "</br>");
document.write("num_after_deleting_from_ending " + fromEnd(inp, del) + "</br>");
// This code is contributed by mukesh07.
</script>
Output:
num_after_deleting_from_starting 124
num_after_deleting_from_ending 134
Time complexity :O(log(N)).
Auxiliary Space: O(log(N)).
C++ Program to Delete Nth Digit of a Number
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem