-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignment 11.py
More file actions
111 lines (75 loc) · 2.08 KB
/
Assignment 11.py
File metadata and controls
111 lines (75 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Assignment - 11
# 1. Write a python script to calculate sum of first N natural numbers
N=int(input('Enter the value of N:\t'))
sum=0
while N:
sum=sum+N
N-=1
print('The sum is:',sum)
# 2. Write a python script to calculate sum of squares of first N natural numbers
N=int(input('Enter the value of N:\t'))
sum=0
while N:
sum=sum+(N**2)
N-=1
print('The sum is:',sum)
# 3. Write a python script to calculate sum of cubes of first N natural numbers
N=int(input('Enter the value of N:\t'))
sum=0
while N:
sum=sum+(N**3)
N-=1
print('The sum is:',sum)
# 4. Write a python script to calculate sum of first N odd natural numbers
N=int(input('Enter the value of N:\t'))
sum=0
N=(2*N)-1
while N>0:
sum=sum+N
N-=2
print('The sum is:',sum)
# 5. Write a python script to calculate sum of first N even natural numbers
N=int(input('Enter the value of N:\t'))
sum=0
N=(2*N)
while N>0:
sum=sum+N
N-=2
print('The sum is:',sum)
# 6. Write a python script to calculate factorial of a given number
n=int(input("Enter the number:\t"))
f=1
for r in range(1,n+1):
f=f*r
print("the factorial of given number is:",f)
# 7. Write a python script to count digits in a given number
num=int(input('Enter a number:\t'))
len=0
while num!=0:
len+=1
num=num//10
print('The length of the number is:',len)
# 8. Write a python script to calculate sum of digits of a given number
num=int(input('Enter a number:\t'))
l=0
while num!=0:
l=l+(num%10)
num=num//10
print('The sum of the digits is:',l)
# 9. Write a python script to print binary equivalent of a given decimal number. (do not use bin() method).
# decimal to binary convert
n=int(input('Enter the number:\t'))
re=""
while n>0:
re=re+str(n%2)
n//=2
re=re[::-1]
print(re)
# 10. Write a python script to print the octal equivalent of a given decimal number. (do not use oct() method)
n=int(input('Enter the number:\t'))
re=""
while n>0:
re=re+str(n%8)
n//=8
re=re[::-1]
print(re)