0

Ideally, I would like to take an int of say... 13647 and split it like so:

The first new variable (int) is the first digit. The second is the first two digits. The third is the first three digits... and so on and so on.

I'm having trouble figuring out how exactly to do this while maintaining an efficient runtime. Thank you for your help!

4
  • @lubgr as I understand, OP wants 13647 -> 1, 13, 136, 1364, 13647, which the dupe doesnt answer completely Commented Sep 18, 2018 at 7:21
  • ...well, actually this is easier than what the dupe is asking for, so maybe the dupe is ok Commented Sep 18, 2018 at 7:22
  • take pen and paper, what operations do you need to perform to get 13 from 13647? Commented Sep 18, 2018 at 7:22
  • YES once I figured out the formula and how to modulo it made much more sense. You are right it is actually easier then. Thank you for your help Commented Sep 19, 2018 at 4:36

1 Answer 1

0

You can try the following:

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main() { int i = 0; int num = 13647; char inArr[20]; itoa(num, inArr, 10); int numArr[strlen(inArr)]; for(i = 0; i < strlen(inArr); i++) { numArr[i] = num/pow(10, strlen(inArr) - i - 1); printf("num[%d] = %d\n", i, numArr[i]); } return 0; } 

The output will be:

num[0] = 1 num[1] = 13 num[2] = 136 num[3] = 1364 num[4] = 13647 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you this is definitely more what I am looking for. I have to do quite a few operations to the numbers derived but I am not worried about the code needed to do them so this was very helpful!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.