1
2
3
While loop statement - Ex13_1
Save the file as "XX_YYYY_Ex13_1.py"
Summing the Digits of a Number
Write a Python program that asks the user to input a number and then calculates the sum of its digits.
Prompt the user to enter a number.
Extract each digit from the number entered by the user.
Sum all the digits of the number.
Print the result of the sum
Input:
123
Output:
6
Example
While loop statement - Ex13_2
Save the file as "XX_YYYY_Ex13_2.py"
Count the Number of Twos in an Input Number
Create a Python script to count the occurrences of the digit "2" in a user-provided integer. Display the count to the user.
Prompt the user to enter a number.
Iterate through each digit of the number.
Count how many times the digit "2" appears.
Print the counting number.
Input:
2024
Output:
2
Example
While loop statement - Ex13_3
Save the file as "XX_YYYY_Ex13_3.py"
The Largest Digit of the Entered Number
Create a Python script to find the maximum digit of a number entered by a user.
Prompt the user to enter a number.
Iterate through each digit of the number.
Find the largest digit of the entered number.
Print the largest number.
Input:
2024
Output:
4
Example
While loop statement - Ex13_4
Save the file as "XX_YYYY_Ex13_4.py"
The Second Largest Digit of the Entered Number
Create a Python script to find the second largest digit of a number entered by a user.
Prompt the user to enter a number.
Iterate through each digit of the number.
Find the second largest digit of the entered number.
Print the second largest number.
Input:
20391
Output:
3
Example