Hi!!! This is my first time to post here and I have a problem and I want your help.....
This is the case my teacher gave me a question to add 1 to 100 using while loop and its really confusing me... but here is my idea and I jsut want you to help me What I am missing.... Thanks

#include <stdio.h>
#include<conio.h>
#define p printf
main()
{clrscr();
int a,b,c;
while(a>=1)
{
b=a+c
a=b+c
c++;
}
p("\n The Answer is:")
   for(a>=0)
      p("%d",a);
getch();
return 0;
}

Firstly, your variables a, b, and c are uninitialised: their value before entering the loop could be anything.

As to your loop, it doesn't really make sense.

You only need two variables. An index, that gets a value of 1 first time through the loop, 2 the second time, 3 the third time, etc. And a sum, which initially has a value of zero, and each time through the loop has the value of index added to it.

Try using descriptive variable names, and get rid of the "#define p printf" line. If you want to use printf, then type its name in full. Brevity is all well and good, but you've gone too far and have code which is not that easy for a mere mortal to understand, and therefore difficult to get right. You're lucky the code is small.

Oh: learn how to use code tags, so you code is laid out in a readable manner in your posts.

Comments
All excellent tips

>#define p printf
This is an extremely bad idea. If your teacher taught it to you, slap him for me.

>clrscr();
This is a bad idea. Not only is it anti-social (by removing the output of other programs when run in a shared console), it unnecessarily destroys the portability of your code.

>while(a>=1)
First, a doesn't have a predictable value. Second, how are you expecting to count from 1 to 100 with that condition?

>for(a>=0) p("%d",a);
I can't begin to guess what you were thinking here.

Here's a tip, and this is what I personally do when writing a new program, write out a bare bones main function with <stdio.h> and then add to it. Here's the bare bones code that I start with:

#include <stdio.h>

int main ( void )
{
  return 0;
}

Now add the parts you need. For example, you know you want to print the sum of 1 to 100, right? So add the framework for doing that:

#include <stdio.h>

int main ( void )
{
  int sum = 0;

  // Sum 1 to 100

  printf ( "The sum is %d\n", sum );

  return 0;
}

The point of this iterative approach is that you can test at every point. You can verify that your program prints the sum (even if it's always zero) at this point. At the previous point you could verify that the program runs and terminates properly. Now for the loop:

#include <stdio.h>

int main ( void )
{
  int sum = 0;
  int i = 1;

  // Sum 1 to 100
  while ( i < 100 )
    sum += i;

  printf ( "The sum is %d\n", sum );

  return 0;
}

Notice how the loop continues as long as i is less than 100. When you initialize i to a value less than 100, that determines the range that you add to the sum. Setting i to 1 gives you the correct range.

Oops, I forgot to increment i in the loop body, so this program has an infinite loop and runs forever. Good thing we were doing an iterative approach where this bug shows up immediately and before there's too much clutter hiding it. It's easily fixed at this point:

#include <stdio.h>

int main ( void )
{
  int sum = 0;
  int i = 0;

  // Sum 1 to 100
  while ( i < 100 ) {
    sum += i;
    ++i;
  }

  printf ( "The sum is %d\n", sum );

  return 0;
}

Now for the testing. Unless you've taken the time to manually sum that big range, it would be a good idea to do some empirical tests on your code using smaller ranges. For example, break out a calculator and sum 1 to 5, then change the condition to i < 5 and see if the result is the same. Repeat as necessary until you have confidence that the algorithm produces correct results in all reasonable cases.

Now shoo.

Comments
great help for this newbie

Thanks! I appreciate your helpful advises.... I'll try it later in my lab class and sure Narue I'll slap my sir for you heheheh......Thanks....^^

This question has already been answered. Start a new discussion instead.