Описание на циклични алгоритми - упражнение 1
Зад. 1. Да се състави програма за пресмятане на произведението
Чрез цикъл с предусловие:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
i=4;
while (i<=200)
{
P=P*(2+sqrt((float)i/3));
i+=2;
}
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
i=4;
while (i<=200)
{
P=P*(2+sqrt((float)i/3));
i+=2;
}
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
Чрез цикъл с постусловие:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
i=4;
do
{
P=P*(2+sqrt((float)i/3));
i+=2;
}
while (i<=200);
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
i=4;
do
{
P=P*(2+sqrt((float)i/3));
i+=2;
}
while (i<=200);
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
Чрез цикъл с управляваща променлива:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
for (i=4; i<=200; i+=2)
P=P*(2+sqrt((float)i/3));
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
#include <math.h>
using namespace std;
int main()
{
int i;
double P;
P=1;
for (i=4; i<=200; i+=2)
P=P*(2+sqrt((float)i/3));
cout << "Proizvedenieto e " << P << endl;
system ("pause");
return 0;
}
Зад. 2. Да се състави програма за извеждане в колона на стойностите на функцията f(x)=х2+10х, където х=1, 2, 3, ... Извеждането спира след извеждане на първата стойност но функцията, по-голяма от 100.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x,f;
x=1;
do
{
f=x*x+10*x;
cout << f << endl;
x++;
}
while (f<=100);
system ("pause");
return 0;
}
#include <math.h>
using namespace std;
int main()
{
int x,f;
x=1;
do
{
f=x*x+10*x;
cout << f << endl;
x++;
}
while (f<=100);
system ("pause");
return 0;
}
Зад. 3. Да се състави програма за пресмятане на периметъра на триъгълник по въведени дължини на страните му, като се прецизира входът.
#include <iostream>
using namespace std;
int main()
{
float a,b,c;
do
{
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
cout << "c=";
cin >> c;
}
while ((a<=0)||(b<=0)||(c<=0)||(a+b<=c)||(b+c<=a)||(c+a<=b));
cout << "Perimetyryt na triygylnika e " << a+b+c << endl;
system ("pause");
return 0;
}
using namespace std;
int main()
{
float a,b,c;
do
{
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
cout << "c=";
cin >> c;
}
while ((a<=0)||(b<=0)||(c<=0)||(a+b<=c)||(b+c<=a)||(c+a<=b));
cout << "Perimetyryt na triygylnika e " << a+b+c << endl;
system ("pause");
return 0;
}
Зад. 4. Да се състави програма за пресмятане на n! (n факториел – n!=1.2.3…n) при въведена стойност на n.
#include <iostream>
using namespace std;
int main()
{
int i,n,x;
do
{
cout << "n=";
cin >> n;
}
while (n<=0);
i=1;
x=1;
while (i<=n)
{
x=x*i;
i++;
}
cout << "n! = " << x << endl;
system ("pause");
return 0;
}
using namespace std;
int main()
{
int i,n,x;
do
{
cout << "n=";
cin >> n;
}
while (n<=0);
i=1;
x=1;
while (i<=n)
{
x=x*i;
i++;
}
cout << "n! = " << x << endl;
system ("pause");
return 0;
}