Jumat, 15 April 2011

Program C++ memindahkan isi dalam variable ke variable lain


Algoritma :
Deskripsi
a,b, t : integer
Deklarasi
t = a
a = b
b = t

Berikut programnya :

#include<iostream.h>
#include<conio.h>

class memindahkan{
friend istream& operator >> (istream&, memindahkan&);
friend ostream& operator << (ostream&, memindahkan&);

public :
memindahkan();
void tukar(){
   t=a ;
   a=b ;
   b=t ;
};

private :
   int a,b;
   int t;
};

memindahkan::memindahkan(){
   cout<<"memindahkan bilangan antar variable"<<endl;
}

istream& operator >> (istream& cin, memindahkan& masukan){
   cout<<"isikan nilai a : ";
   cin>>masukan.a;
   cout<<"isikan nilai b : ";
   cin>>masukan.b;
   cout<<endl;

return cin;
}

ostream& operator << (ostream& out, memindahkan& keluar){
   out<<"hasil setelah di pindah\n"<<endl;
   out<<"nilai a : "<<keluar.a<<endl;
   out<<"nilai b : "<<keluar.b<<endl;

return out;
}

 void main(){
memindahkan nilai;
cin>>nilai;
nilai.tukar();
cout<<nilai;

getch();
}


Explanation:
The program above is a program used to move the contents of the variable to another variable by adding a new variable that is used to store data temporarily. Where variables used are a, b, and t. The process of working this program is to store the original value of a to the t, then the value of b will be loaded into a and b values ​​will be loaded with the value of t that is moving from a value at the beginning of the process. That explanation of this program. Thank you and good luck.

Minggu, 10 April 2011

Program C++ untuk mencari Pembagi terbesar dari dua bilangan integer


Algoritma :
Deklarasi
       a,b,tampung : integer
Deskripsi
       While ( v != 0 )
           Tampung = a % b ;
           a = b ;
           b = tampung ;
       then
              write(a)
       endwhile

#include <iostream.h>
#include <stdio.h>

void fpb(int, int);
main()
{
   fpb(30, 18);
   fpb(45, 15);
   fpb(60, 30);
}

void fpb(int a, int b)
{
   int tampung;
   cout<<"FPB "<< a <<"dan"<< b <<" adalah : "<<endl;

   while(b != 0)
   {
     tampung = a % b;
     a = b;
     b = tampung;
   }
   cout<<"hasilnya :"<< a <<endl;
}

Program class nya :

#include <iostream.h>
#include <conio.h>

class fpb {
friend istream& operator >> (istream&, fpb&);
friend ostream& operator << (ostream&, const fpb&);

public:
fpb();
void hitung_fpbnya(){
    tampung = a % b;
     //a = b;
     //b = tampung;
};

private:
    int a,b;
    int tampung;
};

fpb::fpb(){
cout << "Program mencari FPB \n" << endl;
}
istream& operator >>(istream& cin, fpb& masukan){
   cout << "Masukkan bilangan pertama : ";   cin >> masukan.a;
   cout << "Masukkan bilangan kedua : ";   cin >> masukan.b;
   cout<<endl;

return cin;
}
ostream& operator << (ostream& out, const fpb& keluaran)
{
  out<< " bilangan pertama : " <<keluaran.a<<endl;
  out<< " bilangan kedua : " <<keluaran.b<<endl;
  out<< " FPB "<< keluaran.a << " dan " << keluaran.b <<" adalah : "<<endl;
  out<< " Hasilnya = "<<keluaran.tampung<<endl;

return out;
}

void main(){
  fpb bilangan;
  cin>>bilangan;
  bilangan.hitung_fpbnya();
  cout<<bilangan;

  getch();

}

Explanation:
The program above is used to find the GCF or greatest divisor of two integers. In this program using for loop in which by using the variables a, b and tampung. The process of the program is to initialize a variable that is used after it is put through the loop that has been defined above. And produce output gcd of two integers.

Minggu, 03 April 2011

3 Program C++


Program Memunculkan bilangan ganjil 1 sampai 10

Algoritma :
Deklarasi
       i : integer
Deskripsi
       for i <= 0 to 10 do
           if (i mod 2 =1 )
       then
              write(i)
          endif
       endfor

#include<iostream.h>
#include<conio.h>

int main(){
    int i;
    for (int i=1; i<=10; i++)
    {
      if (i%2==1)
      {
         cout<<i<<" = ganjil"<<endl;
      }
    }
  getch();
  return 0;
}

Program class nya :

#include <cstdlib>
#include <iostream>

using namespace std;

class Ganjil {
public :
        void keluaran();
private :
        int i;   // input
};

void Ganjil::keluaran() {    
         for (int i=1; i<=10; i++)
         {
            if (i%2==1)
            {
             cout<<i<<" = ganjil"<<endl;
            }
         }
}
         
