DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

Python-Exercise

CODE

# print [1 1 1 1 1]

count = 0
i = 5

while count < i:
    print ("1" ,end=" ")
    count = count+1



# print [1 2 3 4 5]

count = 1
i = 5

while count <= i:
    print (count ,end=" ")
    count = count+1



# print [1 3 5 7 9]

count = 1
i = 10

while count < i:
    print (count ,end=" ")
    count = count+2



# print [3 6 9 12 15]

count = 1
i = 5

while count <= i:
    print (count * 3 ,end=" ")
    count = count+1


# print [15 12 9 6 3]

count = 5
i = 1

while count >= i:
    print (count * 3 ,end=" ")
    count = count-1



# print [10 8 6 4 2]

count = 5
i = 1

while count >= i:
    print (count * 2 ,end=" ")
    count = count-1



# print [9 7 5 3 1]

count = 9
i = 1

while count >= i:
    print (count ,end=" ")
    count = count-2



# print Multiples of 3 and 5

max = 100
start = 1

while start <= max:
    if start % 3 == 0 and start % 5 == 0:
        print (start)
    start = start + 1   



#  print Multiples of 3 or 5

max = 100
start = 1

while start <= max:
    if start % 3 == 0 or start % 5 == 0:
        print (start)
    start = start + 1  



# print the divisors of given number

givenNumber = 25
start = 1

while start <= givenNumber:
    if givenNumber % start == 0:
        print (start)
    start+=1



# count the divisors of given number

givenNumber = 25
start = 1
total = 0

while start <= givenNumber:
    if givenNumber % start == 0:
        total += 1
    start+=1

print(total)



# check for perfect number

givenNumber = 498
start = 1
total = 0

while start < givenNumber:
    if givenNumber % start == 0:
       total = total + start
    start += 1 

if total == givenNumber:
    print("perfect number")
else :
    print ("not a perfect number")



# print [1 3 5 7 9 2 4 6 8 10]

count = 1

while count <= 10:
    if count <= 5:
        print(2 * count - 1, end=" ")
    else:
        print(2 * (count - 5), end=" ")
    count += 1




# factorial of given number

givenNumber = 4
total = 1

while givenNumber >= 1:
    total = total * givenNumber
    givenNumber -= 1
print (total)




# sum of first n numbers

givenNumber = 5
total = 0

while givenNumber >= 1:
    total = total + givenNumber
    givenNumber -= 1
print (total)


# print first five prime number

givenNumber = 25
start = 1
total = 0

while start <= givenNumber and total < 5:
    newStart = 1
    count = 0

    while newStart <= start:
        if start % newStart == 0:
            count += 1
        newStart +=1

    if count == 2:
        print(start)
        total += 1

    start +=1



# print sum of first five prime number

givenNumber = 100
start = 50
total = 0
primeTotal = 0

while start <= givenNumber and total < 5:
    newStart = 1
    count = 0

    while newStart <= start:
        if start % newStart == 0:
            count += 1
        newStart +=1

    if count == 2:
        primeTotal = primeTotal + start
        total += 1

    start +=1

print(primeTotal)

Enter fullscreen mode Exit fullscreen mode

OUTPUT

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Good Effort!