Creation of sequence 1, 12, 123,

  • Thread starter Thread starter PainterGuy
  • Start date Start date
  • Tags Tags
    Creation Sequence
Click For Summary
The discussion revolves around creating a program to generate a sequence of numbers where each term builds upon the previous one, specifically displaying terms like 1, 12, 123, and so on, based on user input for the number of terms. The original code provided was incorrect due to the use of an inappropriate increment in the FOR loop, specifically using `i = i + (pow(10, i))`, which was not suitable for this task. Participants suggested using a simple increment (`i++`) and implementing a nested loop structure: an outer loop to iterate through the number of terms and an inner loop to construct each term. They also recommended starting the loop at 0 and using additional variables to hold intermediate sums. Ultimately, the problem was resolved, and the user expressed gratitude for the assistance received.
PainterGuy
Messages
938
Reaction score
73
Hello everyone, :smile:

I was working to create a program to list the sequence:
1
12
123
1234
12345
...

The user will decides how many terms of sequence are displayed. For example the terms, n, given above are five. Will you help me please? I think I need to use the FOR loop here. Given below are some random steps I put down. My math is weak.

second term - first term = 11
third term - second term = 111
fourth term - third term = 1111
...

The code written below is wrong. I have included it so that you can help.:wink:

Code:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()

{
 	
 	int n, i;
 	
 	cout << "How many terms are required?: ";
 	cin >> n;
 	
 	cout << " " << endl;
 	
 	for ( i=1; i<=n; i =i+(pow(10,i)))
 	
 	{
	 	
	 	cout << i << endl;
	 	
    }
		
}

Cheers
 
Technology news on Phys.org
PainterGuy said:
Hello everyone, :smile:

I was working to create a program to list the sequence:
1
12
123
1234
12345
...

The user will decides how many terms of sequence are displayed. For example the terms, n, given above are five. Will you help me please? I think I need to use the FOR loop here. Given below are some random steps I put down. My math is weak.

second term - first term = 11
third term - second term = 111
fourth term - third term = 1111
...

The code written below is wrong. I have included it so that you can help.:wink:

Code:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()

{
 	
 	int n, i;
 	
 	cout << "How many terms are required?: ";
 	cin >> n;
 	
 	cout << " " << endl;
 	
 	for ( i=1; i<=n; i =i+(pow(10,i)))
 	
 	{
	 	
	 	cout << i << endl;
	 	
    }
		
}

Cheers

Hey painterguy. Just a question: Why do you have i=i + (pow(10,i))? If you want to print the terms 1234... just increment that loop by 1 (ie i=i+1 or i++).

Also if you want to do that repeat like you have done like print

1
12
123
1234

for n = 4 (or any n) you need another loop. So in your outer loop you will go from 1 to n (call it x) and in your inner loop you will go from 1 to x.

Just out curiosity so I know your thought process, why did you use the pow statement? I'm not criticizing, I'm just curious about your thought process.
 
Your method will work, but you need another variable to hold the intermediate sum (1, 11, 111, 1111, ...), and yet another to hold the total sum, and only use i as an incremental number. Also you need to start at 0, not 1 (pow(10,0) == 1), so that would be for(i=0; i<n; i++) ... You could use j to hold the intermediate sum, and k to hold the total sum.

Note, a simpler alternative method would be to repeatedly multiply a number by 10 and add i in each loop.
 
Last edited:
Hello chiro, rcgldr,

I am very much thankful to you both for your replies. The problem was solved but I have been little lazy in extending my thanks. :) Actually I was away.

Cheers
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
1K
Replies
10
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K