Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chauhan-tarun/36a68fa64c96a770a016570ee11b4fcc to your computer and use it in GitHub Desktop.
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
# 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