TeachMeSoft

Contoh program C++ Perulangan dalam Perulangan (Mencari Nilai Armstrong)

Contoh program C++ Perulangan dalam Perulangan (Mencari Nilai Armstrong)


 Source code


#include <iostream>
using namespace std;

int main()
{
  int nmr1, nmr2, i, num, digit, ttl;
  cout<<"--------------------------------------------\n";   
  cout<<"-             Angka Armstrong              -\n";
  cout<<"--------------------------------------------\n";
  cout << " Masukan nomor pertama: ";
  cin >> nmr1;
  cout << " Masukan nomor kedua  : ";
  cin >> nmr2;
  cout << " Angka Armstrong antara " << nmr1 << " dan " << nmr2 << " adalah: " << endl;
  cout<<"--------------------------------------------\n";
  for(i = nmr1; i <= nmr2; i++)
  {
        ttl = 0;
        num = i;
        for(; num > 0; num /= 10)
        {
            digit = num % 10;
            ttl = ttl + digit * digit * digit;
        }
        if(ttl == i)
        {
            cout << i << endl;
        }
  }
  cout<<"--------------------------------------------\n";
  return 0;
}


 Output








Contoh program C++ Perulangan, Menghitung SKS, Bobot, Nilai dan IPK

Contoh program C++ Perulangan, Menghitung SKS, Bobot, Nilai dan IPK


 Source code


#include<iostream>
using namespace std;

main(){
    int sks[10],bot[10],d,a, tot=0,totbot=0,totnilai=0,totnilai2=0;  // deklarasi variabel
    float ipk;
    char mk[20][10];// [20] karakter dan 10 data
        cout<<"--------------------------------------"<<endl;
        cout<<"Masukan batas perulangan akhir: ";cin>>a;
        cout<<"--------------------------------------"<<endl;
        for(int i=0; i<a; i++){
            cout<<" 1. Masukan nama MK              : "; cin>>mk[i];
            cout<<" 2. Masukan SKS MK               : "; cin>>sks[i];
            cout<<" 3. Masukan bobot nilai MK (0-4) : "; cin>>bot[i];
            cout<<endl<<endl;
            totbot+=bot[i];
            tot+=sks[i];
            totnilai=sks[i]*bot[i];
            totnilai2+=totnilai;
        }
        ipk=(float)totnilai2/tot;
        cout<<"no. MK / SKS / bobot"<<endl;
        for(int i=0; i<a; i++){
            cout<<i<<". "<<mk[i]<<" / "<<sks[i]<<" / "<<bot[i]<<endl;
        }
        cout<<"Total SKS   : "<<tot<<endl;
        cout<<"Total bobot : "<<totbot<<endl;
        cout<<"IPK         :  "<<ipk<<endl;
       cout<<"Total nilai : "<<totnilai2<<endl;
}


 Output








Contoh program C++ Perulangan, Array, input dan menampilkna total SKS

Contoh program C++ Perulangan, Array, input dan menampilkna total SKS


 Source code

Studi kasus:

  • 2 inputan
  • mata kuliah
  • total sks


 Source code


#include<iostream>
using namespace std;

main(){
    int sks[10],tot=0,d,a;  // deklarasi variabel
    char mk[20][10];// [20] karakter dan 10 data
        cout<<"--------------------------------------"<<endl;
        cout<<"Input Jumlah data: ";cin>>a;
        cout<<"--------------------------------------"<<endl;
        for(int i=0; i<a; i++){
            cout<<"Masukan Mata Kuliah: "; cin>>mk[i];
            cout<<"Masukan SKS        : "; cin>>sks[i];
            tot+=sks[i];
        }
        cout<<"--------------------------------------"<<endl;
        cout<<"Total SKS adalah: "<<tot<<endl;
        cout<<"--------------------------------------"<<endl;
}


 Output









Contoh program C++ Perulangan, Input Nilai awal, nilai akhir dan Menampilkan total perulangan

Contoh program C++ Perulangan, Input Nilai awal, nilai akhir dan Menampilkan total perulangan


  Cara ke-1


Source code ke-1


#include <iostream>

using namespace std;

main(){
    int i, awal, akhir,total=0;
    i=0;
    cout<<"--------------------------------------"<<endl;
    cout<<"-----------PERULANGAN FOR-------------"<<endl;
    cout<<"--------------------------------------"<<endl;
    cout<<"Masukan nilai awal : ";cin>>awal;
    cout<<"Masukan nilai akhir: ";cin>>akhir;
    cout<<"--------------------------------------"<<endl;
    for (i=awal; i<=akhir; i++){
        cout<<"Selamat Datang di UTY"<<endl;
        total=(akhir-awal)+1;
    }
       cout<<"--------------------------------------"<<endl;
       cout<<"Total perulangan adalah "<<total<<endl;
       cout<<"--------------------------------------"<<endl;
}


Output ke-1





 Cara ke-2


Source code ke-2


#include <iostream>

using namespace std;

main(){
    int i, awal, akhir,x=0;
    i=0;
    cout<<"--------------------------------------"<<endl;
    cout<<"-----------PERULANGAN FOR-------------"<<endl;
    cout<<"--------------------------------------"<<endl;
    cout<<"Masukan nilai awal : ";cin>>awal;
    cout<<"Masukan nilai akhir: ";cin>>akhir;
    cout<<"--------------------------------------"<<endl;
    for (i=awal; i<=akhir; i++){
            x++;
        cout<<"Selamat Datang di UTY"<<endl;
    }
       cout<<"--------------------------------------"<<endl;
       cout<<"Total perulangan adalah "<<x<<endl;
       cout<<"--------------------------------------"<<endl;
}


Output ke-2






Contoh program C++ Input Jumlah Perulangan dan menampilkan




 Source code


#include<iostream>
using namespace std;

main(){
    int a[10], b=0, c;
    cout<<"Inpu perulangan: ";cin>>c;
    for(int i=0; i<c; i++){
        cout<<"Masukan nilai: "; cin>>a[i];
    }
    cout<<endl;

    for(int i=0; i<c; i++){
        cout<<"Nilai: "<<a[i]<<endl;
    }
}


 Output









Contoh program C++ Input Jumlah Perulangan, menampilkan dan mengulangi program




 Source code


#include<iostream>
using namespace std;

main(){
    int a[10], b=0, c;  // deklarasi variabel
    char d;
    
    do{
        cout<<"Inpu perulangan: ";cin>>c;
        for(int i=0; i<c; i++){
            cout<<"Masukan nilai: "; cin>>a[i];
        }
        cout<<endl;

        for(int i=0; i<c; i++){
            cout<<"Nilai: "<<a[i]<<endl;
        }
        
    cout<<"Ulangi <y/t>: ";cin>>d;
    }while(d=='y');   
    
}


 Output