The program is pretty simple. The basic aim of the program is to take a number as input from the user and then store the individual digits of the number in an array. For this my approach is to first convert the input integer number into a string. Then we will iterated through every character of the string and covert the character into a digit and store it in the array. First I take the input of the length of the number. Then I take input of the number. Then I convert that integer number into string and then iterate through every position of the string and store the subsequent digit in the array. code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int m;
cin>>m;
string s = to_string(m);
int arr[n];
int j=0;
for(auto d:s){
arr[j] = d-'0';
j+=1;
}
for(int i=0;i<n;i++) cout<<arr[i];
}
The code works absolutely fine for all numbers which do not begin with '0'. But as soon as I give an input integer like '0135' i get strange array elements like '13532764'. Please help me in finding the problem.