facebook like button

27 March, 2011

program 15: addition of n numbers using 'do......while loop'

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

The code: 
--------------------------------------------
/*Addition program using do...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");
    do      //do while loop
    {
      scanf("%f",&i);     //scanning number
      sum=sum+i;         // adding number to sum
      m++;
    }
    while(m<n);
    printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------

6 comments:

  1. Hi
    I have tried doing this program in a different way but i get an error.
    Please help me.Here is the code.

    #include
    int main()
    {
    int sum=0;
    do
    {
    int n;
    printf("enter the number \n");
    scanf("%d",&n);
    sum=sum+n;
    char a;
    printf("do you wish to continue Y/N \n ");
    scanf("%c",&a);
    }
    while (a=='Y');
    printf("sum of numbers you have entered=%d \n",sum);
    return 0;

    }

    What is the mistake I have committed?

    ReplyDelete
    Replies
    1. what is the error you are getting?

      Delete
    2. [Error] C:\Program Files (x86)\C-Free Standard\temp\Untitled1.cpp:15: `a' undeclared (first use this function)
      [Error] C:\Program Files (x86)\C-Free Standard\temp\Untitled1.cpp:15: (Each undeclared identifier is reported only once
      [Error] C:\Program Files (x86)\C-Free Standard\temp\Untitled1.cpp:15: for each function it appears in.)

      this is the error it shows !!

      Delete
    3. There are 2 errors.
      To avoid first error remove 'char a' form 11th line and put it after line 'int sum=0;'. Now your program will compile successfully.

      Second error you'll get when you run the program. when you find the error write about that on my facebook wall: www.facebook.com/programsimply

      This is the cause of first error:
      1. You should understand concept of 'context'. The context of a variable is the part of the program in which the variable is accessible/(visible while the program is running).
      2. now in 15 line you are trying to access variable a, you get an error because a is not in the context so not visible at line 15.
      to know more about this write on
      www.facebook.com/programsimply

      Delete
    4. Is the solution to the error is using flushall() or fflush()?
      I am still a little confused.

      Delete
    5. yes you can use fflush(stdin); just before scanf("%c",&a); or you can put a getchar(); instead of fflush(stdin); in this case because you need to avoid/flush only one character(which is the new line character after the number you entered for adding).

      Delete

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