Tugas Algoritma 3 (Perulangan)
1. Program Mengkonversi Dari angka ke Romawi
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a;
char angka[20];
printf ("Masukan Angka:");
scanf("%d",&a);
switch(a){
case 1:strcpy(angka,"I\n");break;
case 2:strcpy(angka,"II\n");break;
case 3:strcpy(angka,"III\n");break;
case 4:strcpy(angka,"IV\n");break;
case 5:strcpy(angka,"V\n");break;
case 6:strcpy(angka,"VI\n");break;
case 7:strcpy(angka,"VII\n");break;
case 8:strcpy(angka,"VIII\n");break;
case 9:strcpy(angka,"IX\n");break;
case 10:strcpy(angka,"X\n");
}
printf("Jadi Huruf=%s",angka);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar 1 Tampilannya Program menngkonversi Angka Ke Romawi
2. Program Kalkulator Sederhana
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a,b,c,pilihan;
printf ("masukan nilai a:");
scanf ("%i",&a);
printf ("masukan nilai b:");
scanf ("%i",&b);
printf("MEnu\n");
printf ("1. Pertambahan\n");
printf ("2. Pengurangan\n");
printf("3.Perkalian\n");
printf("4. Pembagian\n");
printf ("Pilih Menu : ");
scanf("%d",&pilihan);
switch (pilihan){
case 1:
c=a+b;
printf("Hasil Penjumlahan= %i\n",c);
break;
case 2:
c=a-b;
printf("Hasil Pengurangan= %i\n",c);
break;
case 3:
c=a*b;
printf ("Hasil Perkalian = %i\n",c);
break;
case 4:
c=a/b;
printf ("Hasil Pembagian= %i\n",c);
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
3. persamaan kuadrat
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a,b,c;
float x1,x2;
printf("Masukkan nilai a : ");
scanf("%i",&a);
printf("Masukkan nilai b : ");
scanf("%i",&b);
printf("Masukkan nilai c : ");
scanf("%i",&c);
printf("\n");
x1 = -b + ((b*b) - (4*a*c)^0,5) / 2*a;
printf("Hasil x1 adalah : %.2f \n",x1);
x2 = -b - ((b*b) - (4*a*c)^0,5) / 2*a;
printf("Hasil x2 adalah : %.2f \n",x2);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
4. Menghitung Nilai Akhir
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int uts, uas, nilai;
printf("1. Masukan nilai uts ");
scanf("%i",&uts);
printf("2. Masukan nilai uas ");
scanf("%i",&uas);
nilai=(uts+uas)/2;
if(nilai > 75)
{
printf("Nilai : A \n ");
}
else if(nilai <= 75 && nilai > 60)
{
printf("Nilai : B \n ");
}
else if(nilai <= 60 && nilai > 50)
{
printf("Nilai : C \n ");
}
else if(nilai <= 50 && nilai > 40)
{
printf("Nilai : D \n ");
}
else if(nilai <= 40)
{
printf("Nilai : E \n ");
}
system("PAUSE");
return EXIT_SUCCESS;
}
Komentar
Posting Komentar