You are on page 1of 4

5/12/22, 4:48 PM CS Project - Jupyter Notebook

1) Write a program to input the value of x and n and print the sum
of the following series:
i) 1+x+x^2+x^3+......x^n

ii) 1-x+x^2-x^3+......x^n

iii) x + x^2/2 + x^3/3 +..... x^n/n

iv) x + x ^2/2! + x^3/3! +..... x^n/n!

1) 1+x+x^2+x^3+......x^n

In [4]:

x = float(input('Enter the value of x: '))


n = int(input('Enter the value of n: '))
sum_1 = 0
a=0
while a<=n:
sum_1 += x**a
a += 1
print()
print('Sum = ', sum_1)

Enter the value of x: 2

Enter the value of n: 4

Sum = 31.0

2) 1-x+x^2-x^3+......x^n

In [5]:

x = float(input('Enter the value of x: '))


n = int(input('Enter the value of n: '))
sum_2 = 0
for i in range(0,n+1,2):
sum_2 += x**i
for j in range(1,n+1,2):
sum_2 += -(x**j)
print('Sum = ', sum_2)

Enter the value of x: 2

Enter the value of n: 4

Sum = 11.0

localhost:8888/notebooks/CS Project.ipynb# 1/5


5/12/22, 4:48 PM CS Project - Jupyter Notebook

3) x + x^2/2 + x^3/3 +..... x^n/n

In [1]:

x = float(input('Enter the value of x: '))


n = int(input('Enter the value of n: '))
sum_3 = 0
b=1
while b<=n:
sum_3 += x**b/b
b+=1
print('Sum = ', sum_3)

Enter the value of x: 2

Enter the value of n: 4

Sum = 10.666666666666666

iv) x + x ^2/2! + x^3/3! +..... x^n/n!

In [1]:

x = float(input('Enter the value of x: '))


n = int(input('Enter the value of n: '))
sum_4 = 0
c=1
while c<=n:
d=1
for p in range(1,c+1):
d *= p
sum_4 += x**c/d
c += 1
print('Sum = ', sum_4)

Enter the value of x: 2

Enter the value of n: 4

Sum = 6.0

2) Write a program to check if a year is a leap year or not.

localhost:8888/notebooks/CS Project.ipynb# 2/5


5/12/22, 4:48 PM CS Project - Jupyter Notebook

In [3]:

y = int(input('Enter a year to check if it is a leap year or not: '))


if y%100==0:
if y%400==0:
print(y, 'is a leap year.')
else:
print(y, 'is not a leap year.')
else:
if y%4==0:
print(y, 'is a leap year.')
else:
print(y, 'is not a leap year.')

Enter a year to check if it is a leap year or not: 1998

1998 is not a leap year.

3) Ask user to enter age, sex(M or F), marital status(Y or N) and


then using following rules print their place of service:
• If the employee is female, then she'll work only in urban areas.

• If the employee is a male and age is in between 20-40, then he may work anywhere,

• If the employee is male and age is between 40-60 then he'll work in urban areas only,

• And any other input of age should print 'ERROR'

In [14]:

print('Kindly enter your details below.')


a = int(input('Enter your age: '))
b = input('Enter sex(M or F): ')
c = input('Are you married?(Y or N): ')
if b.upper()=='F' and a in range(20,61):
print('Your Place of Service is in Urban area. ')
elif b.upper()=='M':
if a in range(20,40):
print('Your Place of Service might be anywhere in Rural or Urban area. ')
elif a in range(40,61):
print('Your Place of Service is in Urban area. ')
else:
print('ERROR! Invalid Input.')
else:
print('ERROR! Invalid Input.')

Kindly enter your details below.

Enter your age: 30

Enter sex(M or F): M

Are you married?(Y or N): N

Your Place of Service might be anywhere in Rural or Urban area.

localhost:8888/notebooks/CS Project.ipynb# 3/5


5/12/22, 4:48 PM CS Project - Jupyter Notebook

4) Take integer inputs from the user until he/she presses q (Ask to
press q to quit after every integer input). Print average and
product of all numbers.
In [9]:

a = []
b=0
while b!='q':
i = int(input('Enter a number: '))
b = input('Do u want to quit?(Press q if yes): ')
a.append(i)
avg = sum(a)/len(a)
product = 1
for x in a:
product*=x
print()
print('Average of given numbers: ', avg)
print('Product of given numbers: ', product)

Enter a number: 34

Do u want to quit?(Press q if yes): NO

Enter a number: 45

Do u want to quit?(Press q if yes): NO

Enter a number: 24

Do u want to quit?(Press q if yes): NO

Enter a number: 96

Do u want to quit?(Press q if yes): NO

Enter a number: 35

Do u want to quit?(Press q if yes): q

Average of given numbers: 46.8

Product of given numbers: 123379200

5) Calculate the sum of the digits of a number given by the


user.
In [10]:

a = int(input('Enter a number: '))


sum = 0
while a>0:
sum += a%10
a = a//10
print('Sum of digits= ',sum)

Enter a number: 14736

Sum of digits= 21

localhost:8888/notebooks/CS Project.ipynb# 4/5

You might also like