0% found this document useful (0 votes)
49 views7 pages

Practical Assignment-3

The document contains 12 programs demonstrating the use of for and while loops in Python. The programs include: (1) printing odd numbers from 1 to 100 using a for loop; (2) calculating the sum of even numbers from 1 to 100 using a for loop; and (3) displaying a fibonacci series from 1 to 100 using a while loop. The programs cover basic looping constructs, calculations, input/output, and pattern generation.

Uploaded by

Vishesh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Practical Assignment-3

The document contains 12 programs demonstrating the use of for and while loops in Python. The programs include: (1) printing odd numbers from 1 to 100 using a for loop; (2) calculating the sum of even numbers from 1 to 100 using a for loop; and (3) displaying a fibonacci series from 1 to 100 using a while loop. The programs cover basic looping constructs, calculations, input/output, and pattern generation.

Uploaded by

Vishesh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Science Assignment-3

✓ For Loop:
a) Program-1
#1. Odd numbers from 1 to 100.
for i in range(1,101,2):
print(i,end=" ")

Output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89
91 93 95 97 99

b) Program-2
#2. Sum of even numbers from 1 to 100.
sum=0
for i in range(2,101,2):
sum+=i
print("The sum is",sum,".")

Output:
The sum is 2550 .

c) Program-3
#3. Display factorial of a number.
factorial=1
num=int(input("Enter the number to find factorial: "))
for n in range(num,1,-1):
if n!=0:
factorial*=n
print("The factorial of given number is",factorial,".")
Output:
Enter the number to find factorial: 9
The factorial of given number is 362880 .

d) Program-4
#4. Display Fibonacci series.
n=int(input('Enter the range for Fibonacci series: '))
if n<=0:
print("invalid input.")
elif n==1:
print("Fibonacci series: 0")
else:
list1=[0,1]
v1,v2=0,1
for i in range(n-2):
v3=v1+v2
v1=v2
v2=v3
list1.append(v3)
print("Fibonacci Series :",list1)

Output:
Enter the range for fibonacci series: 9
Fibonacci Series : [0, 1, 1, 2, 3, 5, 8, 13, 21]

e) Program-5
#5. Display table of any number.
num=int(input("Enter the number whose table you want: "))
for m in range(1,11):
print(num,'x',m,'=',num*m)

Output:
Enter the number whose table you want: 19
19 x 1 = 19
19 x 2 = 38
19 x 3 = 57
19 x 4 = 76
19 x 5 = 95
19 x 6 = 114
19 x 7 = 133
19 x 8 = 152
19 x 9 = 171
19 x 10 = 190

f) Program-6
#6. (i) 2 4 6 8 10
# (ii) 5 10 15 20 25 30
# (iii) -10 -7 -4 -1
for i in range(1,6):
print(i*2,end=" ")
print()
for i in range(1,7):
print(i*5,end=" ")
print()
for i in range(-10,0,3):
print(i,end=" ")
print()

Output:
2 4 6 8 10
5 10 15 20 25 30
-10 -7 -4 -1

g) Program-7
#7. Sum of squares of first 100 natural numbers.
sum=0
for i in range(1,101):
sum+=i**2
print('The sum is',sum,'.')

Output:
The sum is 338350 .
h) Program-8
#8. Generate the sequence: -5 10 -15 20 -25 30 ... upto n.
n=int(input("Enter the limit for the sequence: "))
for i in range(5,n+1,5):
if i%2==0:
print(i*1, end=" ")
else:
print((i*-1),end=" ")

Output:
Enter the limit for the sequence: 50
-5 10 -15 20 -25 30 -35 40 -45 50

i) Program-9
#9. Menu based program for calculating area and circumference.
radius=eval(input("Enter the radius of circle: "))
choice=input("Enter 1 for circumference, \n 2 for Area \n Your Choice: ")
if choice=='1':
circumference=2*(22/7)*radius
print("The circumference of given circle is :",circumference)
elif choice=='2':
area=(22/7)*radius**2
print("The area of given circle is :",area)

Output:
Enter the radius of circle: 14
Enter 1 for circumference,
2 for Area
Your Choice: 2
The area of given circle is : 616.0

j) Program-10
#10. Factor of n.
num=int(input("Enter the number to find factors: "))
factors=''
for i in range(2,num+1):
if num%i == 0:
factors=factors+str(i)+', '
# num=num/i
factors=factors+'1'
print('Factors are :',factors)

Output:
Enter the number to find factors: 600
Factors are : 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 25, 30, 40, 50, 60, 75,
100, 120, 150, 200, 300, 600, 1

k) Program-11
#11. Program for pattern.
for i in range(1,7):
print("*"*i)
print()
for i in range(5,0,-1):
print("*"*i)

Output:
*
**
***
****
*****
******

*****
****
***
**
*

✓ While Loop:
a) Program-1
#1. Display sum of digits of a number.
num=input("Enter your number: ")
i=0
sum=0
while i<len(num):
sum+=int(num[i])
i+=1
print('The sum of digits is',sum)

Output:
Enter your number: 354
The sum of digits is 12

b) Program-2
#2. Display product of digits of a number.
num=input("Enter your number: ")
i=0
pdt=1
while i<len(num):
pdt*=int(num[i])
i+=1
print("The product of digits is",pdt)

Output:
Enter your number: 525
The product of digits is 50

c) Program-3
#3. Display reverse order of digits of a number.
num=input("Enter your number: ")
i=1
reverse=""
while i<len(num)+1:
reverse+=num[-i]
i+=1
print('The reverse order is',reverse)
Output:
Enter your number: 6709
The reverse order is 9076

d) Program-4
#4. Display fibonacci series from 1 to 100.
count=0
v1,v2=0,1
fibonacci=[0,1]
while count<=100:
if count==v1+v2:
v1=v2
v2=count
fibonacci.append(count)
count+=1
print(fibonacci)

Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

You might also like