Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #65273

Re: nested while loop computing triangular number - issues

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: nested while loop computing triangular number - issues
Date 2015-07-11 15:28 +0100
Organization A noiseless patient Spider
Message-ID <87si8uu5tr.fsf@bsb.me.uk> (permalink)
References <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:

> I have tried to write a program to prompt the user to name 
> triangular number  5 times, compute the number and display 
> the result. 
>
> I would be grateful for your insight on why the program would 
> compute the correct number only once. I am attaching 3 versions 
> (replacing counter++) and results for each one.
>
> VERSION 1
>
> // nested while loop to prompt the user to name 5 ∆numbers
> #include <stdio.h>
>
> int main (void)
> {
>     int n, number, triangular_number, counter;
>     n = 1;
>     counter = 1;
>     triangular_number = 0;

Why not

   int n = 1, triangular_number = 0, counter = 1, number;

or

   int n = 1;
   int triangular_number = 0;
   int counter = 1;
   int number;

?

>     while ( counter <= 5 )
>     {
>         printf ("Please, name your triangular number:  ");
>         scanf  ("%i", &number);

You need to check the result.  Get into the habit of always checking
that input worked.
                 
>         while ( n <= number )
>         {
>            triangular_number += n;
>            ++n;
>         }

The second time round the outer loop, n has the value it had before.
That's not right for this tringular number.

>         ++counter;
>         printf ("The triangular number %i is %i\n", number, triangular_number);        
>      }
>     
>     return 0;
> }

Have you learned about functions yet?

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 06:31 -0700
  Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 06:52 -0700
  Re: nested while loop computing triangular number - issues Siri Cruz <chine.bleu@yahoo.com> - 2015-07-11 07:07 -0700
    Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 07:27 -0700
      Re: nested while loop computing triangular number - issues Siri Cruz <chine.bleu@yahoo.com> - 2015-07-11 08:19 -0700
  Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 15:28 +0100
    Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 08:01 -0700
      Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-11 12:24 -0400
      Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 21:24 +0100
        Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-12 09:35 -0700
          Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-12 11:55 -0700
            Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-12 22:14 +0100
            Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-13 08:05 -0700
    Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-11 10:51 -0700
      Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-11 14:56 -0700
        Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-12 09:32 -0700
          Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-12 13:19 -0400
            Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-13 10:16 -0700
          Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-12 11:48 -0700
            Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-12 15:23 -0400
            Re: nested while loop computing triangular number - issues Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 11:02 -0700
              Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-15 13:19 -0700
                Re: nested while loop computing triangular number - issues Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-20 11:01 -0700
  Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 08:45 -0700

csiph-web