Algoritma Pemrograman & Elemen Dasar C++

1. Buatlah algoritma untuk mencari luas dan keliling lingkaran
Jawab:
1.Set r=0, phi=22/7 ,  Luas=0 and Keliling=0
2.Input r as jari-jari
3.Calculate Luas = phi * r * r
4.Calculate Keliling = 2 * phi * r
5.The result reguired is the value in Luas and Keliling

2. Buatlah algoritma untuk mencari factor dari suatu bilangan input
Jawab:
1.Set N=0 and the counter k = 1
2.Input N as number
3.Repeat the following step until k>N
a.If N modulus k = 0 then k as the factor
b.increase the value of k by 1
4.the result reguired is the value print by k

3. Buatlah algoritma untuk menghitung rata-rata dari 10 bilangan yang diinputkan
Jawab:
1.Set N1=0, N2=0, N3=0, N4=0, N5=0, N6=0, N7=0, N8=0, N9=0, N10=0 , R=0
2.Input N1, N2, N3, N4, N5, N6, N7, N8, N9, N10 as number
3.Calculate R=(N1+N2+N3+N4+N5+N6+N7+N8+N9+N10)/10
4.The result reguired is the value in R

4. Buatlah algoritma untuk menghitung factorial dari suatu bilangan bulat positif.
N! = 1 . 2 . 3 . 4 . … .N.
Jawab:
1.Set S=0,N=0 and the counter k = 1
2.Input N as number
3.Repeat the following step until k>N
a.Calculate S=S*k
b.increase the value of k by 1
4.the result reguired is the value in S

5. Tulis algoritma untuk menghitung akar persamaan kuadrat. Akar persamaan kuadrat dapat dicari dengan menentukan terlebih dahulu nilai diskriminan dengan rumus D = B2 – 4AC.
– Jika Nilai D < 0 maka merupakan “Akar Imaginer”.
– Jika Nilai  D= 0, maka X1=X2 = –B / (2A).
– Jika Nilai D > 0, maka terdapat dua akar berbeda
X1 = (-B + D) / (2A) dan X2 = (-B – D) / (2A)
Jawab:
1.Set D=0, B=0, A=0, C=0, X1=0 and X2=0
2.Input D, B, A, C as number
3.If D<0 then X1=X2=”Akar Imaginer”
4.If D=0 then calculate X1=X2=-B/(2A)
5.If D>0 then calculate X1 = (-B + D) / (2A) and X2 = (-B – D) / (2A)
6.the result reguired is the value in  X1 and X2

6. Sebuah program C++ memuat deklarasi dan inisialisasi berikut :
int ix = 5, iy = 10;
float fx = 0.001, fy = -17.08;
char ch1 = ‘A’, ch2 = ‘E’;
Tentukanlah hasil evaluasi pernyataan-pernyataan berikut. Untuk setiap soal gunakan kembali nilai yang diberikan pada saat inisialisasi :
a. ch1 = ch2 * ix;

b. ch1 += ch2;

c. fx += (fy *= (iy/ix));

d. iy = (int) (iy / ((int) fy));

e. ch2 == ch1 + ix;

Jawab:
Code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int ix = 5;
int iy = 10;
float fx = 0.001;
float fy = -17.08;
char ch1 = ‘A’;
char ch2 = ‘E’;

cout << ch1 = ch2 * ix << endl;
cout << ch1 += ch2 << endl;
cout << fx += (fy *= (iy/ix)) << endl;
cout << iy = (int) (iy / ((int) fy)) << endl;
cout << ch2 == ch1 + ix << endl;

system(“pause”);
return 0;
}

7. Tulis program untuk menghitung konversi temperatur dari celcius ke fahrenheit, reamur, dan kelvin.
Konversi dari C ke F :  1.8 t  + 32
Konvesi dari C ke R : 0.8 t
Konversi dari C ke K: t + 273

1         2
12345678901234567890123456
1
2
3
4
5
6
Konversi Temperatur

Temperatur asal, C ? 72.8
F =   163.04
R =    58.24
K =   345.80
Jawab:

Code
#include <iostream.>

using namespace std;

int main()
{
float C, R, F, K;

cout << “Konversi Temperatur “;
cout << “\n\nTemperatur asal, C : “;
cin >> C;

R=(0.8)*C;
F=((1.8)*C)+32;
K=C+273;

cout << “F = ” << F << endl;
cout << “R = ” << R << endl;
cout << “K = ” << K << endl;

system(“pause”);
return 0;
}

8. Buatlah program sederhana untuk menghitung akar persamaan kuadrat. Akar persamaan kuadrat dapat dicari dengan menentukan terlebih dahulu nilai diskriminan dengan rumus D = B2 – 4AC.
a. Jika Nilai D < 0 maka merupakan “Akar Imaginer”.
b. Jika Nilai  D= 0, maka X1=X2, yang didapat dengarn rumus –B / (2A).
c. Jika Nilai D > 0, maka terdapat dua akar berbeda yaitu
X1 = (-B + D) / 2A dan X2 = (-B – D ) / 2

Jawab:
Code
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float A, B, C, D;

cout << “Rumus : D = (B*B) – 4AC ” << endl;
cout << “Input A = “;
cin >> A;
cout << “\nInput B = “;
cin >> B;
cout << “\nInput C = “;
cin >> C;
cout << “\nInput D = “;
cin >> D;

if (D<0)
{  cout << “\nX1 dan X2 adalah Akar Imaginer” << endl; }
if (D==0)
{  cout << “\nX1 = X2 = ” << -B / (2*A) << endl; }
if (D>0)
{
cout << “X1 = ” << (-B + sqrt(D)) / 2*A << endl;
cout << “X2 = ” << (-B – sqrt(D)) / 2 << endl;
}

system(“pause”);

return 0;
}