Switch to full style
C++ code examples
Post a reply

C++ While loop

Thu Nov 13, 2008 1:55 pm

While loop example in C++
cpp code
#include <stdio.h>
main()
{
int cnt = 0, /* Count of numbers read. */
tot = 0; /* Total of numbers read. */

/* Read until a read fails. */
int num;
while(scanf("%d", &num) == 1) {
tot = tot + num;
cnt = cnt + 1;
}

/* Print the average. */
if(cnt)
printf("Average is %g\n", (double)tot / (double)cnt);
else
printf("No numbers.\n");
}




Post a reply
  Related Posts  to : C++ While loop
 do while loop and for loop C++ example help.     -  
 Do while loop     -  
 While loop     -  
 Breaking a Loop     -  
 PIC Assembly For Loop Example     -  
 I NEED SOME HELP IN THE FOR LOOP PLZ .. IAM JUST A BEGINNER     -  
 Using include() Within a Loop     -  
 Why will my loop does not work     -  
 Need help with code for a while loop program     -  
 for statement loop indefinitely     -  

Topic Tags

C++ Loops