Download presentation
Presentation is loading. Please wait.
1
Unazat
2
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.
3
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ë cilin ndryshohet variabla i. blloku – komandat të përfshira brenda unazës. blloku i<=fund Po Jo i=i+hapi; i=fillim;
4
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=Shuma+i; cout << "Shuma=" << Shuma << endl; } else cout << "Gabim! n:[1,99]"; return 0; Shembuj daljesh Dalje 1 n: 5 Shuma=15 Dalje 2 n: 50 Shuma=1275 Dalje 3 n: 125 Gabim! n:[1,100] Zgjidhje 2. … for(int i=1; i<=n; Shuma+=i, i++);
5
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=i+2) Shuma=Shuma+(i*i); cout << "Shuma=" << Shuma << endl; return 0; } Shembuj daljesh Dalje 1 n: 6 Shuma=56 Dalje 2 n: 20 Shuma=1540
6
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=1; i<=n; i++) F=F*i; cout << n << "! = " << F << endl; return 0; } Shembuj daljesh Dalje 1 n: 3 3! = 6 Dalje 2 n: 4 4! = 24 Dalje 3 n: 5 5! = 120 3! = 1*2*3=6 4! = 1*2*3*4=24 5! = 1*2*3*4*5=120
7
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=F*i; z=F-2*x+1; cout << "z=" << z << endl; return 0; } Shembuj daljesh Dalje 1 x m: 5 4 z=111 Dalje 2 x m: 3 7 z=40315
8
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; const double pi= ; double r; cout << " r S P \n"; cout << fixed << setprecision(2); for(r=a; r<=b; r=r+0.5) cout << setw(6) << r << setw(8) << pi*r*r << setw(8) << 2*pi*r << endl; return 0; }
9
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;
10
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;
11
Shembull 2. Shuma e katrorëve të numrave natyrorë 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)…
12
Sa është shuma për a=9 dhe b=3?
13
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;
14
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;
15
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;
16
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; 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; }
17
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;
18
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;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.