0% found this document useful (0 votes)
15 views

For Looping

The document discusses different looping structures in C++ including for, while, do-while loops. It provides examples of each loop type calculating the factorial of a number n. It also demonstrates the continue, break, and goto statements. Finally, it shows a Fibonacci sequence example using a for loop.

Uploaded by

Yoga Tri Warmen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

For Looping

The document discusses different looping structures in C++ including for, while, do-while loops. It provides examples of each loop type calculating the factorial of a number n. It also demonstrates the continue, break, and goto statements. Finally, it shows a Fibonacci sequence example using a for loop.

Uploaded by

Yoga Tri Warmen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

For Looping #include <iostream> using namespace std;

int main () { int n; double nfak;

cout<<"input the value of n = "; cin>>n;

cout<<"n! = "<<n<<"! = "; nfak=1; for(n;n>=1;n--) { nfak*=n; if(n!=1) {cout<<n<<"x";}

else {cout<<n<<" = ";} }

cout<< nfak << endl;

system ("PAUSE"); }

#include <iostream> using namespace std;

int main () { int n,i; double nfak;

cout<<"input the value of n = "; cin>>n;

cout<<"n! = "<<n<<"! = "; nfak=1; for(i=1;i<=n;i++) { nfak*=i; if(i!=n) {cout<<i<<"x";}

else {cout<<i<<" = ";} }

cout<< nfak << endl;

system ("PAUSE"); }

2. While Looping

#include <iostream> using namespace std;

int main () { int n; double nfak;

cout<<"input the value of n = "; cin>>n;

cout<<"n! = "<<n<<"! = "; nfak=1; while(n>=1) { nfak*=n; if(n!=1) {cout<<n<<"x";}

else {cout<<n<<" = ";} n--; }

cout<< nfak << endl;

system ("PAUSE"); }

3. Do While Looping

#include <iostream> using namespace std;

int main () { int n; double nfak;

cout<<"input the value of n = "; cin>>n;

cout<<"n! = "<<n<<"! = "; nfak=1; do { nfak*=n; if(n!=1) {cout<<n<<"x";}

else {cout<<n<<" = ";} n--; } while (n>=1); cout<< nfak << endl;

system ("PAUSE"); }

4. Continue Statement

#include <iostream> using namespace std;

int main () { int n; cout<<"\nBilangan Kelipatan = "; cin>>n; cout<<endl;

for(int i=1;i<=100;i++) { if (i%n!=0)continue; cout<<i<<"\t"; }

system ("PAUSE"); }

5. Break Statement

#include <iostream> using namespace std;

int main () { int n; cout<<"\nBilangan Kelipatan = "; cin>>n; cout<<endl;

for(int i=1;i<=100;i++) { if (i%n!=0)break; cout<<i<<"\t"; }

system ("PAUSE"); }

6. Goto Statement

#include <iostream> using namespace std;

int main ()

{ hasil: int n; cout<<"\nBilangan Kelipatan = "; cin>>n; cout<<endl;

for(int i=1;i<=100;i++) { if (i%n!=0)continue; cout<<i<<"\t"; }

goto hasil; system ("PAUSE"); }

Tugas Tambahan

#include <iostream> using namespace std;

int main() { int a,b,c,s,n; a=1; b=1;

cout<<"

''Selamat Mencoba''\n";

cout<<"\nMasukan Banyak Deret yang Anda inginkan : "; cin>>n; cout<<endl; if(n<2) { cout<<"\nBanyak Deret Harus Lebih dari 1 "; goto akhir; }

cout<<a<<", "<<b<<", ";

for(s=2;s<n;s++) { c=a+b; cout<<c<<", "; a=b; b=c; }

akhir:

cout<<"\n\nTerima Kasih"<<endl; system("pause"); return 0; }

You might also like