Thread: generate number

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Post for loop

    Code:
    #include<stdio.h>
    
    //main function..
    int main(void){
    int i,j,k;
    int n=3;
            for(i=0;i<=3;i++) {
                    j=1;
                    while(j<=n) {
                            printf("\t%d",j);
                            j++;
                    }
                    printf("\n");
                    n=n+3;
    
    
    
            }
    
    }
    the output is:
    1 2 3
    1 2 3 4 5 6
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 10 11 12

    but i want the output as
    1 5
    2 6 9
    3 7 10 12
    4 8 11 13 14 15
    anyone help me using the for loop?..
    Last edited by cheeta; 05-03-2010 at 01:23 AM. Reason: wrong information

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well if you care to explain what you are trying to do, sure, why not?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    thanks claudiu, for ur reply i am actually new to c...i actually need the output as :
    1
    2 5 8
    3 6 9 11 13
    4 7 10 12 14 15

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Okay, so if you had say, 20 numbers what would be the pattern then? I am just a little confused on what you are actually trying to do.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    help me with this 15 numbers...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I can guess what the pattern is, but I agree with claudiu: you need to express this pattern yourself. Do it in words, then it may become easier to translate it to code.

    Just helping you with "this 15 numbers" is not the right approach, since obviously the simple solution for this is to print the sequence as-is, without the use of loops. But to understand how to solve this with loops means understanding the general pattern.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    like,if the number is 20,it has to be like

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

    see,the thing is it has to be with 4 rows alone...

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Your not explaining the pattern... all your doing is showing us a bunch of numbers...

    Explain, using words, what the pattern is, then perhaps we can help.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    hey guys,sorry guys i dono to explain it...but atlast ,i cud do it myself...This is the program :
    Code:
    for(i=1; i<=limit; i++)
    {
        printf("%d", i);
        for(j=1; j<i; j++)
        {
            k=1;
            sub = j * limit;
            while(k <= j)
            {
                sub = sub - k;
                k++;
            }
            printf("    %d", (i+sub));
        }
        printf("\n");
    
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The pattern was very clear, but your code is cryptic. Try to get better names for your variables, whenever you can, or comment your code.

    Another way is to use an int array[rows]cols] and then fill in the elements of it by COLUMNS first, and THEN by the rows incrementing.

    Then print it out the normal way, by rows incrementing first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM