there's a problem on Codeforces.com (it's for beginers) which i know it's solution
but my code can't be accepted because there's a condition that i don't know how to write in C++

here's the problem:
Michael has a problem in his first year of primary school. He can not write numbers.
So his teacher suggested that he write the numbers several times, under one condition: if the teacher says "7", then Ahmed will write this number 7 times.
Write a program that will help Ahmed in doing his task.

Input
The input consists of a single line containing one integer X (1 ≤ X ≤ 10).

Output
Print the number X, repeated X times. The numbers should be separated by one space.

[that's the part that i don't know how to write (1 ≤ X ≤ 10).]

and that's my code

#include <iostream>

    using namespace std;

        int main()
        {
            int x;
            cin >> x;

                for (int i=1;i<=x;i++)

                cout << x ;
            return 0;
        }

1 <= X <= 10 isn't a condition that you're supposed to check in your code. It's just telling you that X will be in that range, so you can take that into account when considering performance or picking data types. It's perfectly okay to ignore that information.

The reason that your code is not accepted is this:

The numbers should be separated by one space.

i tried that too and it didn't work it's still unaccepted

and that's my new code

#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;

        for (int i=1;i<=x;i++)

        cout << x << " ";


    return 0;
}

"separated" means that the space should be between the numbers. Your code also adds one space after the last number, which is probably not allowed.

Use an if to check whether you're in the last iteration (i.e. whether i == x). If you are, don't print a space after the number, otherwise do.

I'm sorry i still just a beginer could you write it as a code like mine
to show me how to do it because i didn't complitly get it

Edited 2 Years Ago by aida.atef.31

This article has been dead for over six months. Start a new discussion instead.