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