Thread: while loop, triangular number

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    53

    while loop, triangular number

    Simple program, what is in comment section, for loop, question out of book to be made into a "while" loop. First number works after executing program, but never is updated to 2 through 5th entry. Thanks!
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n, number, triangularNumber, counter;
    
        /*
        for(counter = 1; counter <= 5; ++counter)
        {
            printf("What triangular number do you want? ");
            scanf("%i", &number);
    
            triangularNumber = 0;
    
            for( n = 1; n <= number; ++n)
                triangularNumber += n;
    
            printf("Triangular number %i is %i\n\n", number, triangularNumber);
    
        }
        */
    
    
        counter = 1;
        n = 1;
        triangularNumber = 0;
    
        while (counter <= 5)
        {
            printf("What triangular number do you want? ");
            scanf("%i", &number);
            while (n <= number)
            {
                triangularNumber += n;
                n++;
            }
            printf("Triangular number %i is %i\n\n", number, triangularNumber);
            ++counter;
        }
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,036
    You need n = 1; every time you start your while (n <= number) loop.

    Recall that a for loop of the form
    Code:
    for ( a ; b ; c ) {
      d;
    }
    Is (apart from some issues over the semantics of 'continue')
    Code:
    a;
    while ( b ) {
      d;
      c;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    53
    Here's working code, thanks Salem!

    Code:
    // 4_5_5.c
    
    #include <stdio.h>
    
    int main(void)
    {
    	int n, number, triangularNumber, counter;
    
    	/*
    	for(counter = 1; counter <= 5; ++counter)
    	{
    		printf("What triangular number do you want? ");
    		scanf("%i", &number);
    
    		triangularNumber = 0;
    
    		for( n = 1; n <= number; ++n)
    			triangularNumber += n;
    
    		printf("Triangular number %i is %i\n\n", number, triangularNumber);
    
    	}
    	*/
    
    
    	triangularNumber = 0;
    	counter = 1;
    	while (counter <= 5)
    	{
    		n = 1;
    		triangularNumber = 0;
    		counter++;
    		printf("What triangular number do you want? ");
    		scanf("%i", &number);
    		while (n <= number)
    		{
    			triangularNumber += n;
    			++n;
    		}
    
    		printf("Triangular number %i is %i\n\n", number, triangularNumber);
    	}
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heat transfer from triangular fins
    By hemanth342 in forum C Programming
    Replies: 6
    Last Post: 01-30-2018, 10:16 AM
  2. [ask]pyramid number with loop
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-13-2009, 07:57 AM
  3. Triangular number?
    By akaile in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2009, 04:10 AM
  4. triangular collision detection
    By ichijoji in forum Game Programming
    Replies: 1
    Last Post: 03-20-2003, 07:48 PM
  5. newbie- need help with triangular #
    By DirtElk in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 10:14 AM

Tags for this Thread