• Tidak ada hasil yang ditemukan

Algoritma dan Pemrograman - Soal dan Jawaban Pemrograman C++ [www.alonearea.com].pdf

N/A
N/A
Protected

Academic year: 2021

Membagikan "Algoritma dan Pemrograman - Soal dan Jawaban Pemrograman C++ [www.alonearea.com].pdf"

Copied!
13
0
0

Teks penuh

(1)

A

LGORITMA

P

EMROGRAMAN

10 SOAL DARI TINGKAT 2

1. Buat Pyramid Angka 10 baris, seperti gambar di bawah:

1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr(); int x, y, z, bil=10; for( x = 1; x <= bil; x++ ) { for( z = bil; z >= x; z-- ) cout << ' '; for( z = x; z < 2*x; z++ ) { y = z % 10; cout << y; } for( z = 2*(x-1); z >= x; z-- ) { y = z % 10; cout << y; } cout << endl; } getch(); }

2. Tampilkan bilangan Ganjil dan Genap dari 1 sampai 20?

CODING:

NAMA : Imam Ciptarjo

NPM : 6307130 – 1IT9

MATA KULIAH : Praktek Algoritma & Pemrograman

DOSEN

: Murnawan, S.T.

(2)

A

LGORITMA

P

EMROGRAMAN

========================================================= #include<iostream.h> #include<conio.h> void main() { clrscr(); int angka;

cout << "Angka Genap dari 1-20" << endl;; for (angka=2; angka<=20; angka+=2)

cout << angka << endl;

cout << "\nAngka Ganjil dari 1-20" << endl;; for (angka=1; angka<=20; angka+=2)

cout << angka << endl;

getch();

}

3. Buat program konversi Celcius ke Fahrenheit?

CODING:

========================================================= #include <conio.h> #include <stdio.h> void main() { clrscr();

float celcius, fahrenheit; char lagi='Y';

printf("Program konversi celcius ke fahrenheit \n"); do

{

printf("\nNilai celcius : ");

scanf("%f", &celcius); fahrenheit=1.8*celcius+32;

printf("Nilai fahrenheit : %.2f \n", fahrenheit);

printf("Coba lagi (Y/T) ? "); scanf("%s", &lagi);

}

while (lagi=='Y' || lagi=='y'); getch();

}

4. Buat program menentukan bilangan prima dari n_awal ke n_akhir?

CODING:

========================================================= #include <conio.h> #include <iostream.h> #include <math.h> void prima(); int a,b,c; void main () {

(3)

A

LGORITMA

P

EMROGRAMAN

clrscr ();

cout<<"Masukkan nilai awal : "; cin>>a; cout<<"Masukkan nilai akhir: "; cin>>b; prima ();

getch (); }

void prima () {

cout<<"Bilangan Prima antara "<<a<<"-"<<b<<": "<<endl; for (c=a;c<=b;c++) { if ((c==2)||(c==3)||(c==5)||(c==7)) { cout<<c<<" "; } else if ((c%2!=0)&&(c%3!=0)&&(c%5!=0)&&(c%7!=0)) { cout<<c<<" "; } } }

5. Tampilkan huruf c dengan kolom sebanyak 15 baris sebanyak 10, seperti gambar di bawah?

ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccccccccccccc

CODING:

========================================================= #include <iostream.h> #include <conio.h> main() { clrscr(); int x, y; for (x=1; x<=10; x++) {

for (y=1; y<=15; y++) { cout <<"c"; } cout<<endl; } getch(); }

(4)

A

LGORITMA

P

EMROGRAMAN

Misal: input: w, kolom: 6, baris: 5

wwwwww wwwwww wwwwww wwwwww wwwwww

CODING:

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

int ulang (char hrf, int x, int y); int main()

{

clrscr(); char huruf;

int kolom, baris;

cout << "Menampilkan suatu huruf atau angka sebanyak 'kolom dan 'baris' \n";

cout << "--- \n \n";

cout << "Berapa kolom ? "; cin >> kolom;

cout << "Berapa baris ? "; cin >> baris;

cout << "Masukkan sebuah huruf atau angka : "; cin >> huruf;

cout << endl;

ulang (huruf, kolom, baris); return 0;

getch(); }

int ulang (char hrf, int x, int y) {

int i, j;

for (i=0; i<y; i++) { for (j=0; j<x; j++) cout << hrf; cout << endl; } getch(); }

7. Buat program menentukan bilangan terbesar dan terkecil dari jumlah data yang diinput?

CODING:

