Q2:- Write the python programs for the following problems (using for loop)
a) To display the series of numbers from 2 to 20.
b) To display the series of odd numbers from 30 to 1 in reverse order.
c) To display the multiplication table of a given number.
d) To display the sum of first 30 natural numbers.
e) To display a message “hello” 20 times on the screen.
Answers
go to github they will help u :)
a)To display the series of numbers from 2 to 20.
b) To display the series of odd numbers from 30 to 1 in reverse order.
c) To display the multiplication table of a given number.
d) To display the sum of the first 30 natural numbers.
e) To display a message “hello” 20 times on the screen.
Explanation:
1. start, end = 5, 20
# iteration
for num in range(start, end + 1):
# check
if num % 2 == 0:
print(num, end = " ")
2. sum=0
for n in range(1,30):
if n%2!=0:
while n!=0:
digit=n%10
sum=sum*10+digit
n=n//10
print(sum)
3. number = int(input ("Enter the number of which the user wants to print the multiplication table: "))
count = 1.
# we are using while loop for iterating the multiplication 10 times.
print ("The Multiplication Table of: ", number)
while count <= 10:
number = number * 1.
print (number, 'x', i, '=', number * count)
4. num = 30
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)
5. for I in 1 .. 20 loop
Put_Line ("Hello");
end loop;