Creation of sequence 1, 12, 123,

  • Thread starter Thread starter PainterGuy
  • Start date Start date
  • Tags Tags
    Creation Sequence
Click For Summary

Discussion Overview

The discussion revolves around creating a program to generate a sequence of numbers where each term is formed by appending the next integer (1, 12, 123, etc.). Participants explore programming approaches to implement this sequence, focusing on the use of loops and variable management.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant suggests using a FOR loop to generate the sequence but expresses uncertainty about their mathematical understanding.
  • Another participant questions the use of the expression i=i+(pow(10,i)) for incrementing the loop, proposing instead to simply increment i by 1.
  • A different participant recommends using an additional variable to hold the intermediate sum and suggests starting the loop at 0 instead of 1, indicating that pow(10,0) equals 1.
  • There is a mention of a simpler method involving multiplying a number by 10 and adding the loop index in each iteration.

Areas of Agreement / Disagreement

Participants provide various suggestions and corrections, but there is no consensus on a single approach to implement the sequence generation. Multiple competing views on how to structure the code remain present.

Contextual Notes

Some participants note the need for additional variables to manage intermediate sums and suggest different starting points for the loop, indicating potential limitations in the initial code provided.

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
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K