24- misol. n soning barcha bo‘luvchilarini aniqlovchi dastur tuzilsin.
1-usul
#include
using namespace std;
void main()
{
int n;
cout<<"n="; cin>>n;
for(int i=1; i<=n/2; i++) //Sonning barcha bo'luvchilari 1 dan n/2 gacha oraliqda joylashgan bo'ladi.
if(n%i==0)
cout<
}
2-usul
1-usul
25-misol. a va b sonlarining EKUBini toping.
1-usul
#include
using namespace std;
int main()
{
int a, b, EKUB;
cout <<"a="; cin>>a;
cout <<"b="; cin>>b;
if(a>b)
{
for(int i=1; i<=b; i++)
if(a%i==0 && b%i==0)
EKUB=i;
}
else
{
for(int i=1; i<=a; i++)
if(a%i==0 && b%i==0)
EKUB=i;
}
cout<<"EKUB="< return 0;
}
2-usul. Evklid algoritmi
#include
using namespace std;
int main()
{
int a, b, EKUB;
cout <<"a="; cin>>a;
cout <<"b="; cin>>b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout< return 0;
}
26-misol. Ikki sonning eng kichik umumiy karralisini (EKUK) kamida ikkita usul bilan topuvchi dastur tuzing.
#include
using namespace std;
int main()
{
int a, b, EKUK;
cout <<"a="; cin>>a;
cout <<"b="; cin>>b;
if(a {
for(int i=a*b; i>=b; i--)
if(i%a==0 && i%b==0)
EKUK=i;
}
else
{
for(int i=a*b; i>=a; i--)
if(i%a==0 && i%b==0)
EKUK=i;
}
cout<return 0;
}
27-misol. Berilgan n soni tub son bo‘lishini tekshiruvchi dastur tuzing. Faqatgina 2 ta bo‘luvchiga – 1 va o‘ziga ega bo‘lgan natural songa tub son deyiladi. Shuni qayd etish kerakki, 1 soni tub songa kirmaydi, chunki uning faqatgina bitta bo‘luvchisi – o‘zi bor.
#include
using namespace std;
int main()
{
int n, S=0;
cout<<"n="; cin>>n;
for(int i=1; i<=n/2; i++)
if (n%i==0)
S++;
if (S==2)
cout<<"Tub son";
else
cout<<"Tub son emas";
return 0;
}
29-misol. Berilgan [n, m] oraliqda mavjud tub sonlarni topuvchi dastur tuzing.
#include
using namespace std;
int main()
{
int n, m, S=0;
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
for(int j=n; j<=m; j++)
{
for(int i=1; i<=j; i++)
if (j%i==0)
S++;
if (S==2)
cout<S=0;
}
return 0;
}
30-misol. 7 ga bo‘linadigan, va 2, 3, 4, 5, 6 ga bo‘linganda qoldig‘i 1 ga teng bo‘lgan eng kichik butun sonni toping.
#include
#include
using namespace std;
int main()
{
cout << " Ushbu shartni qanoatlantiruvchi eng kichik son: n=";
int i = 8, n=0;
while (n==0)
{
if (i % 7 == 0 && i % 2 == 1 && i % 3 == 1 && i % 4 == 1 && i % 5 == 1 && i % 6 == 1)
{
n = i;
break;
}
i++;
}
cout << n << endl;
return 0;
}
31. 2320 va 2350 oraliqdagi qaysi sonlar tub, qaysilari murakkab?
#include
#include
using namespace std;
int main()
{
int S=0;
for (int i = 2320; i <= 2350; i++)
{
for (int j = 2; j < i/2; j++)
if (i%j == 0)
{
S++;
}
if (S==0)
cout << i << "-TUB" << endl;
else
cout << i << "-MURAKKAB" << endl;
S = 0;
}
return 0;
}
32. Kiritilgan ixtiyoriy 1 dan katta bo‘lgan natural sonning 1 ga teng bo‘lmagan eng kichik natural toq bo‘luvchisini topuvchi dastur tuzing.
#include
#include
using namespace std;
int main()
{
int n, k = 0;
cout << "n="; cin >> n;
for (int i = 3; i <= n; i += 2)
{
if (n%i == 0)
{
cout << "Ushbu sonning 1 dan katta eng katta toq bo'luvchisi: "<< i< k = 1;
break;
}
}
if (!k)
cout << "Bunday bo'luvchisi yo'q" << endl;
return 0;
}
33. O‘zining raqamlar yig‘indisi kvadratidan 6 ga kichik bo‘lgan ikki xonali sonni toping.
#include
#include
using namespace std;
int main()
{
int q1, q2, i=10, k = 0;
while (!k && i<100)
{
q1 = i % 10;
q2 = i / 10;
if (i == (q1 + q2)*(q1 + q2) - 6)
{
cout << i << endl;
k = 1;
break;
}
i++;
}
return 0;
}
34. Qisqartirish mumkin bo‘lgan kasr berilgan, uning surat va maxraji - natural m va n sonlar. Shunday m1 va n1 sonlarni topingki, ularni umumiy bo‘luvchisi bo‘lmasin, bo‘lsin, ya’ni kasr qisqarsin.
#include
#include
using namespace std;
int EKUBT(int m, int n)
{
while (m != n)
{
if (m > n)
{
m = m - n;
}
else
{
n = n - m;
}
}
return m;
}
int main()
{
int m, n, EKUB, m1, n1;
cout << "m="; cin >> m;
cout << "n="; cin >> n;
EKUB = EKUBT(m, n);
m1 = m / EKUB;
n1 = n / EKUB;
cout << m1 << "/" << n1 << endl;
return 0;
}
35. 1 dan n gacha bo‘lgan butun sonlarning har biriga o‘zining barcha bo‘luvchilarini chop etuvchi dasturni tuzing. Masalan, 35 soning bo‘luvchilari: 1, 5, 7, 35. Shunga o‘xshash bo‘luvchilar ro‘yxati 1 dan n gacha bo‘lgan sonlar uchun chiqarilishi kerak.
#include
#include
using namespace std;
int main()
{
int i = 1, n;
cout << "n="; cin >> n;
while (i <= n)
{
cout << i << "-soni bo'luvchilari: ";
cout << "1 ";
for (int j = 2; j <= i / 2; j++)
if (i%j == 0)
cout << j << " ";
cout << i << endl;
i++;
}
return 0;
}
36. Quyidagi xususiyatlarga ega bo‘lgan eng kichik natural n soni topilsin:
a) uning o‘nlar xonasidagi raqami 6;
b) 6 ni sonning oxiridan boshiga o‘tkazsa, hosil bo‘lgan son berilganiga nisbatan 4 baravar katta bo‘ladi.
#include
#include
using namespace std;
int Teskari_son(int i)
{
int S1=0;
while (i>0) //Sonni teskari tartibda almashtirish algoritmi
{
S1 = S1 * 10 + i % 10;
i = i / 10;
}
return S1;
}
int main()
{
int i = 16, k=0, S;
while (i<606)
{
S = Teskari_son(i);
cout << S << endl;
if (4 * i == S)
{
cout << i;
k = 1;
break;
}
i += 10;
}
return 0;
}
37. Raqamlar yig‘indisi kvadrati m ga teng bo‘lgan n dan kichik barcha natural sonlarni ekranga chiqaruvchi dastur tuzing.
#include
#include
using namespace std;
int R_Yig(int i)
{
int q, S = 0;
while (i > 0)
{
q = i % 10;
S += q;
i = i / 10;
}
return S*S;
}
int main()
{
int n, m;
cout << "m="; cin >> m;
cout << "n="; cin >> n;
for (int i = 10; i < n; i++)
{
if (R_Yig(i) == m)
cout << i << " ";
}
return 0;
}
38. Berilgan p butun sonni ikkita butun sonning kvadratlari yig‘indisi sifatida ifodalash mumkinmi? Ushbu masalani yechadigan dastur tuzing.
#include
#include
using namespace std;
int main()
{
int p;
cout << "p="; cin >> p;
for (int i = 1; i <= p; i++)
for (int j = 1; j <= i; j++)
for (int k = 1; k <= j; k++)
if (pow(i, 2) + pow(j, 2) == p)
cout << i << " " << j << endl;
return 0;
}
39. = ( + ) shartni qanoatlantiruvchi barcha to‘rt xonali sonlar topilsin.
#include
#include
using namespace std;
int Natija(int i)
{
int q, q1, S = 0;
q = i / 100;
q1 = i % 100;
S = pow(q, 2) + pow(q1, 2);
return S;
}
int main()
{
for (int i = 1000; i <= 9999; i++)
{
if (i == Natija(i))
cout << i << endl;
}
return 0;
}
40. a (a = 2, 3, 4, 5, 6, 7, 8, 9) raqami bilan tugaydigan va oxirgi raqamini sonning boshiga o‘tkazganda, raqamlar o‘rni almashtirilgan sonning tarkibida nechta 1 bo‘lsa shuncha marta oshish xususiyatiga ega bo‘lgan sonlar topilsin.
#include
#include
using namespace std;
int Teskari_son(int i)
{
int S1 = 0;
while (i>0) //Sonni teskari tartibda almashtirish algoritmi
{
S1 = S1 * 10 + i % 10;
i = i / 10;
}
return S1;
}
int BirS(int i)
{
int S = 0, q;
while (i > 0)
{
q = i % 10;
if (q == 1)
S++;
i = i / 10;
}
return S;
}
int main()
{
int j, S;
for (int i = 10; i <= 100000; i++)
{
j = Teskari_son(i);
S = BirS(j);
if (j == S*i)
cout << i << endl;
}
return 0;
}
41. dan katta bo‘lmagan barcha tub sonlarga bo‘linadigan butun n sonlar topilsin.
#include
#include
using namespace std;
int main()
{
int k;
for (int i = 1; i <= 1000; i++)
{
for (int j = 1; j <= (int)sqrt(i); j++)
{
if (i%j == 0)
{
k = 1;
}
else
{
k = 0;
break;
}
}
if (k)
cout << i << endl;
}
return 0;
}
42. Berilgan natural sonni quyidagi ko‘rinishda tasvirlash mumkin yoki mumkin emasligini tekshiradigan dastur tuzing:
a) 2 ta tub sonning ayirmasi;
#include
#include
using namespace std;
int TubA(int a, int b)
{
int S1 = 0, S = 0;
for (int i = 2; i <=a / 2; i++)
if (a%i == 0)
S++;
for (int i = 2; i <=b / 2; i++)
if (b%i == 0)
S1++;
if (S == 0 && S1==0)
return 1;
}
int main()
{
int b = 2, n, a, k=0;
cout << "n="; cin >> n;
a = b + n;
while (k==0)
{
k = TubA(a, b);
if (k)
{
cout << "HA" << endl;
cout << a << "-" << b << "=" << n;
}
a++;
b++;
}
return 0;
}
b) 3 ta tub sonning ayirmasi;
v) biron-bir tub sonning kvadrati;
#include
#include
using namespace std;
int TubA(int b)
{
int S = 0;
for (int i = 2; i <=b / 2; i++)
if (b%i == 0)
S++;
if (S == 0)
return 1;
}
int main()
{
int n, b, k;
float a;
cout << "n="; cin >> n;
a= sqrt(n);
b = (int)a;
if (b - a == 0)
{
k = TubA(b);
if (k==1)
cout << "HA";
else
cout << "YO'Q";
}
else
cout << "YO'Q";
return 0;
}
g) biron-bir tub sonning kubi.
HA yoki YO‘Q javobini chop etish kerak.
43. To‘rtburchakni kvadratlarga ajratish.
Tomonlari natural son bo‘lib, a va b ga teng bo‘lgan to‘rtburchak berilgan. Agar har doim to‘rtburchakdan yuzasi maksimal darajada bo‘lgan kvadrat kesib olinsa, uni tomonlari natural sonlar bilan ifodalangan nechta kvadratga ajratish mumkinligini aniqlovchi dastur tuzing.
44. Berilgan yuzali (yuza - natural son bilan ifodalangan kiritiladigan ma’lumot) tomonlari natural son bo‘lgan barcha to‘rtburchaklarni topish dasturini tuzing. Masalan, agar yuza 12 ga teng bo‘lsa, u holda biz uchta har xil to‘rtburchak hosil qilamiz:
Qirrasining o‘rnini almashtirganda biri ikkinchisidan hosil bo‘luvchi to‘rtburchaklarni bir xil deb hisoblaymiz.
#include
#include
using namespace std;
int main()
{
unsigned int S;
cout << "S="; cin >> S;
for (int i = 1; i <= S; i++)
for (int j = 1; j <= i; j++)
if (i*j == S)
cout << i << "x" << j << endl;
return 0;
}
45. 2 ta sonning o‘zaro tub yoki tub emasligini , uchta sonning o‘zaro tub yoki tub emasligini tekshiruvchi dastur tuzing.
#include
#include
using namespace std;
int TubA(int a, int b)
{
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
int main()
{
unsigned int a, b, c, k;
//-------------Ikki son uchun --------------//
cout << "a="; cin >> a;
cout << "b="; cin >> b;
cout << "c="; cin >> c;
k = TubA(a, b);
if (k == 1)
cout << "Bu ikki son o'zaro tub";
else
cout << "Bu ikkita son o'zaro tub emas!";
//-----------Uchta son uchun --------------//
k = TubA(TubA(a, b), c);
if (k)
cout << "Bu uchta son o'zaro tub";
else
cout << "Bu uchta son o'zaro tub emas!";
return 0;
}
46. ga bo‘linuvchi berilgan k sondan kichik bo‘lgan barcha n natural sonlarni toping.
#include
#include
using namespace std;
int main()
{
int k, i = 1;
float c;
cout << "k="; cin >> k;
while (i <= k)
{
if (((int)pow(i, 2) + 1) / (i + 1) == 0)
cout << i << " ";
else
cout << "0 ";
i++;
}
return 0;
}
47. birikma son hisoblangan eng kichik n sonni toping (Serpinskiy masalasi).
Do'stlaringiz bilan baham: |