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);
}
--------------------------------------------
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);
}
--------------------------------------------
Hi
ReplyDeleteI 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?
what is the error you are getting?
Delete[Error] C:\Program Files (x86)\C-Free Standard\temp\Untitled1.cpp:15: `a' undeclared (first use this function)
Delete[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 !!
There are 2 errors.
DeleteTo 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
Is the solution to the error is using flushall() or fflush()?
DeleteI am still a little confused.
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