Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ОПАМ.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
327 Кб
Скачать
☆

Int main()

{

int n,k;

bool flag;

cout<<" Number simplicity"<<endl;

cout<<" Enter integer: "<<endl;

cin>>n;

k=2;

flag=true;

while ((k<=(int)sqrt((double)n)) && flag)

{

if (n % k == 0)

flag=false;

++k;

}

if (flag) cout<<n<<" Is simple "<<endl;

else cout<<n<<" Is not simple "<<endl;

}

Приклад 3.5

/*Вгадати число, що його згенерував комп’ютер */

#include <iostream>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

Int main()

{

const int penalty=10;

int x, y,prize;

cout<<" Define number, which computer generate"<<endl;

srand((unsigned)time(NULL));

y=rand()%1000;

prize=100;

do

{

cout<<" Enter number ";

cin>>x;

if (x>y)

{

cout<<"Wrong, enter smaller"<<endl;

prize=prize-penalty;

}

if(x<y)

{

cout <<"Wrong, enter bigger"<<endl;

prize=prize-penalty;

}

}

while(x!=y);

cout<<" You have guessed right! "<<endl;

cout<<" You have won the prize "<<prize<<endl;

}

Приклад 3.6

/* Обчислення факторіала числа */

#include <iostream>

#include <math.h>

using namespace std;

Int main()

{

long factorial;

int n,i;

cout<<"Factorial calculation-n! "<<endl;

do

{

cout<<"Enter integer number:"<<endl;

cin>>n;

}

while (n>=16);

factorial=1;

for (i=2;i<=n;++i)

factorial*=i;

cout<<n<<"!="<<factorial<<endl;

}

Приклад 3.7

/* Визначити прості числа */

#include <iostream>

#include <math.h>

using namespace std;

Int main()

{

int n,k;

cout<<"Number simplicity"<<endl;

cout<<"Enter integer: "<<endl;

cin>>n;

k=2;

while (k<=(int)sqrt((double)n))

{

if (n % k == 0) break;

k++;

}

if (k==(int)sqrt((double)n)+1)

cout<<n<<" Is simple"<<endl;

else cout<<n<<" Is not simple"<<endl;

}

Приклад 3.8

/* Послідовність чисел Фібоначчі */

#include <iostream>

#include <math.h>

using namespace std;

Int main()

{

int f1,f2;

int fi;

int i,n;

cout<<"Enter the length of Fibonacci sequence: ";

cin>>n;

f1=1; f2=1;

if (n>0) cout<<f1;

if (n>1) cout<<" "<<f2;

for ( i=3; i<=n ; i++)

{

fi=f1+f2;

f1=f2;

f2=fi;

cout<<" "<<fi;

}

cout<<endl;

}

Приклад 3.9

/* Обчислення функції sin(x) за рядом Тейлора */

#include <iostream>

#include <math.h>

using namespace std;

Int main()

{

float x,s,item;

int i;

float eps;

cout<<"sin(x) calculation"<<endl;

cout<<"Enter function argument x: "<<endl;

cin>>x;

cout<<"Enter mistake"<<endl;

cin>>eps;

s=x;

item=x;

i=2;

while (fabs(item)>eps)

{

item*=(-x*x)/(i*(i+1));

s+=item;

i+=2;

}

cout<<"s="<<s<<" sin("<<x<<")= "<<sin(x)<<" error="<<fabs(s-sin(x))<<endl;

}

Приклад 3.10

/* Обчислити ланцюговий дріб */

#include <iostream>

#include <math.h>

using namespace std;

Int main()

{

float z, b; //поточний знаменник і доданок до нього

int n; //кількість дробових рисок

cout<<"Chain fractions"<<endl;

cout<<"Enter number of fractions n: "<<endl;

cin>>n; //увести кількість дробових рисок

b=2*n+1;

z=b; //останній знаменник

while (b>1)

{

b=b-2; //модифікувати b

z=b+1/z; //визначити поточний знаменник

}

cout<<" s="<<z<<endl;} //вивести значення ланцюгового дробу

Приклад 4.1

/* Калькулятор нарахувань за депозитним внесками */

#include <iostream>

#include <math.h>

#include <conio.h>

using namespace std;

char key; //вибраний пункт меню

float deposit; //початковий внесок

int period; //термін дії рахунку

float rate; //відсоткова ставка річного прибутку

float sum; //сума на депозиті

int year; //поточний рік

//======================== введення даних =========================================

void Init()

{

cout<<"Enter deposit "<<endl;

cin>>deposit;

cout<<"Enter period "<<endl;

cin>>period;

cout<<"Enter rate: 5..20 "<<endl;

cin>>rate;

rate/=100; //перетворення цілочислового відсотка на коефіцієнт rate

}

//======================= виведення результатів розрахунків ==========================

void Browse()

{

cout<<"Sum at the end of "<<year<<" year is "<<sum<<endl;

cout<<"Press enter to continue"<<endl;

getch(); //чекання натиснення клавіші ENTER

}

//======================= обчислення щорічних сум на рахунку =========================

void Solution()

{

sum=deposit; //початкове значення суми

for ( year=1; year<=period; year++)

{

sum*=(1+rate); //рекурентна формула збільшення суми

Browse();

}

}

//=========================== обчислення кінцевої суми ===============================

void Final()

{

year=period;

//розрахунок суми за формулою складних відсотків

sum=deposit*pow((1+rate),period);

Browse();

}

//================================= меню програми ==================================

void Menu()

{

cout<<"1. Enter data"<<endl;

cout<<"2. Year by year sum"<<endl;

cout<<"3. Final sum"<<endl;

cout<<"4. Clear calculations"<<endl;

cout<<"ESC - exit"<<endl;

cout<<"=========================="<<endl;

}

//================================= головна функція ============================= ======