notes2

2fnn average 33 34 return 0 35 36 37 end function

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: all grades that have been input */ float average; /* average of grades */ /* initialize variables */ total = 0; /* initialize total */ counter = 1; /* initialize loop counter */ /* initialize students */ printf( "\nEnter the total number of students in the class: " ); scanf( "%d", &students ); Ex. Prog. – Counter-controlled repetition 20 /* process while loop */ 21 while ( counter <= students ) { /* set up loop */ 22 printf( "Enter grade %d: ", counter ); /* ask for input */ 23 scanf( "%d", &grade ); /* read grade from user */ 24 total = total + grade; /* add grade to total */ 25 counter = counter + 1; /* increment counter */ 26 } /* end while */ 27 28 /* perform calculations */ 29 average = total / students; 30 31 /* display results */ 32 printf( "The class average is %.2f.\n\n", average ); 33 34 return 0; 35 36 37 } /* end function main */ Program Output % a.out Enter the total number of students in the class: 4 Enter grade 1: 56 Enter grade 2: 68 Enter grade 3: 98 Enter grade 4: 47 The class average is 67.25. % Prog. 2 – Counter-controlled repetition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* example of a while repetition statement*/ #include <stdio.h> int main() { /* convert 10 angles from degrees to radians */...
View Full Document

Ask a homework question - tutors are online