output from nested while loop

This is a discussion on output from nested while loop within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to get the following output from a nested while loop: enter=5: 1 22 333 4444 55555 I ...

  1. #1
    Unregistered
    Guest

    Question output from nested while loop

    Hi, I'm trying to get the following output from a nested while loop:
    enter=5:
    1
    22
    333
    4444
    55555
    I can do this fine with a for loop but cannot get it right with a 'while', and i'm not surewhat i'm doing wrong. The code i've got below outputs:
    1
    22
    33
    44
    55
    66...
    Can anybody make a suggestion?? Thankyou

    #include <stdio.h>
    main()
    {
    int rows;
    int num=1;
    int col=1;

    printf("\nEnter a number between 1-9\n"); //get values
    scanf("%d",&rows);

    if (rows > 9||rows < 1)
    {
    printf("\nSorry. Invalid input.\n");
    return; //terminate program
    }

    while (num<= rows){ //outer loop
    printf("%d\n",num);
    num++;

    while (col<= rows) //inner loop
    col++;
    printf("%d",num);


    }//end while loop
    return 0;

    }//end main

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659

    while (col<= rows) //inner loop
    col++;
    printf("%d",num);


    Without brackets, the while loop only affects the incrementing of cols...
    ...should be:

    Code:
    while (col<= rows) //inner loop
     {
      col++;
      printf("%d",num);
     }



    ITSA
    Socket Library!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i, j, val;
      printf ( "Enter a number from 1 - 9: " );
      /*
      ** Always check the return value of user input functions.
      */
      if ( scanf ( "%d", &val ) != 1 || val < 1 || 9 < val ) {
        fprintf ( stderr, "Invalid input\n" );
        exit ( EXIT_FAILURE );
      }
      /*
      ** Outer loop, determine the number of rows and
      ** the value to be printed, after printing,
      ** move to the next line.
      */
      for ( i = 1; i <= val; i++ ) {
        /*
        ** Inner loop, print the value i times.
        */
        for ( j = 0; j < i; j++ )
          printf ( "%d", i );
        printf ( "\n" );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  3. Can I make a nested loop out of this?
    By RpgActioN in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2005, 09:59 PM
  4. Nested Loop problem
    By chead in forum C Programming
    Replies: 7
    Last Post: 01-03-2005, 11:47 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

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