Created
April 8, 2018 09:19
-
-
Save chauhan-tarun/36a68fa64c96a770a016570ee11b4fcc to your computer and use it in GitHub Desktop.
Exercise answers of video tutorial "Python 3 for beginners - #7. While Loop" by TheTechnicalFist : https://youtu.be/PBToc_O7OzY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Write a program to print all odd numbers Between 1 to 100. | |
# Take count | |
count = 1 | |
# Run while loop from 1 to 100 | |
while count <= 100: | |
# Check if number is odd and print it in console | |
if count % 2 != 0: | |
print(count) | |
# Increase count to prevent infinite loop | |
count += 1 | |
# 2. Write a program to print multiplication table of any number. | |
# Take num from User | |
num = eval(input("Enter a number : ")) | |
# Take count | |
count = 1 | |
# Run while loop from 1 to 10 | |
while count <= 10: | |
# Print the table | |
print(num, " X ", count, " = ", num * count) | |
# Increase count | |
count += 1 | |
# 3. Write a program to find the sum of first `n` digits. (Take `n` from user). | |
# Take input from user | |
n = eval(input("Enter a number : ")) | |
# Take a variable to store sum | |
sumOfNNumber = 0 | |
# Run while loop from 1 to input value | |
while n > 0: | |
# Add N into sum | |
sumOfNNumber += n | |
# Reduce n to prevent infinite loop | |
n -= 1 | |
# Print sum of numbers | |
print(sumOfNNumber) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment