Can't do this program at all

write a program that prints out these following numbers using C++
5 7 10 14 15 20 21 25 28 30

can someone help please ?
Last edited on
You are unable to even write the Hello World program and then replace the text with the numbers?
Look for a pattern. Only the first two numbers are prime. All the others are multiples of them.
thanks chervil
Here's a sample solution, although I'm sure there is a better way, as Chervil said.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    int x=5,y=7;
    //first 7 numbers
    for (int i=0; i<3; i++)
    {
        cout<<x<<" ";
        if (i!=2)//don't output y the 3rd time
            cout<<y<<" ";
        x+=5;
        if (i==2)//output new x and then y instead
            cout<<x<<" "<<y;
        y+=7;
    }   //last 3 numbers
    for (int i=0; i<2; i++)
    {
        x+=5;//add another 5 to x
        cout<<" "<<x<<" ";//output x
        if (i==0)
            cout<<y;//output last y
    }//the second time 5 is added to x and 30 is outputted
    return 0;//terminate program
}
5 7 10 14 15 20 21 25 28 30
Last edited on
hanahyasser wrote:
write a program that prints out these following numbers using C++
5 7 10 14 15 20 21 25 28 30
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "5 7 10 14 15 20 21 25 28 30" << std::endl;
}
I was trying to come up with a relatively short solution that doesn't contain any numbers whatsoever (i.e. it doesn't contain any of the characters 0123456789), but I gave up because it was getting to be too long to fit completely on one line of code (sans the #include ) and within the boundaries of the post.

So instead, I'll just offer this:
1
2
#include <iostream>
int main(){for(int i=1;i<6;++i)std::cout<<(i>3?7*i-7:5*i)<<' '<<(i<3?7*i:5*i+5)<<' ';}
The OP never asked for the program to be obfuscated, modular/extensible, dynamic, etc. so I have no idea why you are all suggesting such complex solutions.
I suppose that is true, although it was assumed it was based on a pattern. Otherwise, your example above is simple enough.
Did you ever bought some books or reading any tutorials to learn C++?
Topic archived. No new replies allowed.