Practical Program Exercise 1 Calculate Factorial and Sum of Series for 12th Computer Science
Looping
- we may need to execute a set of statements multiple times, called iteration or looping.
for loop
- for loop is the most comfortable loop.
- It is also an entry check loop.
- The condition is
checked in the beginning and the body of the loop(statements-block 1) is executed if it is only True otherwise the loop is not executed.
PY 1(a) – CALCULATE FACTORIAL
QUESTION:
Write a program to calculate the factorial of the given number using for loop.
AIM:
To calculate the factorial of the given number using for loop.
CODING:
num = int(input(“Enter the number:”))
if(num==0):
fact = 1
fact = 1
for i in range(1, num+1):
fact = fact*i
print(“Factorial of “, num,” is”, fact)
OUTPUT:
Enter a Number: 0
Factorial of 0 is 1
Enter a Number: 12
Factorial of 12 is 479001600
RESULT:
The Output was verified successfully
PY 1(b) – SUM OF SERIES
QUESTION:
Write a program to sum the series: 1/1 + 22/2 + 33/3 + ……. nn/n
AIM:
To calculate the sum of the series: 1/1 + 22/2 + 33/3 + ……. nn/n
CODING:
num = int(input(“Enter a value of n:”))
s=0.0
for i in range(1,num+1):
a=float(i**i)/i
s=s+a
print(“The sum of the series is”, s)
OUTPUT:
Enter a value of n: 4
The sum of the series is 76.0
RESULT:
The Output was verified successfully