How should i give space between my output numbers while i am storing them in an array ?Like if i ask user to enter numbers and one entered 12345 ,i want to display them as 1 2 3 4 5

Dani AI

Generated

If the goal is to take an input like 12345 and print 1 2 3 4 5, the simplest, safest approach is to read the input as a string and print each character with a space. That preserves leading zeros and avoids integer overflow.

#include <iostream>
#include <string>

int main() {
    std::string s;
    if (!(std::cin >> s)) return 0;
    for (size_t i = 0; i < s.size(); ++i) {
        if (i) std::cout << ' ';
        std::cout << s[i];
    }
    std::cout << '\n';
    return 0;
}

If you need the digits as actual integers (for arithmetic), convert each char to an int and store in a vector or array:

#include <iostream>
#include <vector>
#include <string>

int main() {
    std::string s;
    std::cin >> s;
    std::vector<int> digits;
    digits.reserve(s.size());
    for (char c : s) if (c >= '0' && c <= '9') digits.push_back(c - '0');
    for (size_t i = 0; i < digits.size(); ++i) {
        if (i) std::cout << ' ';
        std::cout << digits[i];
    }
    std::cout << '\n';
}

If you already have an integer value, extract digits with modulo/divide, remembering that extraction produces digits in reverse order (so reverse before printing). Watch out for 0 and negative numbers, and for very large numbers that overflow int/long long.

A quick note on 's snippet: it uses 1-based indices on a fixed a[5] (valid indexes are 0..4), prints i instead of the array contents, and returns a[i] after the loop (undefined). Fix indexing, print the stored values, and return 0 from main. Reading as a std::string is usually the cleanest solution for this exact formatting task.

hope it help.

#include<iostream>
using namespace std;
int main()
{
        int a[5];
        int i;

        for ( i = 1; i < 6; i++ )
            a[i] = i;
        for ( i = 1; i < 6; i++ )
            cout << i << ' ';
        return a[i];
}
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.