Program to find the sum of first 10 natural numbers using for loop.

Code:

num = int(input("Enter a number: "))

if num < 0:

  print("Enter a positive number")

else:

   sum = 0

   # use while loop to iterate until zero

   while(num > 0):

       sum += num

       num -= 1

   print("The sum is", sum)

 

Output:

Comments

Popular posts from this blog

State use of pep and pip