180 likes | 342 Views
5. Unazat. Unaza FOR. Numri i ekzekutimeve të ciklit është i njohur. for(inicializim; kushti; azhurnim) komanda;. for(inicializim; kushti; azhurnim) komanda;. for(inicialzimim; kushti; azhurnim){ komanda1; komanda2; … komandaN; }. Komanda e zakonshme.
E N D
Unaza FOR Numri i ekzekutimeve të ciklit është i njohur . for(inicializim; kushti; azhurnim) komanda; for(inicializim; kushti; azhurnim) komanda; for(inicialzimim; kushti; azhurnim){ komanda1; komanda2; … komandaN;} • Komanda e zakonshme. • Komanda for e përbërë. • Komanda for e ndërthurur.
i=fillim; Jo Po blloku i=i+hapi; i<=fund for(i=fillim;i<=fund; i=i+hapi){ blloku;} PARAMETRA TË UNAZËS: i – variabla e unazës. fillim – vlera fillestare variablës i. fund –vlera përfundimtare e variablës i. hapi –hapi me të cilinndryshohet variabla i. blloku – komandat të përfshira brenda unazës.
Komanda FOR e zakonshme Shembull 1. Të llogaritet shuma e numrave natyrorë mes 1 dhe n(n<100). Zgjidhje 1. #include <iostream> using namespace std; int main() { int n,i,Shuma=0; cout << "n: "; cin >> n; if(n>=1 && n<=99) { for(i=1; i<=n; i++) Shuma+=i; cout << "Shuma=" << Shuma << endl; } else cout << "Gabim! n:[1,99]"; return 0; } Shembuj daljesh Dalje 1n: 5Shuma=15 Dalje 2 n: 50Shuma=1275 Dalje 3 n: 125Gabim! n:[1,100] Zgjidhje 2. … for(int i=1; i<=n; Shuma+=i, i++); …
Shembull 2. Shuma e katrorëve të numrave natyrorë çift mes 2 dhe n. #include <iostream> using namespace std; int main() { int n,Shuma=0; cout << "n: "; cin >> n; for(int i=2; i<=n; i+=2) Shuma+=i*i; cout << "Shuma=" << Shuma << endl; return 0; } Shembuj daljesh Dalje 1n: 6Shuma=56 Dalje 2 n: 20Shuma=1540
Shembull 3. Të llogaritet faktorieli i numrit n. n! = 1*2*3* … *n #include <iostream> using namespace std; int main() { int n,F=1; cout << "n: "; cin >> n; for(int i=2; i<=n; i++) F*=i; cout << n << "! = " << F << endl; return 0; } Shembuj daljesh Dalje 1n: 33!= 6 Dalje 2 n: 44!= 24 Dalje 3 n: 55!= 120 3! = 1*2*3=6 4! = 1*2*3*4=24 4! = 1*2*3*4*5=120
Shembull 4. Të llogaritet vlera e funksionit z=(m+1)! – 2x + 1 #include <iostream> using namespace std; int main() { int x,m,z,F=1; cout << "x m: "; cin >> x >> m; for(int i=2; i<=m+1; i++) F*=i; z=F-2*x+1; cout << "z=" << z << endl; return 0; } Shembuj daljesh Dalje 1x m: 5 4z=111 Dalje 2 x m: 3 7z=40315
Shembull 5. Të shtypet tabela e sipërfaqeve dhe e perimetrave të rrethit me reze r, e cila ndryshon mes vlerës 1 dhe 5 me hap 0.5 #include <iostream> #include <iomanip> using namespace std; int main() { const int a=1, b=5; double r, pi=3.14159; cout << " r S P \n"; cout << fixed << setprecision(2); for(r=a; r<=b; r+=0.5) cout << setw(6) << r << setw(8) << pi*r*r << setw(8) << 2*pi*r << endl; return 0; }
Komanda FOR e përbërë Shembull 1. Të shtypet tabela e faktorielëve të numrave natyror mes 1 dhe n. #include <iostream> #include <iomanip> using namespace std; int main() { int n,F=1; cout << "n: "; cin >> n; cout << " i F \n"; for(int i=2; i<=n; i++) { F*=i; cout << setw(5) << i << setw(10) << F << endl; } return 0; }
Shembull 2. Të llogaritet për numra natyrorë x dhe n. #include <iostream> #include <iomanip> using namespace std; int main() { int x,n; cout << "x n: "; cin >> x >> n; cout << " n y \n"; for(int i=1, y=1; i<=n; i++) { y=y*x; cout << setw(5) << i << setw(10) << y << endl; } return 0; }
Shembull 2. Shuma e katrorëve të numrave natyrorë çift mes a dhe b, duke mos i përfshirë katrorët e numrave 5,6,9. (fq.177) #include <iostream> #include <iomanip> using namespace std; int main() { int a,b,Shuma=0; bool z; cout << "a b: "; cin >> a >> b; cout << " n Katrori \n"; for(int i=a; i<=b; i++) { z = (i==5) ||(i==6) || (i==9); if(z==false) { Shuma+=i*i; cout << setw(5) << i << setw(12) << i*i << endl; } } cout << "Shuma=" << Shuma << endl; return 0; } Pa variablën logjike z: if(i!=5 && i!=6) && i!=9)…
Shembull 3. Të gjinden numrat natyrorë mes m dhe n që janë të plotpjestueshëm me numrin r. #include <iostream> #include <iomanip> using namespace std; int main() { int m,n,r,x,y; cout << "m n r: "; cin >> m >> n >> r; cout << "Te plotpjestueshem me " << r << endl << " Numri Numri/" << r << endl; for(int i=m; i<=n; i++) { x=i/r; y=x*r; if(y==i) cout << setw(5) << i << setw(10) << x << endl; } return 0; }
Komanda FOR të ndërthurura Shembull 1. Të shtypen numrat: #include <iostream> #include <iomanip> using namespace std; int main() { for(int i=0; i<=9; i++) { for(int j=0; j<=i; j++) cout << setw(2) << j; cout << endl; } return 0; }
Shembull 2. Të shtypet tabela e vlerave të funksionit: për vlera të variablës k mes 0 dhe n. x,m,n – vlera hyrëse #include <iostream> #include <iomanip> using namespace std; int main() { int k,m,n; double x,y,Shuma; cout << "x m n: "; cin >> x >> m >> n; cout << " k y \n"; for(k=0; k<=n; k++) { Shuma=0; for(int i=2; i<=m+1; i++) Shuma+=(2*k+i-1); y=x/3-4*Shuma; cout << setw(5) << k << setprecision(4) << fixed << setw(15) << y << endl; } return 0; }
Shembull 3. Të shtypen numrat e Piagorës x,y dhe z për të cilët vlen barazimi: #include <iostream> #include <iomanip> using namespace std; int main() { int x,y,z,n,a,b; cout << "n: "; cin >> n; cout << " x y z \n"; for(x=1; x<=n-2; x++) for(y=x+1; y<=n-1; y++) for(z=y+1; z<=n; z++) if(x*x+y*y==z*z) cout << setw(5) << x << setw(5) << y << setw(5) << z << endl; return 0; }
Komanda FOR me variabël numruese të tipit Char Shembull 1. Të shtypen shkronjat e mëdhe dhe kodet e tyre: #include <iostream> using namespace std; int main() { char k; int x,n=0; for(k='A'; k<='Z'; k++) { x=k; cout << k << " " << x << " "; n++; if(n%8==0) cout << endl; } cout << endl; return 0; }
Komanda FOR me variabël numruese të tipit Char Shembull 1. Të shtypen shkronjat: #include <iostream> #include <iomanip> using namespace std; int main() { char x,y; for(x='Z'; x>='A'; x--) { for(y='A'; y<=x; y++) cout << setw(2) << y; cout << endl; } return 0; }