Jawaban latihan 3.1 - 3.5 pertemuan minggu ke- 3 algoritma dan pemrograman.
Dev C++ :
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main()
{
long diskriminan;
float x2;
int b;
int c;
float x1;
int a;
float s;
cout << "masukkan nilai A = ";
cin >> a;
cout << "masukkan nilai B = ";
cin >> b;
cout << "masukkan nilai C = ";
cin >> c;
diskriminan =(b*b)-(4*a*c);
s=diskriminan;
if (a==0)
{
cout << "bukan persamaan kuadrat" << endl; }
else
{
if (diskriminan>0)
{
x1 =-b+s/2*a;
x2 =-b-s/2*a;
cout << "X1 = "<<x1 << endl;
cout << "X2 = "<<x2 << endl; }
else
{
if (diskriminan==0)
{
x1 =-b/2*a;
x2 =x1;
cout << "X1 = "<<x1 << endl;
cout << "X2 = "<<x2 << endl; }
else
{
cout << "akar imajiner = "<<a<<"+"<<b << endl; }
}
}
return 0;
}
Outputnya :
#include <iostream>
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[])
{
int nilai;
cout<<"masukkan nilai";
cin>>nilai;
if(nilai>=80&&nilai<=100)
cout<<"nilai = A"<<endl;
else if(nilai>=60&&nilai<=80)
cout<<"nilai = B"<<endl;
else if(nilai>=40&&nilai<=60)
cout<<"nilai = C"<<endl;
else if(nilai>=20&&nilai<=40)
cout<<"nilai = D"<<endl;
else if(nilai=0&&nilai<=20)
cout<<"nilai = E"<<endl;
else
cout<<"salah masukkan nilai"<<endl;
return 0;
}
Kasus 3.5
Dev C++ :
#include <iostream>
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[])
{
int hari_ke;
char hari;
cout<<"masukkan hari ke-";cin>>hari_ke;
switch(hari_ke){
case 1:cout<<"hari senin"<<endl;break;
case 2:cout<<"hari selasa"<<endl;break;
case 3:cout<<"hari rabu"<<endl;break;
case 4:cout<<"hari kamis"<<endl;break;
case 5:cout<<"hari jumat"<<endl;break;
case 6:cout<<"hari sabtu"<<endl;break;
case 7:cout<<"hari minggu"<<endl;break;
default:cout<<"salah masukkan"<<endl;break;
}
return 0;
}
Dev C++ :
Kasus 3.2
Dev C++ :
Kasus 3.3
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main()
{
long diskriminan;
float x2;
int b;
int c;
float x1;
int a;
float s;
cout << "masukkan nilai A = ";
cin >> a;
cout << "masukkan nilai B = ";
cin >> b;
cout << "masukkan nilai C = ";
cin >> c;
diskriminan =(b*b)-(4*a*c);
s=diskriminan;
if (a==0)
{
cout << "bukan persamaan kuadrat" << endl; }
else
{
if (diskriminan>0)
{
x1 =-b+s/2*a;
x2 =-b-s/2*a;
cout << "X1 = "<<x1 << endl;
cout << "X2 = "<<x2 << endl; }
else
{
if (diskriminan==0)
{
x1 =-b/2*a;
x2 =x1;
cout << "X1 = "<<x1 << endl;
cout << "X2 = "<<x2 << endl; }
else
{
cout << "akar imajiner = "<<a<<"+"<<b << endl; }
}
}
return 0;
}
Outputnya :
Kasus 3.4
Dev C++ :#include <iostream>
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[])
{
int nilai;
cout<<"masukkan nilai";
cin>>nilai;
if(nilai>=80&&nilai<=100)
cout<<"nilai = A"<<endl;
else if(nilai>=60&&nilai<=80)
cout<<"nilai = B"<<endl;
else if(nilai>=40&&nilai<=60)
cout<<"nilai = C"<<endl;
else if(nilai>=20&&nilai<=40)
cout<<"nilai = D"<<endl;
else if(nilai=0&&nilai<=20)
cout<<"nilai = E"<<endl;
else
cout<<"salah masukkan nilai"<<endl;
return 0;
}
Kasus 3.5
Dev C++ :
#include <iostream>
#include <conio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[])
{
int hari_ke;
char hari;
cout<<"masukkan hari ke-";cin>>hari_ke;
switch(hari_ke){
case 1:cout<<"hari senin"<<endl;break;
case 2:cout<<"hari selasa"<<endl;break;
case 3:cout<<"hari rabu"<<endl;break;
case 4:cout<<"hari kamis"<<endl;break;
case 5:cout<<"hari jumat"<<endl;break;
case 6:cout<<"hari sabtu"<<endl;break;
case 7:cout<<"hari minggu"<<endl;break;
default:cout<<"salah masukkan"<<endl;break;
}
return 0;
}
Comments
Post a Comment