Writing this sequence of numbers in code
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: Writing this sequence of numbers in code

1. Junior Member
Join Date
Nov 2012
Posts
21

Writing this sequence of numbers in code

How can I write the list(or pattern like number) list into a loop or something equal to that in c++?

1=3
2=3
3=3
4=3
5=6
6=6
7=6
8=6
9=9
10=9
11=9
12=9
13=12
14=12
15=12
16=12
17=15

and so on..

extra note: the left column is like the strlen of a string..I want the value on the left side compared to the string length. So if I have a string that is 14 characters long, I will get 12.
Last edited by terryeverlast; May 10th, 2014 at 08:51 AM.

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,734

Re: Writing this sequence of numbers in code

Originally Posted by terryeverlast
How can I write the list(or pattern like number) list into a loop or something equal to that in c++?
What is the pattern involved? For example, at a glance it seems that the numbers on the left hand side of the '=' on each line are the natural numbers, counting from 1. The numbers of the right hand side appear to be multiples of 3, except that each multiple of 3 appears four times. From here, it should be possible to derive a function to make the numbers on the left hand side to the corresponding numbers on the right hand side, upon which this becomes a simple exercise of implementing it.

Originally Posted by terryeverlast
the left column is like the strlen of a string..I want the value on the left side compared to the string length. So if I have a string that is 14 characters long, I will get 12.
Sorry, but I don't follow. I don't see how a string of with a length of 14 corresponds to 12.

3. Re: Writing this sequence of numbers in code

I don't really understand the question re string length, but this will produce the output as shown in post #1
Code:
```#include <iostream>
using namespace std;

const int maxnum = 100;

int main()
{
for (int i = 1; i <= maxnum; ++i)
cout << i << "=" << (((i - 1) / 4) + 1) * 3 << endl;

}```

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•