int main(int argc, char *argv[])
{   Ganjil i;
    i.keluaran();
   
    system("PAUSE");
    //return EXIT_SUCCESS;
}

Jeliot :
import jeliot.io.*;



class Ganjil {
public void keluaran(){
       for (int i=1; i<=10; i++)
           {
             System.out.print("ganjil = " +i);
           }
}
       
private int i;         // input


public static void main() {    
    Ganjil i = new Ganjil();
    i.keluaran();
    }
}

Explanation of the program:
In this case I would display the odd numbers from 1 to 10 by using the program C + + is a class in which the later will be imported into jeliot. Friends will be able to see the results of running this program using jeliot. Here I use a for loop and conditioning if.

Teman-teman silahkan mencoba program ini.... Semoga bermanfaat,, J

Program Bilangan habis di bagi 3 dan 5 dari 0 sampai 100

Algoritma :
Deklarasi
       i : integer
Deskripsi
       for i <= 0 to 100 do
           if (i mod 3 = 0 and i mod 5 = 0 )
       then
              write(i)
          endif
       endfor

#include <iostream.h>
#include <conio.h>

int main() {
    int i;
    for (int i=1; i<=100; i++){
        if (i%3==0 && i%5==0)
           cout<<i<<endl;
    }
    getch();
    return 0;
}

Program class :
#include <cstdlib>
#include <iostream>

using namespace std;

class bilangan {
public :
        void keluaran();
private :
        int i;   // input
};

void bilangan::keluaran() {    
         for (int i=1; i<=100; i++)
         {
            if (i%3==0 && i%5==0)
            {
             cout<<i<<endl;
            }
         }
}
        
int main(int argc, char *argv[])
{   bilangan i;
    i.keluaran();
   
    system("PAUSE");
    //return EXIT_SUCCESS;
}

Program jeliot nya :
import jeliot.io.*;



class bilangan {
public void keluaran(){
       for (int i=1; i<=100; i++)
           {
            if(i%3==0 && i%5==0){
             System.out.print(" " +i);
           }
       }
}
       
private int i;         // input


public static void main() {    
    bilangan i = new bilangan();
    i.keluaran();
    }
}

Explanation:
In this program, namely the case where if we want to display the numbers 0 to 100 that runs out at the 3 and 5. Here I also use for loop to solve this case. And inside the loop using a conditioning if to perform a process in which his statement if (i mod 3 = 0 and i mod 5 = 0). Once the process runs its program.

Oya, teman-teman juga bisa melihatnya jeliotnya lhoo....
Thank you...

Mencari nilai minimum dan maksimum dan jumlah semua bilangan positif yang diinputkan.

Algoritma :
Deklarasi
       nilai          : integer
       max,min,jumlah    : integer
       jumlah_inputan     : float
Deskripsi
       Jumlah inputan :
       for <= i to jumlah_inputan do
       then
              write(nilai)
       endfor

       max = nilai
       for <= i to jumlah_inputan do
              if (nilai > max)
       then
              write(nilai)
          endif
       endfor

min = nilai
       for <= i to jumlah_inputan do
              if (nilai < min)
       then
              write(nilai)
          endif
       endfor
      
       jumlah = nilai
       for <= i to jumlah_inputan do
              if ( nilai >=0 )
       then
              write(nilai)
          endif
       endfor

#include <iostream.h>

int main()
{
    int nilai[25];
    float jumlah_inputan;
    int max, min, jumlah;

    cout<< "masukkan jumlah inputan : ";
    cin>> jumlah_inputan;
    for(int i = 0; i < jumlah_inputan; i ++){
    cout<< "masukkan nilai : ";
    cin >> nilai[i];
    cout<< endl;
    }
    max = nilai[0];
    for(int i = 1; i < jumlah_inputan; i++){
            if( nilai[i] > max){
                max = nilai[i];
                }
            }
    min = nilai[0];
    for(int i = 1; i < jumlah_inputan; i++){
            if( nilai[i] < min){
                min = nilai[i];
                }
            }
    jumlah = nilai[0];
    for (int i = 1; i < jumlah_inputan; i++){
           if ( nilai[i] >= 0 ){
                jumlah = jumlah + nilai[i];
                }
            }

    cout<< "nilai maksimum : "<< max <<endl;
    cout<< "nilai minimum  : "<< min <<endl;
    cout<< "jumlah semua bilangan positif : "<<jumlah<<endl;

    cout<< endl;

    return 0;
}

Explanation of the program:

In this case I asked to look for the largest number, smallest and the number of positive numbers are entered. To resolve this case I use for loop and use a conditioning if. In this program we are asked to input numbers we want directly from the keyboard. After that the program will continue in accordance with how many numbers you want entered. After that the program will select the number of the largest and the smallest number and add up all the positive numbers are entered. And the program will provide the appropriate output.

Thank you .... do not forget the try friends ..

Greetings IT ...



Template by:

Free Blog Templates