Skip to content

While sikl operatori

Kirish

While ham for kabi takrorlanish operatori hisoblanadi Whileni for operatoridan farqi whileda ma’lum shartlar qanoatlantirganda buyruqlar blokining ko’p marta bajarilish imkonini beradi.

Misollar

1-misol

# salom dunyo so’zini 10 marta chiqarish
a = 10
while a == 10:
    print('salom dunyo')

2-misol

# 1 dan 10 gacha bo’lgan sonlarni chiqarish
n = 1
while n <= 10:
    print(n)
    n += 1

3-misol

# 1 dan 10 gacha bo’lgan sonlar yig’indisini hisoblash
n = 1
s = 0
while n <= 10:
    s += n
    n += 1
print(s)

4-misol

# juft sonlarni chiqarish
n = int(input('n = '))
c = 1
while c <= n:
    if c % 2 != 1:
        print(c)
    c += 1

5-misol

# n soni necha xonaligini aniqlash
n = int(input('n = '))
c = 0
while n != 0:
    n //= 10
    c += 1
print(c)

6-misol

# n sonini raqamlari yig’indisini hisoblash
n = int(input('n = '))
c = 0
s = 0
while n != 0:
    n //= 10
    c += 1
    s += c
print(s)

7-misol

# n sonini m soniga bo’lgandagi
# butun va qoldiq qismini bo’lish
# va qoldiqsiz bo’lish amalisiz chiqarish
n = 10
m = 2
c = 0
while n > m:
    n -= m
    c += 1
print(n, c)

8-misol

# kompyuter o’ylagan sonni topish
import random
b = True
a = random.randint(0, 10)
while b:
    n = int(input('son kiriting = '))
    if n == a:
        b = False
print('topdingiz kompyuter', a, ' sonini oylagandi')