========================================================= #include <conio.h> #include <stdio.h> void main() {

(5)

A

LGORITMA

P

EMROGRAMAN

clrscr();

int i, n, max, min, bil;

printf ("Program mencari bilangan terbesar dan terkecil \n"); printf("Banyak data : "); scanf("%d", &n); printf("Masukkan bilangan : "); scanf("%d", &bil); max=bil; min=bil;

for (i=2; i<=n;i++) { printf("Masukkan bilangan : "); scanf("%d", &bil); if (bil>max) max=bil; if (bil<min) min=bil; }

printf ("Data terbesar %d \n", max); printf ("Data terkecil %d \n", min); getch();

}

8. Tampilkan Nilai A, B, C sebagai berikut

Nilai A=a (dari a sampai j) sampai Nilai A=j

Nilai B=5 (perkalian 5 sampai 45) sampai Nilai B=45

Nilai C=2 (perkalian 2 sampai 1024) sampai Nilai C=1024

CODING:

========================================================= #include <iostream.h> #include <conio.h> void main() { clrscr(); char a; int b, c;

for (a='a', b=5, c=2; a<='j'; a++, b=b+5, c=c*2) { cout<<"Nilai A = "<<a<<endl; cout<<"Nilai B = "<<b<<endl; cout<<"Nilai C = "<<c<<endl; cout<<endl; } getch(); }

9. Tampilkan segitiga siku-siku dengan n (input dari 1-20), misal input 4, hasilnya

1 2 4 3 6 9 4 8 12 16

CODING:

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

(6)

A

LGORITMA

P

EMROGRAMAN

main() { clrscr(); int i, j, k;

cout << "Masukkan jumlah bilangan (1-20) : "; cin >> i;

if (i>0 && i<=20) for (j=1; j<=i; j++) { for (k=1; k<=j; k++) { cout<<k*j<<' '; }

cout<<'\n';

} else {cout << "Gimana seeh, udah dibilangin dari 1-20..!!!";} getch();

}

10. Tampilkan segitiga dengan n (input dari 1-20), misal input 4, hasilnya

1 2 4 3 6 9 4 8 12 16 3 6 9 2 4 1

CODING:

========================================================= #include <iostream.h> #include <conio.h> main() { clrscr(); int i, j, k;

cout << "Masukkan jumlah bilangan (1-20) : "; cin >> i;

if (i>=1 && i<=20) { for (j=1; j<=i; j++) { for (k=1; k<=j; k++) { cout<<k*j<<' '; } cout<< endl; } for (j=i-1; j>=1; j--) { for (k=1; k<=j; k++) { cout<<k*j<<' ';

(7)

A

LGORITMA

P

EMROGRAMAN

} cout<< endl; }

} else {cout << "Yang diinput harus dari 1-20";} getch();

}

10 SOAL DARI TINGKAT 3

1. Masukkan kalimat, kemudian tampilkan kalimat tersebut dimulai dari urutan yang paling

belakang (terbalik). Contoh = Hallo -> ollaH

CODING:

========================================================= #include <iostream.h>

#include <conio.h> #include <string.h>

void balik (char *karakter); void main()

{

clrscr();

char kalimat[100];

cout << "\nMasukkan kata : "; cin.getline (kalimat, 100); balik(kalimat);

getch(); }

void balik (char *karakter) { int s; s=strlen(karakter); cout << "Hasilnya : "; for (int n=s; n>=0; n--) { cout << *(karakter+n); } }

2. Buat program jumlah anak ayam diinputkan (n), lalu tampilkan :

Anak ayam turun n, mati satu tinggal n-1 Anak ayam turun 0, mati satu tinggal induknya

CODING:

========================================================= #include <iostream.h> #include <conio.h> main() {

(8)

A

LGORITMA

P

EMROGRAMAN

clrscr(); int x, y;

cout << "Masukkan jumlah anak ayam : "; cin >> x;

for (y=x; y>=1; y--) {

if (y==1) {

cout << "Anak ayam turun " << y << ", mati satu tinggal induknya " <<endl;}

else {

cout << "Anak ayam turun " << y << ", mati satu tinggal " << y-1 <<endl;}

}

getch(); }

3. Tampilkan bilangan Faktorial dari n! dan hasilnya => 3! = 3 x 2 x 1 = 6

CODING:

========================================================= #include <iostream.h> #include <conio.h> main() { clrscr(); int C, BIL; long faktorial = 1;

