C++ Program to remove only the tenth digit of the number

In this tutorial, we will learn how to remove only the tenth digit of a number in C++.

This program removes only the tenth digit of the integer.

How to put an integer into an array?

int number;
cin>>number;
int temp_hold=number;
int array[100],count=0;
while(temp_hold!=0){
array[count]=temp_hold%10;
count++;
temp_hold/=10;
}

In the above code, the integer is assigned to the variable name number.

  • The array[100] is declared to hold each values of the integer. The variable ‘count’ initialised with 0 monitors the index position.
  • The temp_hold is a variable holding the integer number.
  • While loop works until the temp_hold is equal to zero.
  • array[0] now holds last digit of the integer , temp_val %10 gets only the last digit.
  • The count is now incremented and the array[0] becomes array[1] and so on.
  • temp_hold=temp_hold/10 operation is performed to avoid infinite loop to occur.
  • The while loop executes until all the digits are stored into the array.

How to find the tenth digit if the integer is a single digit?

This problem is also solved in this program. Here we are maintaining a count variable to put the digit into the array.

The count is checked whether it is greater than 1 if not then the value is directly printed denoting , there is no tenth digit in the integer.

INPUT 1:



Enter the number:

1234

C++ code to remove only the tenth digit of a number

#include<iostream.h>
using namespace std;
int main(){
    cout<<"Enter the number:"<<"\n";
    int number;
    cin>>number;
    int temp_hold=number;
    int array1[100],array2[100],index=0,count=0,ind=0;
    int temp_var=number;
//To find the number of digits in the number//
      while(temp_hold!=0){
         temp_hold/=10;
         count++;
      }
     if(count>1){
   cout<<"The number after removing only the tenth digit:"<<"\t";
        while(temp_var!=0){
          array1[index]=temp_var%10;
          index++;
          if(index==1){
          temp_var/=10;
          }
          temp_var/=10;
        }
           if(index>=1){
              for(int i=index-1;i>=0;i--){
                  array2[ind]=array1[i];
                  ind++;
              }
           }
           for(int i=0;i<ind;i++){
              cout<<array2[i];
           }
     }
     else{
        cout<<"The number does not have tenth digit.";
        }
}

In the above code,

  • The index value is incremented every time and the digits are stored into the array simultaneously.
  • The count if it is greater than 1 then it is sure that the number is having tenth digit which can be removed.
  • The while loop is executed until temp_var is equal 0.
  • The array1[index] stores from the last digit and simultaneously if the index value is equal to 1 the temp_var is divided by 10 to avoid infinite loop.
  • The array1[] contains the digits in the reverse order.
  • The array2[ind] is declared with ind=0 as index position.
  • The for loop is initialised from index1-1 and the condition ends if it is equal to 0 and decremented.
  • The  array2[] starts holding the values of array1[] in the reverse order until the for loop is terminated.
  • The array2 now contains the number without the tenth digit and it is printed.
  • If the count of digits in the number is 1 , the else segment is executed which prints the number.

OUTPUT 1:

The number after removing only the tenth digit :  124

INPUT 2 :

Enter the number : 3

OUTPUT 2:

The number does not have tenth digit.

This program removes only the tenth digit of the number.

We hope this tutorial helped you to understand how to remove only the tenth digit of the number.

You may also read,

Leave a Reply

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