-4

Let suppose a user enters a number below:

54353325421435

i want the variables below to store parts of the number above

Eg.

int part[3]
part[0]=54353
part[1]=32
part[2]=5421435

and then add up the digits and store it the variable like below:

Eg.

int sum[3]
sum[0]=5+4+3+5+3      //sum up part[0]
sum[1]=3+2            //sum up part[1]
sum[2]=5+4+2+1+4+3+5  //sum up part[2]

Sorry guys! I don't know, how to explain this better! I hope you understand my question. Thanks for reading

Ken White
  • 123,280
  • 14
  • 225
  • 444
mike
  • 88
  • 1
  • 9
  • Have you read up on 4loops , also how much is each element suppose to store , and can you show us wat you have tried? – amanuel2 Oct 09 '16 at 04:57
  • @amanuel2 i have no idea how to make this program! i am c++ beginner – mike Oct 09 '16 at 05:00
  • The only way your gonna make a program is be first learning not giving you answers! [This](http://www.learncpp.com/) is a good tutorial.. [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) are awesome guides on c++ books. – amanuel2 Oct 09 '16 at 05:01
  • @amanuel2 yeah, i am leaning! i pretty much know the basics ! anyway thanks for websites! too! – mike Oct 09 '16 at 05:05
  • Why do you guys! Vote down my page without giving to a reason!! So that i can improve next time!!! – mike Oct 09 '16 at 05:09
  • Aravind : what i told you is what you need to improve on. Don't ask questions on SO , that says "How do you do this" .. Show us what you have tried , then we can help you from there onwards. And Learn C++ from good resources i listed . This problem won't be too difficult after then ;) . And most of all have fun learning C++! – amanuel2 Oct 09 '16 at 05:12
  • @amanuel2 thats what! i exactly don't have no clue how to make this program! its not like i dont know anything about c++ ! i know all basics . eg till chapter 12 in your wedsite http://www.learncpp.com/! anyway leave that! how would you start if you r making a program like this?? – mike Oct 09 '16 at 05:18
  • I might give you an answer tomorrow. I will be giving cliff hangers for you to edit my Anwser and solve them. You agree? Since I'm about too sleep now – amanuel2 Oct 09 '16 at 05:28
  • @amanuel2 sure, !! thanks for effort!! i appreciate that!! btw! good night! (try to dreaming to solve it) – mike Oct 09 '16 at 05:31
  • OK night. Tell me how much numberless each array element is suppose to store? – amanuel2 Oct 09 '16 at 05:36
  • @amanuel2 part[0]=54353 //thats is 5 digt, part[1]=32 //thats is 2 digt, part[2]=5421435 //thats is 7 digt , make sure that i can change it in the future too! – mike Oct 09 '16 at 05:49

2 Answers2

0

Problem

You want to divide your integer to 3 different parts. Basically, you have a number 54353325421435, and you want to divide it up into:

part[0]=54353
part[1]=32
part[2]=5421435

Then add them up.

Solution

A for loop will do best. If you don't know what a for loop is, basically it's a means of iteration with a defined starting and ending point. For example, here is a simple iteration that prints out "hello world" 2 times:

for(int i=0; i<2; i++)
   cout << "Hello World" << endl;

You can learn more about for loops here. In your case, what you want to do is iterate through this. So basically, first you store the variable in an integer. (I'm sure you can do that.)

 const unsigned long long NUM = 54353325421435; //Make it a constant to not change it

And then you have an array of parts as you mentioned above:

int part[3]

What you can do now is, loop through the NUM. So let me show you how to do the first one:

int access_digits(const unsigned long long int n, int index)
{
    int digit_array[64] = {0};

    unsigned long long digits = 0, digit, number = n;
    while (number) {
        digit = number % 10;
        number /= 10;
        digit_array[digits] = digit;
        digits++;
    }

    return digit_array[digits - index - 1];
}

 std::string digits;
 for(int i=0; i<=4; i++)
 {
    digits.append(std::to_string(access_digits(NUM,i)));
 }
 int digit_int = std::stoi( digits );

You can see above, first that there is an access_digits function. You can use that function to access digits by index. (Credit goes toward Slayther.) Anyway, after that, you can see I am looping from 0 to 4 to get the first 5 digits for part[0]. The first 5 digits bring 54353.

Now finally you want to add them up. Well, again that's pretty easy. Just loop through the digits, and have an accumulator add them up like so:

  int accum=0;
  for(int i=0; i<4; i++)
  {
    accum += access_digits(digit_int,i);
  }
 

Exercise

Now edit this to include part[1] and part[2] below on the exercise section.

References

Iterating through digits in integer in C

Teenage Territory chat

for loop

std::string

string::append

Glossary

For Loops:

Executes init-statement once, then executes statement and iteration_expression repeatedly until the value of condition becomes false. The test takes place before each iteration.

Syntax

formal syntax:

attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement

informal syntax:

  attr(optional) for ( declaration-or-expression(optional) ; declaration-or-expression(optional) ; expression(optional) ) statement
Community
  • 1
  • 1
amanuel2
  • 4,508
  • 4
  • 36
  • 67
-2

if you want the sum of the digits of no. as you mentioned above then

int number,digit,sum=0;
while(number!=0)
{
 digit=number%10;
 sum=s+digit;
 number=number/10;
}

this code segment will calculate the sum of the digits of the no.

if number=123;
it will calculate sum=3+2+1=6;