Pages

Rabu, 13 April 2011

Program C++ Mencari Nilai Rata-Rata Dengan For(Program C + + Finding Value With Average For)

As we know that in the programming language C + + For statement is used for looping (looping) of one or a number of statements. As an example we can find the average value of the input by the user. Then the program will display the total number and average value of the value entered by the user. Suppose we want to find the average value of 10 values ​​entered by the user, then the C + + program is as follows: 01 # include 02 # include 03 04 void main () 05 { 06 float n, total, rata2; 07 total = 0; 08 for (int i = 0; i <10; i + +) 09 { 10 court <<"Enter the value to" <<(i +1) <> n; 12 total = total + n; 13} 14 rata2 = total / 10; 15 court <<"Total =" <<total <<endl; 16 court <<"rata2 =" <<rata2 <<endl; 17 getch (); 18} Explanation of Program C + + Finding With Average Value For: At first we declare variables, namely n, total and rata2 with type float. Why float? so that later the average value which can be displayed in the form of fractions. Total variable we will use to store our total value of the initialization (give the initial value) with 0. Furthermore we do looping (looping) with For as much as 10 times starting from 0 to 9. In each iteration we ask for input from the user to be stored in the variable n. Then the total value (new) added value of n which had previously been entered by the user. After 10 times iteration (looping), we find the average value by doing the division of total variable is the total divided by 10 and the results are stored in a variable rata2. The final step is to display the total value stored in the variable total and displays the average value stored in the variable average.

0 komentar:

Posting Komentar