The language i'm trying to do this in is c++ let's say i have a number 123456 , i want to divide this up into partitions of two so i would have: 12,34,and 56. I want to add the partitions up and it would equal 102. How can this be done in c++? Thanks for the feed back :D:D

Recommended Answers

All 3 Replies

You could convert the number into a string, then creating substrings of every two characters, and then parse them back to int and add them together?

Another approach would be using modulus, adding the rests of your value x mod 100, 10000 (then split by 100) and so on.

Good luck to you!
Emil Olofsson

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{

    char myArray10];
    string first;
    string second;
    string third;
    char alpha = 'a';

    itoa(1234,myArray,10);
    cout << myArray;

first.assign(myArray,2);
second.assign(myArray,2,2);

cout << first;
cout << second;





    return 0;




    }

    So how would i convert the strings back into an integer and add them up?

atol(first.c_str())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.