Creation of sequence 1, 12, 123,

  • Thread starter Thread starter PainterGuy
  • Start date Start date
  • Tags Tags
    Creation Sequence
AI Thread 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
72
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top