Sabtu, 09 April 2011

Tugas Kelompok Alpro

   1. #include <iostream>
   2.
   3. 
   4.
   5. using namespace std;
   6.
   7. 
   8.
   9. int prime(int x) {
  10.
  11. int a;
  12.
  13. 
  14.
  15. for(a=2;a<=x;a++) {
  16.
  17. if(x%a==0) {
  18.
  19. cout << a << " * ";
  20.
  21. x/=a;
  22.
  23. a--;
  24.
  25. }
  26.
  27. }
  28.
  29. return 0;
  30.
  31. }
  32.
  33. 
  34.
  35. main() {
  36.
  37. int x;
  38.
  39. cin.clear();
  40.
  41. cout << "\nInput an integer\n";
  42.
  43. cin >> x;
  44.
  45. prime(x);
  46.
  47. system("PAUSE");
  48.
  49. 
  50.
  51. }




...

    * 3.2 kpk;



   1. #include <iostream.h>
   2.
   3. #include <conio.h>
   4.
   5. 
   6.
   7. class kpk {
   8.
   9. friend istream& operator>>(istream&, kpk&);
  10.
  11. public :
  12.
  13. kpk ();
  14.
  15. private:
  16.
  17. int a,b,nilai;
  18.
  19. };
  20.
  21. 
  22.
  23. kpk::kpk() { cout<<"Menghitung KPK"<<endl; }
  24.
  25. 
  26.
  27. istream& operator>>(istream& in, kpk& masuk) {
  28.
  29. masuk.nilai=0;
  30.
  31. cout<<"Masukkan A: "; in>>masuk.a;
  32.
  33. cout<<"Masukkan B: "; in>>masuk.b;
  34.
  35. for (int z=1; z<=masuk.b; z++) {
  36.
  37. masuk.nilai=masuk.nilai+masuk.a;
  38.
  39. if (masuk.nilai%masuk.b==0) {
  40.
  41. cout<<"KPK dari "<< masuk.a << " dan "<< masuk.b <<" adalah "<< masuk.nilai <<endl;
  42.
  43. return in;
  44.
  45. }
  46.
  47. }
  48.
  49. }
  50.
  51. 
  52.
  53. main () {
  54.
  55. kpk X;
  56.
  57. cin >> X;
  58.
  59. getch ();
  60.
  61. }







    * 3.2 GCD;



   1. #include <cstdlib>
   2.
   3. #include <iostream>
   4.
   5. using namespace std;
   6.
   7. 
   8.
   9. class hitung_Gcd {
  10.
  11. public :
  12.
  13. int gcd_iteratif(int c, int d);
  14.
  15. int gcd(int c, int d);
  16.
  17. private:
  18.
  19. int bil1, bil2;
  20.
  21. int hasil;
  22.
  23. };
  24.
  25. 
  26.
  27. int hitung_Gcd::gcd_iteratif(int c, int d)
  28.
  29. { int r;
  30.
  31. while (d > 0) {
  32.
  33. r = c % d;
  34.
  35. c = d;
  36.
  37. d = r;
  38.
  39. }
  40.
  41. return (c);
  42.
  43. }
  44.
  45. 
  46.
  47. int hitung_Gcd::gcd(int c, int d)
  48.
  49. {
  50.
  51. if (d==0) return(c);
  52.
  53. if (c<d) return(gcd(d,c));
  54.
  55. return(gcd(c-d, d));
  56.
  57. }
  58.
  59. 
  60.
  61. int main(int argc, char *argv[])
  62.
  63. {
  64.
  65. hitung_Gcd X;
  66.
  67. int a = 20, b = 10;
  68.
  69. cout << "Gcd (iteratif) dari : " << X.gcd_iteratif(a,b) << endl;
  70.
  71. cout << "Gcd (rekursi) dari : " << X.gcd(a,b) << endl;
  72.
  73. system("PAUSE");
  74.
  75. return EXIT_SUCCESS;
  76.
  77. }





    * 3.5 bilnangan prima tanpa clss



   1. #include <cstdlib>
   2.
   3. #include <iostream>
   4.
   5. 
   6.
   7. using namespace std;
   8.
   9. 
  10.
  11. int main(int argc, char *argv[])
  12.
  13. {
  14.
  15. long unsigned int nUserInput;
  16.
  17. cin >> nUserInput;
  18.
  19. for(long unsigned int nIterator = 2; nIterator <= nUserInput; nIterator++)
  20.
  21. {
  22.
  23. if((nUserInput % nIterator) == 0)
  24.
  25. {
  26.
  27. cout << nIterator;
  28.
  29. nUserInput /= nIterator;
  30.
  31. nIterator = 1;
  32.
  33. if(nIterator != nUserInput) {
  34.
  35. cout << " * ";
  36.
  37. }
  38.
  39. }
  40.
  41. }
  42.
  43. system("PAUSE");
  44.
  45. return EXIT_SUCCESS;
  46.
  47. }

Lihat Selengkapnya

Tidak ada komentar:

Posting Komentar