search

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





<pre class="brush:php;toolbar:false">1 2 3 4 5 6 7 8 9 10

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





<pre class="brush:php;toolbar:false">Sum of the number:10
55

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





<pre class="brush:php;toolbar:false">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 

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

def print_odd_number(no):
    num=1
    while num





<pre class="brush:php;toolbar:false">Enter the number:20
1 3 5 7 9 11 13 15 17 19

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)
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

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





<pre class="brush:php;toolbar:false">Enter the number:5
120
  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))
Enter the number:123
6

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))

Enter the number:12345
5

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))
Enter the number:123
321

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





<pre class="brush:php;toolbar:false">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

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")

Enter the number:121
Palindrome
Enter the number:123
Not palindrome

*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)

Enter the base number:2
Enter the power number:5
32

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")
Enter the number:123
Not armstrong
Enter the number:153
Armstrong

4.Sum of even and odd position:

def sum_of_even_odd(num):
    odd=0
    even=0
    index=0
    while index<len digit="int(num[index])" if index even="even+digit" else: odd="odd+digit" return num='input("Enter' the number: print of>





<pre class="brush:php;toolbar:false">Enter the number:12345
sum of even number: 9
sum of odd number: 6

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

Statement
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
What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

What are unit tests in Python?What are unit tests in Python?Apr 30, 2025 pm 02:05 PM

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

What are Access Specifiers in Python?What are Access Specifiers in Python?Apr 30, 2025 pm 02:03 PM

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

What is __init__() in Python and how does self play a role in it?What is __init__() in Python and how does self play a role in it?Apr 30, 2025 pm 02:02 PM

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

What is the difference between @classmethod, @staticmethod and instance methods in Python?What is the difference between @classmethod, @staticmethod and instance methods in Python?Apr 30, 2025 pm 02:01 PM

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool