The need:
You have seen in some previous programs we added 'n' numbers using three types of loops. All those program were based on the concept of counter. But generally in real life we don't know how many numbers we'll be adding. The program below illustrates how we enter multiple numbers to get the sum without giving their count before hand. This is the same previous addition program with only difference of 'sentinel' instead of 'counter' with a 'do....while loop'.
The code:
--------------------------------------------
/*Addition program using do...while loop with a sentinel*/
#include<stdio.h>
main()
{
float i,sum=0;
printf("To add numbers keep on giving numbers ...\nPress ENTER after each number...\nEnter 0 after last number to be added\n");
do //do while loop
{
scanf("%f",&i); //scanning number
sum=sum+i; // adding number to sum
}
while(i!=0);
printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------
I have already told you the term 'sentinel' in earliier post. In this post we'll come to know what this is. If you run the given program then we come to know that the program says,
"To add numbers keep on giving number...
Press ENTER after each number...
Enter 0 after last number to be added"
After this the program keeps on accepting numbers until we enter 0 and as you enter 0 the program gives the output. Now is you see the code of this program, you'll come to know that while loop will terminate when the number 0 is given as input. This concept when we use some of the input value to limit the number of runs of loop is called 'use of sentinel' and this value is called 'sentinel'.
You have seen in some previous programs we added 'n' numbers using three types of loops. All those program were based on the concept of counter. But generally in real life we don't know how many numbers we'll be adding. The program below illustrates how we enter multiple numbers to get the sum without giving their count before hand. This is the same previous addition program with only difference of 'sentinel' instead of 'counter' with a 'do....while loop'.
The code:
--------------------------------------------
/*Addition program using do...while loop with a sentinel*/
#include<stdio.h>
main()
{
float i,sum=0;
printf("To add numbers keep on giving numbers ...\nPress ENTER after each number...\nEnter 0 after last number to be added\n");
do //do while loop
{
scanf("%f",&i); //scanning number
sum=sum+i; // adding number to sum
}
while(i!=0);
printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------
I have already told you the term 'sentinel' in earliier post. In this post we'll come to know what this is. If you run the given program then we come to know that the program says,
"To add numbers keep on giving number...
Press ENTER after each number...
Enter 0 after last number to be added"
After this the program keeps on accepting numbers until we enter 0 and as you enter 0 the program gives the output. Now is you see the code of this program, you'll come to know that while loop will terminate when the number 0 is given as input. This concept when we use some of the input value to limit the number of runs of loop is called 'use of sentinel' and this value is called 'sentinel'.
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply