Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Lab Report, 10 Problems On Loop of Python

The document contains 10 code examples demonstrating the use of for loops in Python to: 1) print the first 10 natural numbers; 2) print even numbers within a given range; 3) calculate the sum of numbers from 1 to a given number; 4) calculate the sum of odd numbers within a range; 5) print a multiplication table; 6) print items in a list; 7) count digits in a number; 8) check if a string is a palindrome; 9) reverse a word; and 10) check if a number is an Armstrong number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab Report, 10 Problems On Loop of Python

The document contains 10 code examples demonstrating the use of for loops in Python to: 1) print the first 10 natural numbers; 2) print even numbers within a given range; 3) calculate the sum of numbers from 1 to a given number; 4) calculate the sum of odd numbers within a range; 5) print a multiplication table; 6) print items in a list; 7) count digits in a number; 8) check if a string is a palindrome; 9) reverse a word; and 10) check if a number is an Armstrong number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Printing 1st 10 natural numbers:


n=10
for i in range(1,n+1):
print(i)

2. Printing even numbers within the given range:


n=int(input("Ending even number of the range? "))
for i in range(0,n+1,2):
print(i)

3. Calculating sum of all numbers from 1 to given number:


n=int(input("Ending number of the range?(1-?) "))
sum=0
for i in range(1, n+1):
sum+=i
print(sum)

4. Calculating the sum of all the odd numbers:


n=int(input(“Ending odd number of the range? “))
sum=0
for i in range(1,n+1,2):
sum+=i
print(sum)

5. Multiplication table:
n=int(input("Enter number "))
for i in range(1,11):
print(n,"X",i,"=",n*i)
6. Displaying list’s numbers:
list=[1,5,7,10]
for i in list:
print(i)

7. Counting the total number of digits in a number:


n=str(input("Number? "))
count=0
for i in n:
count+=1
print(count)

8. Checking a string if it is palindrome:


string=str(input("Enter the string "))
reversed_string=""
for i in string:
reversed_string=i+reversed_string
if(string==reversed_string):
print("The string", string, "is a Palindrome")
else:
print("The string", string, "is not a Palindrome")

9. Reversing a word:
word=str(input("Which word you want to reverse? "))
reversed_word=""
for i in word:
reversed_word=i+reversed_word
print(reversed_word)

10. Checking a number if it is an Armstrong number:


number = input("Enter number ")
length = len(number)
sum = 0
for i in number:
sum += int(i) ** length
if sum == int(number):
print("The given number", number, "is an Armstrong Number")
else:
print("The given number", number, "is not an Armstrong Number")

You might also like