Detyra Te Zgjidhura CPP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The document content discusses C++ programs that use different programming constructs like switch statements, if statements, and for loops to perform calculations and logic.

The switch statement allows a program to evaluate an expression and attempt to match the expression's value to a list of case labels, and then execute the associated block of code.

The if statement allows a program to execute a block of code only if a specified condition is true.

ARBLEND SHALA

//Qershor 2014/2015
//E zgjidhur me switch
#include <iostream>
using namespace std;
void Llogaritja(double a1, double a2, char karakteri);
int main()
{
double a, b;
char x;
cout << "Shkruaj nje ne numer te plote: "; cin >> a;
cout << "Tani edhe nje numer te plote ju lutem: "; cin >> b;
cout << "Shtypni nje karakter\nM per mbledhje\nZ per zbritje\nS per shumezim\nP
per pjestim: "; cin >> x;
Llogaritja(a, b, x);
}
void Llogaritja(double a1, double a2, char karakteri)
{
switch (karakteri)
{
case 'M':
case 'm':
cout << a1 << " + " << a2 << " = " << a1 + a2 << endl; break;
case 'z':
case 'Z':
cout << a1 << " - " << a2 << " = " << a1 - a2 << endl; break;
case 's':
case 'S':
cout << a1 << " * " << a2 << " = " << a1 * a2 << endl; break;
case 'p':
case 'P':
cout << a1 << " / " << a2 << " = " << a1 / a2 << endl; break;
default:
cout << "Keni shtypur karakter palidhje !!" << endl;
}
}
//Qershor 2014/2015 (e zgjidhur me if)
#include <iostream>
using namespace std;
void Llogaritja(double a1, double a2, char karakteri);
int main()
{
double a, b;
char x;
cout << "Shkruaj nje ne numer te plote: "; cin >> a;
cout << "Tani edhe nje numer te plote ju lutem: "; cin >> b;
cout << "Shtypni nje karakter\nM per mbledhje\nZ per zbritje\nS per shumezim\nP
per pjestim: "; cin >> x;
Llogaritja(a, b, x);
}
void Llogaritja(double a1, double a2, char karakteri)
{
if (karakteri == 'M' || karakteri == 'm')
cout << a1 << " + " << a2 << " = " << a1 + a2 << endl;
else if (karakteri == 'Z' || karakteri == 'z')
cout << a1 << " - " << a2 << " = " << a1 - a2 << endl;
else if (karakteri == 'S' || karakteri == 's')
cout << a1 << " * " << a2 << " = " << a1 * a2 << endl;
else if (karakteri == 'P' || karakteri == 'p')
cout << a1 << " / " << a2 << " = " << a1 / a2 << endl;
else cout << "Keni shtypur nje karakter palidhje! " <<
endl;
}
//Te shkruhet programi per formimin e vektori A, nga antaret tek dhe me temedhenj se 10
te matrices B(m,m)
//Qershor 2014/2015
#include <iostream>
using namespace std;
int main()
{
const int m = 3, n = 3;
int B[m][n] = { { 10,-20,33 },
{ -40,35,-60 },
{ 79,-80,90 } };
int i, j, k, A[m*n];
//Krijimi i vektorit – Fillimi k=0;
k = 0;
for (i = 0;i<m;i++)
for (j = 0;j<n;j++)
if ((B[i][j] > 10) && ((B[i][j] % 2) == 1))
{
A[k] = B[i][j];
k = k + 1;
}
//Shtypja e vektorit
cout << "A=[";
for (i = 0;i<k;i++)
{
cout.width(5);
cout << A[i];
}
cout << "]\n";
system("Pause");
return 0;
}
//Detyre Provimi (NEW) JANAR 2016
#include <iostream>
using namespace std;
void Kontrolla(int a, int b);
int main()
{
int h1, h2;

Fillimi1:
cout << "Shtypni nje numer ne mes 1-1000: ";
cin >> h1;
if ((h1 < 1) || h1>1000)
{
cout << "Ju duhet te shtypni nje numer ne internvalin 1-1000!!\n";
goto Fillimi1;
}

Fillimi2:
cout << "Tani ju lutem shtypni nje numer ne mes 1-10: ";
cin >> h2;
if ((h2 < 1) || h2>10)
{
cout << "Ju duhet te shtypni nje numer ne internvalin 1-10!!\n";
goto Fillimi2;
}

Kontrolla(h1, h2);

void Kontrolla(int a, int b)


{
float rezultati;
rezultati = a / b;
if ((a%b) == 0)
{
cout << "Numri " << a << " plotojestohet me numrin " << b << endl;
cout << a << " / " << b << " = " << rezultati << endl;
}

else
{
cout << "Numri " << a << " nuk plotpjestohet me " << b << " ! " << endl;
}
}
//Detyre Kollokfiumi #VN
#include <iostream>
using namespace std;
int i, j;
void nrPlotPjest(int x)
{
int y;
for (i = 1;i <= x;i++)
if (x%i == 0 && i != 1 && i != x)
{
y = x / i;
cout << x << " plotpjesohet me " << i << " (" << x << "/" << i <<
"=" << y << ")" << endl;
}
}
int main() {
int x;
Fillimi:
cout << "Gjeni plotpjestuesit per x = "; cin >> x;
if (x<1 || x>100)
{
cout << "Ju lutem shtypeni nje vlere ne intervalin [1,100] ";
goto Fillimi;
}

int f;
for (i = 1;i <= x;i++)
{
f = 0;
for (j = 1;j <= i;j++)
{
if (i%j == 0) f++;
}
}
if (f == 2)
cout << "Numri " << x << " eshte primar " << endl;
nrPlotPjest(x);
return 0;
}
Disa zgjidhje nga lenda Gjuhe Programumese C++ te Afatit Shtator
2015:

//Detyra 2a
#include <iostream>
using namespace std;
int main()
{
int a, b;
double Z;
cout << "Shtypni vleren e variables a: ";
cin >> a;
cout << "Shtypni vleren e variables b: ";
cin >> b;

if (a != b)
{
int i = 1;
if (i < a*a*a)
Z = a + b;
else
Z = a + b;

}
else
{
int j = 2;
if (j <= 2 * a + b)
Z = a*b;
else
Z = a / b;
}
cout << "Rezultati final: " << Z << endl;
return 0;
}
3.Te shkruhet programi per formimin e vektorit A, nga antaret cift e matrices
B(m,m).Matrica B dhe variabla m te incicializohen direct me vlera gjate
deklarimit te tyre.
//Det 3
#include <iostream>
using namespace std;
int main()
{
const int m = 3, n = 3;
int B[m][n] = { { 10,-20,3 },
{ -40,50,-60 },
{ 70,-80,9 } };
int i, j, k, A[m*n];
//Krijimi i vektorit – Fillimi k=0;
k = 0;
for (i = 0;i<m;i++)
for (j = 0;j<n;j++)
if (((B[i][j] % 2) == 0))
{
A[k] = B[i][j];
k = k + 1;
}
//Shtypja e vektorit
cout << "A=[";
for (i = 0;i<k;i++)
{
cout.width(5);
cout << A[i];
}
cout << "]\n";
system("Pause");
return 0;
}
Det 5.
#include <iostream>
using namespace std;
void Dita(int a);
void Dita(int a)
{

switch (a)
{
case 1: cout << "Dita " << a << " eshte E hane ";
break;
case 2: cout << "Dita " << a << " eshte E marte ";
break;
case 3: cout << "Dita " << a << " eshte E merkure ";
break;
case 4: cout << "Dita " << a << " eshte E enjte ";
break;
case 5: cout << "Dita " << a << " eshte E premte ";
break;
case 6: cout << "Dita " << a << " eshte E shtune ";
break;
case 7: cout << "Dita " << a << " eshte E diele ";
break;
default: cout << "Gabim-Dita duhet te jete mes 1 dhe 7\n";
}

int main()
{
int hyrja;
cout << "Jepni numrin e dites: 1 deri ne 7: ";
cin >> hyrja;
Dita(hyrja);
}

You might also like