Cetaklah bilangan 1 sampai 4
c++:
#include <iostream>
/* 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 i;
for(i=1;i<=4;i++){
cout<<" "<<i;
}
return 0;
}
raptor:
KASUS 4.2
Cetaklah bilangan ganjil dari 0 sampai 10
c++:
#include <iostream>
/* 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 i;
for(i=0;i<=10;i++)
if(i%2==1)
cout<<i;
else
cout<<" ";
return 0;
}
raptor:
KASUS 4.3
Carilah rata-rata dari n bilangan bulat positif
c++:
#include <iostream>
/* 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 i,n,x,jumlah;
float rerata;
jumlah=0;
cout<<"Banyak Data :";
cin>>n;
for(i=1;i<=n;i++){
cout<<"Masukkan Data ke "<<i<<" :";cin>>x;
jumlah +=x;
}
rerata=(float)jumlah/n;
cout<<"Rata-rata = "<<rerata;
return 0;
}
raptor:
KASUS 4.4
Hitunglah rata-rata dari integer positif(banyak data ditentukan dari data yang dimasukkan)
c++:
#include <iostream>
/* 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) {
float c,hasil,x,b,mhs,a;
x=1;
cout<<"Masukkan jumlah mahasiswa : ";cin>>mhs;
while(!(x>mhs)){
cout<<"Masukkan Nilai UAS : ";cin>>a;
cout<<"Masukkan Nilai UTS : ";cin>>b;
cout<<"Masukkan Nilai Tugas : ";cin>>c;
hasil=(a+b+c)/3;
cout<<"Nilai Rata-rata Mahasiswa ke-"<<x<<" adalah "<<hasil<<endl;
x=x+1;
}
return 0;
}
raptor:
KASUS 4.7
Hitunglah nilai dari xy dengan x bilangan real dan y bilangan bulat
c++:
#include <iostream>
/* 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 a,b;
float x,y;
y = 1;
cout<<"Masukkan angka : ";cin>>x;
cout<<"Masukkan pangkat : ";cin>>b;
cout<<endl;
if (b>=0) {
a = 1;
while (!(a>b)){
y=y*x;
a=a+1;
}}
else{
a=0;
while(!(a<=b)){
y=y*(1/x);
a=a-1;
}}
cout<<"Jadi "<<x<<" pangkat "<<b<<" adalah : "<<y<<endl;
return 0;
}
raptor:
KASUS 4.8
Hitunglah AxB dengan metode penjumlahan
c++:
#include <iostream>
/* 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 i,b,a,hasil;
i = 1;
hasil = 0;
cout<<"Masukkan angka pertama : ";cin>>a;
cout<<"Masukkan angka kedua : ";cin>>b;
while (!(i>(abs(b)))){
hasil = hasil + a;
i =i+1;
}
if (b<0){
cout<<a<<" x "<<b<<" : -"<<hasil<<endl;
}
else{
cout<<a<<" x "<<b<<" : -"<<hasil<<endl;
}
return 0;
}
sekian dan terima kasih, semoga bermanfaat :)
Tidak ada komentar:
Posting Komentar