# Digits – reverse pattern using C++

In this tutorial, we will learn how to reverse the digits of the integer in reverse pattern using C++ program.

The integer is obtained as input. It is put into an array and each digits are reversed.

## How to put integer into array?

The integer is obtained and it is stored in the variable number.

An array is initialized and it starts holding from the last digit of the number.

```long int number;
cin>>number;
long int temp=number;
int a,ind=0;
while(temp!=0){
a[ind]=temp%10;
temp/=10;
ind++;
}```

In the above code,

The array is initialized with the variable name ‘a’ and it starts holding value from the last.

The array’s index position is maintained using the variable ‘ind’.

Pattern to be printed:

The variable number holds the value 74381, the pattern is printed as,

```7
44444444
333
8888
1111111
```

INPUT 1:

`Enter the number: 74381`

### C++ code to print the digits reverse pattern:

```#include<iostream.h>
using namespace std;
int main(){
long int number;
cout<<"Enter the number:"<<"\t";
cin>>number;
long int temp=number;
int a,b,ind=0,index=0;
while(temp!=0){
a[ind]=temp%10;
temp/=10;
ind++;
}
for(int i=ind-1;i>=0;i--){
b[index]=a[i];
index++;
}
cout<<"\n"<<"DIGITS IN THE REVERSE PATTERN :"<<"\n";
for(int i=0;i<ind;i++){
if(a[i]==0){
cout<<"-";
}
else{
for(int j=0;j<a[i];j++){
cout<<b[i];
}
}
cout<<"\n";
}
}```

In the above code, the number is obtained using the variable ‘number’ and it is temporarily stored in another variable also.

The array a is initialised to hold the digits and it is stored using the while loop;

The second array b initialised and it holds the array values in the reverse order.

The index values of both the arrays are maintained using the variable ind=0 and index=0 respectively.

The for loop is initialized with the variable name ‘i’ with initial value as 0 and it is executed until the condition ‘i’ is less than ‘ind’ value.

If statement checks whether any of the digit is 0 if so, then the program prints – (hyphen)

The else statement gets executed and it prints the digits pattern in the reverse order.

If the first digit is 7 and the last digit is 1 then 7 is printed only once.

The second digit is 4 and the last previous digit is 8 then the number 4 is printed 8 times.

The same way the entire digits are printed in the desired pattern.

OUTPUT 1:

```DIGITS IN THE REVERSE PATTERN :
7
44444444
333
8888
1111111```

INPUT 2:

`Enter the number: 5019`

OUTPUT 2:

```DIGITS IN THE REVERSE PATTERN :
555555555
0
-
99999
```

Thus the program prints the digits in the reverse pattern.

We hope this tutorial helped you to understand how to print the digits in the reverse pattern.