Ichma ich sikllarni tashkil qilish




Download 5,53 Mb.
bet13/23
Sana13.05.2024
Hajmi5,53 Mb.
#230011
1   ...   9   10   11   12   13   14   15   16   ...   23
Bog'liq
Iroda BMI

Ichma ich sikllarni tashkil qilish
Har qanday dasturlash tillarida parametrli sikl operatori yoritilgan vaqtda, albatta ichma ich sikllarni tashkil qilish keltirib o‘tiladi. Python dasturlash tilida ham ichma ich sikllarni tashkil qilish imkoniyati mavjud. Ichma ich sikllarni for sikl operatori orqali hosil qilinadi. Ichma ich sikllar – bu takrorlanish ichida yana takrorlanish hosil qilinadi, takrorlanishning har bir qadamida yana n marta takrorlanish bajariladi. Ichma ich sikllarni tashkil etish jarayonini misollar yordamida tushuntirib o‘tamiz.
Misol. (sin( ) ) ni hisoblash dasturini tuzing.

n=input('n=')
n=int(n)
s=0
from math import*
for i in range(1,n+1):
p=1 for j in range(1,n+1):
p*=(sin(j)+i**2)
s+=p print('s=',s)
====== RESTART: C:\Users\User\Desktop\1.py
n=4
s= 77735.97341482465 >>>

Misol. ! ... ( 1) 1*2*3 3 1*2 2 n n x x n x y  x     ni hisoblash dasturini tuzing.


Bu misolni ikki usulda hisoblash dasturini tuzish mumkin, birinchisi ichma ich sikl asosida, ikkinchisi esa bitta sikl orqali, biz ikkala holatni ham keltirib o‘tamiz.
Ichma ich sikl orqali dasturini tuzish birinchi o‘rganuvchilar uchun bo‘ladi, lekin bu eng yomon usul hisoblanadi va u quyidagicha:



x=input('x=')
x=int(x)
n=input('n=')
n=int(n)
s=0
p=1 # suratdagi darajalarni aniqlaydi
t=-1 # ishorani aniqlaydi
q=1 #maxrajdagi darajalarni aniqlaydi
from math import*
for i in range(1,n+1):
p=1
q=1
for j in range(1,i+1):
p*=x
q*=j
s+=t*p/q
t*=-1
print('s=',s) ====== RESTART: C:\Users\User\Desktop\1.py =================== x=5 n=6 s= 8.368055555555557 >>>

Bitta sikl orqali bajarish jarayonini qarab o‘tamiz, bunda surat va maxrajdagi ko‘paytuvchilarni sikl ichiga olib yozamiz. (-1)n darajani hisoblashni eng maqbul usuli bu bitta o‘zgaruvchi orqali hisoblashdir, bunda sikl ichiga shu o‘zgaruvchiga -1 ni ko‘paytirish orqali bajaramiz.



x=input('x=')
x=int(x)
n=input('n=')
n=int(n)
s=0
p=1 # suratdagi darajalarni aniqlaydi
t=-1 # ishorani aniqlaydi
q=1 #maxrajdagi darajalarni aniqlaydi
from math import*
for i in range(1,n+1):
p*=x
q*=i
s+=t*p/q
t*=-1
print('s=',s)
====== RESTART: C:\Users\User\Desktop\1.py
x=5
n=6
s= 8.368055555555557 >>>



Download 5,53 Mb.
1   ...   9   10   11   12   13   14   15   16   ...   23




Download 5,53 Mb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Ichma ich sikllarni tashkil qilish

Download 5,53 Mb.