cout << "Masukkan bilangan yang akan dihitung : "; cin >> BIL; C=BIL; cout << C << "! = "; while (C >= 1) { faktorial *= C; if (C != 1) { cout << C <<" x "; } else { cout << C <<" = "; } C--; } cout << faktorial; getch(); }

4. Buat program untuk menampilkan tahun kabisat dari n_awal hingga n_akhir yang angka

terakhirnya 6

CODING:

========================================================= #include <iostream.h>

(9)

A

LGORITMA

P

EMROGRAMAN

int main() { clrscr(); int a, b, tahun; float i=0;

cout << "Masukkan tahun awal : "; cin >> a;

cout << "Masukkan tahun akhir : "; cin >> b;

cout << "Tahun kabisat yang berakhiran 6 adalah : \n"; for (tahun=a; tahun<=b; tahun++) {

if (tahun%4==0 && ((tahun+4)%20==i) && tahun%100!=0) { cout << tahun << ' '; } else { cout << ""; } } getch(); }

5. Program menampilkan bilangan yang habis dibagi 6 dari n_awal hingga n_akhir

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr(); int a, b, bil;

cout << "Masukkan bilangan awal : "; cin >> a;

cout << "Masukkan bilangan akhir : "; cin >> b;

cout << "\nBilangan yang habis dibagi 6 adalah : \n"; for (bil=a; bil<=b; bil++) {

if (bil%6==0) { cout << bil << ' '; } else { cout << ""; } } getch(); }

6. Animasi angka 0 berjalan dari pojok kiri atas hingga pojok kanan dalam satu baris

CODING:

========================================================= #include <iostream.h>

#include <conio.h> #include <dos.h>

(10)

A

LGORITMA

P

EMROGRAMAN

int main() { clrscr(); int j; for (j=1; j<=80; j++) { cout << "0"; delay(100); } cout<<'\n'; getch(); }

Tampilkan formasi bintang berikut : 7. * * * * * * * * * * * *

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr(); int x, y, z, bil=3; for( x = bil; x >= 1; x-- ) { for( z = x; z <= bil; z++ ) cout << '*'; cout << endl; } for( x = 1; x <= bil; x++ ) { for( z = bil; z >= x; z-- ) cout << ' '; for( z = x; z < 2*x; z++ ) { y = z % 10; cout << '*'; } cout << endl; } getch(); }

(11)

A

LGORITMA

P

EMROGRAMAN

Tampilkan formasi bintang berikut : 8. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr(); int x, y, z, bil=10; for( x = bil; x >= 1; x-- ) { for( z = x; z <= bil; z++ ) cout << ' '; for( z = 2*x; z > x; z-- ) { y = z % 10; cout << "*"; } for( z = x; z <= 2*(x-1); z++ ) { y = z % 10; cout << "*"; } cout << endl; } getch(); }

Tampilkan formasi bintang berikut : 9. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr();

(12)

A

LGORITMA

P

EMROGRAMAN

int x, y, z, bil=10; for( x = bil; x >= 1; x-- ) { for( z = x; z <= bil; z++ ) cout << '*'; for( z = 2*x; z > x; z-- ) { y = z % 10; cout << ' '; } for( z = x; z <= 2*(x-1); z++ ) { y = z % 10; cout << ' '; } for( z = bil; z >= x; z-- ) cout << '*'; cout << endl; } getch(); }

Tampilkan formasi bintang berikut : 10. 0 * * * * * * 0 0 * * * * * 0 0 0 * * * * 0 0 0 0 * * * 0 0 0 0 0 * * 0 0 0 0 0 0 *

CODING:

========================================================= #include <iostream.h> #include <conio.h> int main() { clrscr(); int x, y, z, bil=10; for( x = bil; x >= 1; x-- ) { for( z = x; z <= bil; z++ ) cout << '0'; for( z = 2*x; z > x; z-- ) { y = z % 10; cout << '*'; } cout << endl; } getch(); }

(13)

A

LGORITMA

P

EMROGRAMAN

SOAL DARI BAPAK RUDI FIRMAN

1. Tampilkan deretan dari 1-100, tentukan angka yang mempunyai nilai 9?

CODING:

========================================================= #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); for(int x=0; x<=100; x++) { cout << x << ","; } cout << "\n" << endl; for(int y=1; y<=80;)

{ if (y==0) { y+=1; } else if (y==1) { y+=8; }

else if (y>=1 && y<=100) {

y+=10;

}

cout << "\n Angka 9 diantara 1-100 : " << y; }

for (y=90; y<=99; y++)

cout << "\n Angka 9 diantara 1-100 : " << y; getch();

Referensi

Dokumen terkait