Home > Backend Development > Python Tutorial > Day - Looping exercises

Day - Looping exercises

Barbara Streisand
Release: 2024-12-07 09:30:14
Original
284 people have browsed it

Day - Looping exercises

Here are some while loop questions focused on numbers for practice:

Basic Problems

1.Print Numbers
Write a program to print numbers from 1 to 10 using a while loop.

def print_number(no):
    num=1
    while num<=no:
        print(num, end=" ")
        num+=1
print_number(10)

Copy after login
1 2 3 4 5 6 7 8 9 10
Copy after login

2.Sum of N Numbers
Write a program to calculate the sum of the first NN natural numbers using a while loop.

def sum_of_number(no):
    num=1
    total=0
    while num<=no:
        total=total+num
        num+=1
    return total
no=int(input("Sum of the number:"))
print(sum_of_number(no))
Copy after login
Sum of the number:10
55
Copy after login

3.Even Numbers
Write a program to print all even numbers between 1 and 50 using a while loop.

def print_even_number(no):
    num=2
    while num<=no:
        print(num, end=" ")
        num+=2
print_even_number(50)

Copy after login
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 
Copy after login

4.Odd Numbers
Write a program to print all odd numbers between 1 and NN.

def print_odd_number(no):
    num=1
    while num<=no:
        print(num, end=" ")
        num+=2
    return no
no=int(input("Enter the number:"))
print_odd_number(no)
Copy after login
Enter the number:20
1 3 5 7 9 11 13 15 17 19
Copy after login

5.Reverse Count
Write a program to print numbers from 20 to 1 in reverse order using a while loop.

def print_reverse_number(no):
    num=20
    while num>=no:
        print(num, end=" ")
        num-=1
print_reverse_number(1)
Copy after login
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
Copy after login

Intermediate Problems

1.Factorial Calculation
Write a program to calculate the factorial of a given number using a while loop.

def find_factorial(num):
    no=1
    factorial=1
    while no<=num:
        factorial=factorial*no
        no+=1
    return factorial
num=int(input("Enter the number:"))
print(find_factorial(num))
Copy after login
Enter the number:5
120
Copy after login
  1. Sum of Digits Write a program to find the sum of the digits of a given number (e.g., 123 → 1 2 3=6).
def sum_of_digits(num):
    sum=0
    while num>0:
        sum=sum+num%10
        num=num//10
    return sum
num=int(input("Enter the number:"))
print(sum_of_digits(num))
Copy after login
Enter the number:123
6
Copy after login

3.Count Digits
Write a program to count the number of digits in a given number (e.g., 12345 → 5 digits).

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count
num=int(input("Enter the number:"))
print(count_of_digits(num))

Copy after login
Enter the number:12345
5
Copy after login

4.Reverse a Number
Write a program to reverse a given number (e.g., 123 → 321).

def reverse_number(num):
    reverse=0
    while num>0:
        reverse=reverse*10+num%10
        num=num//10
    return reverse
num=int(input("Enter the number:"))
print(reverse_number(num))
Copy after login
Enter the number:123
321
Copy after login

5.Multiplication Table
Write a program to print the multiplication table of a given number nn using a while loop.

def multiply(num):
    no=1
    while no<=15:
        print(no,"*",num,"=",no*num , end="\n")
        no+=1
num=int(input("Enter the number:"))
multiply(num)
Copy after login
Enter the number:12
1 * 12 = 12
2 * 12 = 24
3 * 12 = 36
4 * 12 = 48
5 * 12 = 60
6 * 12 = 72
7 * 12 = 84
8 * 12 = 96
9 * 12 = 108
10 * 12 = 120
11 * 12 = 132
12 * 12 = 144
13 * 12 = 156
14 * 12 = 168
15 * 12 = 180
Copy after login

Advanced Problems

1.Check Palindrome
Write a program to check if a given number is a palindrome (e.g., 121 → palindrome, 123 → not a palindrome).

def palindrome(num):
    count=0
    while num>0:
        count=count*10+num%10
        num=num//10
    return count
num=int(input("Enter the number:"))
result=palindrome(num)
if result==num:
    print("Palindrome")
else:
    print("Not palindrome")

Copy after login
Enter the number:121
Palindrome
Enter the number:123
Not palindrome

Copy after login

*2.Find the power *

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result
base=int(input("Enter the base number:"))
power=int(input("Enter the power number:"))
result=find_power(base,power)
print(result)

Copy after login
Enter the base number:2
Enter the power number:5
32
Copy after login

3.Armstrong Number
Write a program to check if a given number is an Armstrong number (e.g., 153 → 13 53 33=15313 53 33=153).

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result

def find_armstrong(num,count):
    armstrong=0
    while num>0:
        rem=num%10
        result= find_power(rem,count)
        armstrong=armstrong+result
        num=num//10
    return armstrong


num=int(input("Enter the number:"))
count=count_of_digits(num)
armstrong_result=find_armstrong(num,count)

if armstrong_result==num:
    print("Armstrong")
else:
    print("Not armstrong")
Copy after login
Enter the number:123
Not armstrong
Enter the number:153
Armstrong

Copy after login

4.Sum of even and odd position:

def sum_of_even_odd(num):
    odd=0
    even=0
    index=0
    while index<len(num):
        digit=int(num[index])
        if index%2==0:
            even=even+digit

        else:
            odd=odd+digit

        index+=1
    return even,odd

num=input("Enter the number:")
even,odd=sum_of_even_odd(num)
print("sum of even number:",even)
print("sum of odd number:",odd) 

Copy after login
Enter the number:12345
sum of even number: 9
sum of odd number: 6

Copy after login

The above is the detailed content of Day - Looping exercises. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template