Kamis, 14 April 2011

Looking for the Biggest or Smallest Numbers


One simple algorithm problem is finding the largest or smallest numbers, where numbers are entered by the user. For example, look for the largest or smallest numbers among the 3 pieces of number of input by the user. We can answer these questions and look for the largest or smallest numbers using the service conditions are available in C + + programming language.
But generally to seek the largest or smallest numbers like this, the programmer will use the IF statement because it is easier to understand and more flexible. For more details, please refer to C + + program below to find the largest numbers:

# Include <iostream>
# Include <conio.h>

void main ()
{
   court <<"Program Seeking Numbers Greatest" <<endl;
   int bil1, bil2, bil3, the largest;
   court <<"Enter number 1:";
   cin>> bil1;
   court <<"Enter number 2:";
   cin>> bil2;
   court <<"Enter number 3:";
   cin>> bil3;
   if (bil1> bil2)
      largest = bil1;
   else
      largest = bil2;
   if (bil3> biggest)
      largest = bil3;
   court <<"Biggest number =" <<largest;
   getch ();
}



Explanation of the program to find the largest numbers:
Initially the program will prompt the user to enter three numbers that bil1, bil2 and bil3. Then the program will compare between bil1 and bil2, if it turns out bil1 larger, it is considered the biggest stuffed with bil1 value, if otherwise then terbesardiisi with bil2. Next live compare between bil3 with the largest, if turns out bil3 bigger than the biggest, the greatest value is replaced again with bil3 value.
In the end the largest will be filled with the largest number among the three numbers entered by the user. As an exercise, please modify the above program for a program to find the largest number among the 4 fruit number that entered by the user. Good luck, if you do not succeed and the curious way, please contact me at wirautama06 [at] yahoo.com.

Showing Value Letter With IF

This time we will try to program C + + to show the value of letters of numbers entered by the user. So for example user input 85 numbers then it will display the letter A. Problem selengkapkanya are as follows:

By using the IF statement, create a program to display the letter from the value entered by the user, with sebegai following provisions:
If the value is greater than 80, then the value of letter = A
If the value is greater than 75, then the value of letter = B
If a value greater than 65, then the value of letter = C
If the value is greater than 45, then the value of letter = D
If the value is less than or equal to 45, then the value of letter = E
If the input value is greater than 100 or less than 0, it will display the message "wrong input".
To make the program C + + displays the value of letters as a matter of the above, we can use the IF statement. The following source code C + + program to display the value of these letters:


01  # include <iostream>
02  # include <conio.h>
03
04  void main ()
05  {
06  court <<"Program Point Value" <<endl;
07  int bil;
08  court <<"Enter the value number:";
09  cin>> bil;
10  if (bil> 100 | | bil <0)
11  court <<"Input wrong";
12  else if (bil> 80)
13  court <<"The value of letter = A";
14  else if (bil> 75)
15  court <<"The value of letter = B";
16  else if (bil> 65)
17  court <<"The value of letter = C";
18  else if (bil> 45)
19  court <<"The value of letter = D";
20  else
21  court <<"The value of letter = E";
22  getch ();
23  }

Explanation of the program displays the value of the letter:
In accordance with the provisions in question, then we must ensure that if the input is above 100 or below 0 will display the message "Input wrong", therefore first tested whether the input above 100 or below 0. Further testing of the new input values ​​to
determine the value of the letters according to the provisions on the matter.

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 <iostream>
02  # include <conio.h>

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) <<":";

11  cin>> 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 rata2.