0% found this document useful (0 votes)
12 views

Practical Assignment-While+For Loop

The document contains 8 programming questions that involve using while and for loops to solve problems. The questions include writing programs to: 1) Find the sum of a squared number series using a while loop 2) Find the factorial of a number using a while loop 3) Display the reverse of a number entered using a while loop 4) Find the sum of digits in a 5 digit number using a while loop 5) Display the table of a number up to a limit using a while loop 6) Find the sum of an odd number series using a for loop 7) Find the sum of user-defined number of numbers using a for loop 8) Print a descending number series using a for loop

Uploaded by

Apoorva Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Practical Assignment-While+For Loop

The document contains 8 programming questions that involve using while and for loops to solve problems. The questions include writing programs to: 1) Find the sum of a squared number series using a while loop 2) Find the factorial of a number using a while loop 3) Display the reverse of a number entered using a while loop 4) Find the sum of digits in a 5 digit number using a while loop 5) Display the table of a number up to a limit using a while loop 6) Find the sum of an odd number series using a for loop 7) Find the sum of user-defined number of numbers using a for loop 8) Print a descending number series using a for loop

Uploaded by

Apoorva Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical Assignment : Loops

Q1) Write a program to find sum of series 1, 4, 9, 16……,n


using while loop.
ANS - n = int(input("Enter the value of n: "))
sum_of_series = 0
i=1
while i <= n:
term = i ** 2
sum_of_series += term
i += 1
print(f"The sum of the series 1^2 + 2^2 + 3^2 + ... + {n}^2 is:
{sum_of_series}")
Q2) Write a program to find Factorial of a given number using
while loop.
ANS number = int(input("Enter a number to find its factorial:
"))
factorial = 1
i=1
while i <= number:
factorial *= i
i += 1
print(f"The factorial of {number} is: {factorial}")
Q3) Write a program to display reverse of the number entered
using while loop.
For example, if user enters 123, the program should output 321.
ANS number = int(input("Enter a number to display its reverse:
"))
reverse_number = 0
while number > 0:
digit = number % 10
reverse_number = (reverse_number * 10) + digit
number = number // 10
print(f"The reverse of the entered number is:
{reverse_number}")
Q4) Write a program to find sum of digits of a 5 five digit
number using while loop.

Hint: While(n>0)
Rem=n%10
Sum=sum+Rem
N=N//10
Hint:
Rev=0
While(n>0)
Rem=n%10 1 2 3
Rev=rev*10+rem 1 12 123
N=N//10 32 3 0
ANS number = int(input("Enter a five-digit number: "))
while not (10000 <= number <= 99999):
print("Invalid input! Please enter a five-digit number.")
original_number = number
sum_of_digits = 0
while number > 0:
digit = number % 10
sum_of_digits += digit
number = number // 10
print(f"The sum of digits of {original_number} is:
{sum_of_digits}")
Q5) Write a program to display the table of a number upto m
using while loop where n and m will be user defined variables.
number = int(input("Enter the number for which you want to
display the table: "))
limit = int(input("Enter the limit (m) for the table: "))
i=1
while i <= limit:
result = number * i
print(f"{number} * {i} = {result}")
i += 1
6) Write a program to find sum of series 1+3+5+9+11 using for
loop.
ANS sum_of_series = 0
for i in range(1, 12, 2):
sum_of_series += i
print(f"The sum of the series 1 + 3 + 5 + 7 + 9 is:
{sum_of_series}")
7) Write a program to find sum of n user defined numbers using
for loop.
ANS n = int(input("Enter the value of n: "))
sum_of_numbers = 0
for _ in range(n):
num = float(input("Enter a number: "))
sum_of_numbers += num
print(f"The sum of {n} numbers is: {sum_of_numbers}")
8) Write a program to to print the following series using for
loop.
10,9,8,7,6,5,4,3,2,1
Ans for i in range(10, 0, -1):
print(i, end=" ")

You might also like