C program question
oasf2004_at_yahoo.com
Date: 10/30/04
- Next message: Barry Schwarz: "Re: Linkst List and Pointer Problem"
- Previous message: Brian Riis: "Re: Favorite debuggers"
- Next in thread: Barry Schwarz: "Re: C program question"
- Reply: Barry Schwarz: "Re: C program question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Oct 2004 13:57:47 -0700
Hi All,
One of the exercises of my C book ("C by Dissection" by Kelley and
Pohl) asked me to write a program that reads in an integer value for n
and then sums the integers from n to 2 * n if n is nonnegative, or
from 2 * n to n if n is negative. I think I was able to wrote
successfully the first part of the exercise, but I couldn't write the
second part. Please, see bellow the first part of my code:
#include <stdio.h>
int main()
{
int n;
printf("Input one integer: ");
scanf("%d", &n);
while (n >= 0)
printf("The result is %d\n", n += (2 * n));
return 0;
}
How could I write the second part of my code? I have no idea how could
I write a second while loop in order to get the sums from 2 * n to n,
if n is negative. I have tried:
...
while (n < 0) {
printf("The result is %d\n", n -= (2 * n));
....
but of course I didn't get the correct answer.
Thanks in advance for any hint!
Hoffmann
- Next message: Barry Schwarz: "Re: Linkst List and Pointer Problem"
- Previous message: Brian Riis: "Re: Favorite debuggers"
- Next in thread: Barry Schwarz: "Re: C program question"
- Reply: Barry Schwarz: "Re: C program question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|