Professional Documents
Culture Documents
komanda1;
komanda2;
…
komandaN;
}
Komanda e zakonshme.
Komanda for e përbërë.
Komanda for e ndërthurur.
1
for(i=fillim; i<=fund; i=i+hapi){
blloku;
}
PARAMETRA TË UNAZËS:
2
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; Shembuj daljesh
Dalje 1
int main() { n: 5
int n,i,Shuma=0; Shuma=15
cout << "n: ";
cin >> n; Dalje 2
if(n>=1 && n<=99) { n: 50
for(i=1; i<=n; i++) Shuma+=i; Shuma=1275
cout << "Shuma=" << Shuma << endl;
} Dalje 3
else cout << "Gabim! n:[10,99]"; n: 125
return 0; Gabim! n:[1,100]
}
Zgjidhje 2.
…
for(int i=1; i<=n; Shuma+=i, i++);
…
3
Shembull 2. Shuma e katrorëve të numrave natyrorë çift mes 2 dhe n.
n6
Shuma 2 2 4 2 6 2 4 16 36 56
4
Shembull 3. Të llogaritet faktorieli i numrit n.
n! = 1*2*3* … *n
#include <iostream>
using namespace std;
Shembuj daljesh
int main() { Dalje 1
int n,F=1; n: 3
cout << "n: "; 3! = 6
cin >> n;
for(int i=2; i<=n; i++) F*=i; Dalje 2
cout << n << "! = " << F << endl; n: 4
return 0; 4! = 24
}
Dalje 3
3! = 1*2*3=6
n: 5
4! = 1*2*3*4=24 5! = 120
4! = 1*2*3*4*5=120
5
Shembull 4. Të llogaritet vlera e funksionit z=(m+1)! – 2x + 1
#include <iostream>
Shembuj daljesh
using namespace std;
Dalje 1
int main() { x m: 5 4
int x,m,z,F=1; z=111
cout << "x m: ";
cin >> x >> m;
Dalje 2
for(int i=2; i<=m+1; i++) F*=i;
x m: 3 7
z=F-2*x+1;
z=40315
cout << "z=" << z << endl;
return 0;
}
6
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;
}
7
Komanda FOR e përbërë
#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;
}
8
Shembull 2. Të llogaritet y xn 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;
}
9
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)…
10
Sa është shuma për a=9 dhe b=3?
11
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;
}
12
Komanda FOR të ndërthurura
#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;
}
13
m 1
x
Shembull 2. Të shtypet tabela e vlerave të funksionit: y 4 (2k i 1)
për vlera të variablës k mes 0 dhe n. 3 i2
#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;
}
14
Shembull 3. Të shtypen numrat e Piagorës x,y dhe z për të cilët vlen barazimi:
x2 y2 z2
#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;
}
15
Komanda FOR me variabël numruese të tipit Char
#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;
}
16
Komanda FOR me variabël numruese të tipit Char
#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;
}
17