Thread: output from nested while loop

  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,708

    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);
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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, 07: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, 10:59 PM
  4. Nested Loop problem
    By chead in forum C Programming
    Replies: 7
    Last Post: 01-04-2005, 12:47 AM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM