How can i easily produce a sequence of numbers?
I would like to produce a sequence between 1 and 10 in 2's and I'm not sure how to do this.
Can anyone help please?
Thanks in advance
This is a discussion on Sequence of numbers within the C Programming forums, part of the General Programming Boards category; How can i easily produce a sequence of numbers? I would like to produce a sequence between 1 and 10 ...
How can i easily produce a sequence of numbers?
I would like to produce a sequence between 1 and 10 in 2's and I'm not sure how to do this.
Can anyone help please?
Thanks in advance
Use a for loop with an increment of 2.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
The user specifies the length of the sequence (length) ie to 10 and also times, which is how many they want it to go up i.e 2. Sequence is how many values should be in the sequence.
I have so far:
I don't know what to put inside the loop (where not sure is) so I get the right thing out. please help!Code:int length; int times; int sequence; int w; /*user specifies length times ,*/ sequence=length/times; for(w=1;w<=sequence;w++) { NOT SURE +times }
Last edited by emj83; 02-27-2009 at 04:39 AM.
Ok, a couple comments... It seems to me that length and sequence seem to be the same thing in your code...
The user could specify the following for your problem...
1. Starting number
2. Ending Number
3. Number of Numbers in the sequence
4. The incremental value
#2 and #3 are kind of mutually exclusive. In other words if they specify the ending number then specifying the number of numbers in the sequence doesn't make sense (unless you code it so that whichever happens first or last or something).
I'm assuming for the code below, that the user is specifying ONLY the number of numbers in the sequence...
The code would be slightly different if you want the user to specify the ending number in the sequence...Code:int start_num = 0; int curr_num = 0; //Holds the current number int num_count = 0; //Number of numbers in the sequence int increment_num = 0; int x = 0; //Counter // Get user input curr_num = start_num; for (x = 1; x <= num_count; x++) { printf("%d\n", curr_num); // Print each number on it's own line curr_number += increment_num; }
Of course if the user specifies an incremental value that will never count to the end number, then the end number will not be in the sequence, but the loop will stop when the bound is broken...Code:int start_num = 0; int end_num = 0; int increment_num = 0; int x = 0; //Counter // Get user input for (x = start_num; x <= end_num; x += increment_num) { printf("%d\n", x); // Print each number on it's own line }
if the user specifies start_num = 1, end_num = 10, and increment_num = 2 then the sequence output will be 1, 3, 5, 7, 9.