Kamis, 14 April 2011

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.

Rabu, 13 April 2011

Program C + + To Determine Passed / Not Passed

Ex matter: 1. create a program to determine whether a student is "Passed" or "No Pass" on the basis of Value Theory and Practice Value entered by the user. Terms of students would graduate if the average value of at least 60 and minimum value of Practice 55. Eg = 100 Value Theory, Practice Value = 50 then the result = Disqualified. For example, Value Theory = 40, Value Practice = 90 then the result is Passed. Seeing the above question, then we know that there are two inputs namely Value Theory and Practice Value. Students will be based on two things: the average value and value theory. So first we must find the average value of first is by adding up the value of theory and practice and then divided by two. Program C + + more to answer the questions above are as follows: # Include <iostream> # Include <conio.h>
void main () {clrscr (); float the theory, practice, rata2;
 
court <<"Enter the value of theory:";
 
cin>> theory;
 
court <<"Enter the value of practice:";
  
cin>> practice;
  
rata2 = (theory + practice) / 2;
 
if (rata2> = 60 & & practices> = 55) {
 
court <<"Passed";} else {
 
court <<"No Pass";}
 
getch ();}
Program Explanation: First we create 3 pieces of variables, namely the theory, practice and rata2, the three of us make with the float type data type can store fractional values. This is to anticipate when will the results of the division produces a float value. Furthermore, the program will ask for input from the user to the theory and practice. The next step the program will calculate the value rata2 ie the number of theory and practice are divided in two. Then by using the IF statement, the program will perform testing whether rata2 value of at least 60 and practice at least 55. Remember here in the IF part of the test using the operators AND, so the two expressions must be TRUE.
If the result is correct, it will display "Passed" and vice versa if it is wrong it will display the "No Passing".

Minggu, 10 April 2011

CASE 5.5 WEEK TO-5

Below is the program for the factorial function recursively
search n! ...

The program is as follows:

# include <iostream>
float factorial (float n)
{

if (n> 1)
return n * factorial (n-1);
else
return 1;
}
main ()
{

float fac, n;
court <<"\ t \ tMENCARI factorial \ n";
court <<"input number ="; cin>> n;
court <<"\ nEnter a number: \ n";
cin>> n;
court <<"Faktorialdari" <<n <<"is" <<factorial (n) <<endl;
return 0;
}

The above program consists of only 2 functions:
- factorial (float n)
- main ()

The program above using the user input, ie the values ​​in the fed itself by the user.
In the fourth row (n> 1) a statement of function, check if n is greater than 1. If yes, then the factorial function will memangil itself with the argument n-1. This will be done continuously for n greater than 1. Once n reaches 1, then the condition if (n> 1) This is false and the factorial function returns the value 1.

case of 5.2-5 weeks

case of 5.2-5 weeks
  which determines the largest value of 2 integers.

Maximum function 2 (input a, b: integer): integer
Description
      if (a> b) then return a
      else return b

The program is as follows:

# include <iostream>
  The biggest double (double x, double y)
  {
  double maximum = x;
  if (maximum> y)
      maximum = x;
      return (maximum);
      }
  int main ()
  {
  double a, b;
  a = largest (6.5, 20);
  b = largest (4.5);

  court <<"a =" <<a <<endl;
  court <<"b =" <<b <<endl;
  return (0);
  }


The program above has two argmen of type double and give
the return value of type double form the largest number among the second argument.

Program C++ Untuk Menentukan Lulus/Tidak Lulus


Ex soal :
1.   buatlah program untuk menentukan apakah seorang mahasiswa “Lulus” atau “Tidak Lulus” berdasarkan Nilai Teori dan Nilai Praktek yang diinputkan oleh user. Syarat mahasiswa akan Lulus jika nilai rata-rata minimal 60 dan Nilai Praktek minimal 55.
Misal Nilai Teori = 100, Nilai Praktek = 50 maka hasilnya = Tidak Lulus. Misalnya Nilai Teori = 40, Nilai Praktek = 90 maka hasilnya Lulus.
Melihat soal diatas, berarti kita mengetahui bahwa inputnya ada dua yaitu Nilai Teori dan Nilai Praktek. Mahasiswa akan berdasarkan dua hal yaitu nilai rata-rata dan nilai teori. Jadi sebelumnya kita harus mencari nilai rata-rata terlebih dahulu yaitu dengan menjumlahkan nilai teori dan praktek lalu dibagi dua. Program C++ selengkapnya untuk menjawab pertanyaan diatas adalah sebagai berikut :
#include <iostream.h>
#include <conio.h>

void main(){    clrscr();
float teori, praktek, rata2;
 cout<<"Masukkan nilai Teori : ";
 cin>>teori;
 cout<<"Masukkan nilai Praktek : ";
  cin>>praktek;
  rata2 = (teori + praktek) / 2;
 if(rata2 >= 60 && praktek >= 55)    {
 cout<<"Lulus";    }    else    {
 cout<<"Tidak Lulus";    }
 getch();}

Penjelasan Program :
Pertama-tama kita membuat 3 buah variabel yaitu teori, praktek dan rata2, ketiganya kita buat dengan tipe float yaitu tipe data yang dapat menyimpan nilai pecahan. Hal ini untuk antisipasi jika nanti hasil pembagian menghasilkan nilai pecahan. Selanjutnya program akan meminta inputan dari user untuk teori dan praktek.
Langkah selanjutnya program akan menghitung nilai rata2 yaitu jumlah teori dan praktek dibagi dua. Lalu dengan menggunakan pernyataan IF, program akan melakukan pengujian apakah nilai rata2 minimal 60 dan praktek minimal 55. Ingat disini bagian pengujian pada IF menggunakan operator DAN, jadi kedua ungkapan harus bernilai TRUE.

Jika hasilnya benar, maka akan ditampilkan “Lulus” dan sebaliknya jika salah maka akan ditampilkan “Tidak Lulus”.

Sabtu, 09 April 2011

Contoh Pernyataan For Sederhana Dalam Program C++


Dalam bahasa pemrograman C++ untuk melakukan perulangan (looping) yang paling umum digunakan adalah pernyataan For. Pernyataan For berguna untuk melakukan perulangan (looping) terhadap satu atau sejumlah pernyataan.
Contoh sederhana adalah untuk menampilkan tulisan echa sebanyak 8 kali, seperti contoh program dibawah ini :
01
#include <iostream.h>
02
#include <conio.h>
03

04
void main()
05
{
06
    clrscr();
07
    int i;
08
    for(i=0; i<8; i++)
09
    {
10
        cout<<"echa"<<endl;
11
    }
12
    getch();
13
}
Penjelasan Pernyataan For Sederhana Dalam Program C++ diatas :
Mula-mula kita membuat sebuah variabel baru yaitu i dengan tipe integer. Dalam pernyataan for, i diberi nilai awal yaitu 0. Kemudian dilakukan pengujian apakah i<8, karena hasilnya true maka pernyataan di dalam For dijalankan. Kemudian dilakukan increment terhadap i dalam pernyataan i++. Selanjutnya dilakukan pengujian lagi apakah i<8. Jika iya (true) maka pernyataan di dalam For dijalankan, jika tidak maka program dilanjutkan ke bawah For. Begitu seterusnya hingga pengujian i<8 bernilai false.

Outputnya :