facebook like button

27 March, 2011

program 14: addition of n numbers using 'while loop'

The need:
     You have seen the previous program how we added 'n' numbers using 'for loop'. The program below illustrates the use of 'while loop'. This is the same previous addition program with only difference of for and while loop.

The code: 
--------------------------------------------
/*Addition program using while loop*/
#include<stdio.h>
main()
{
    int m=0,n;
    float i,sum=0;
    printf("how many numbers do you want to add?\n");
    scanf("%d",&n);
    printf("Now keep on giving numbers ...\nPress ENTER after each number...\n");
    while(m<n)      //while loop
    {
      scanf("%f",&i);     //scanning number
      sum=sum+i;         // adding number to sum
      m++;
    }
    printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply