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)
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<=no: total=total+num num+=1 return total no=int(input("Sum of the number:")) print(sum_of_number(no))
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<=no: print(num, end=" ") num+=2 print_even_number(50)
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<=no: print(num, end=" ") num+=2 return no no=int(input("Enter the number:")) print_odd_number(no)
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<=num: factorial=factorial*no no+=1 return factorial num=int(input("Enter the number:")) print(find_factorial(num))
Enter the number:5 120
- 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<=15: print(no,"*",num,"=",no*num , end="\n") no+=1 num=int(input("Enter the number:")) multiply(num)
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(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)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
