Sunday 10 April 2011

CONTOH PROGRAM C++ Program berstruktur pengulangan dengan counter

Program berstruktur pengulangan dengan counter: Petunjuk: 1. Gunakan langkah-langkah pembuatan template program seperti pada program sederhana I (“Hello World!) yang lalu , tetapi gunakan Project_name yang berbeda,misalnya Program5 2. Gantilah blok program fungsi main ( ingat,yang dimaksud blok adalah barisan perintah diantara { dan } dari fungsi main),dengan yang berikut: int counter; /* number of grade to be entered next */ int grade; /* grade value */ int total; /* sum of grades input by user */ int average; /* average of grades */ /* initialization phase */ total = 0; /* initialize total */ counter = 1; /* initialize loop counter */ /* processing phase */ while ( counter <= 10 ) { /* loop 10 times */ printf( "Enter grade: " ); /* prompt for input */ scanf( "%d", &grade ); /* read grade from user */ total = total + grade; /* add grade to total */ counter = counter + 1; /* increment counter */ } /* end while */ /* termination phase */ average = total / 10; /* integer division */ printf( "Class average is %d\n", average ); /* display result */ return 0; /* indicate program ended successfully */ Output Program: Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81

0 komentar:

Post a Comment

 
Design by Andhika Yuswanto | Bloggerized by Kunir - JAWA TIMUR | UNIVERSITAS JEMBER