Skip to content

Commit 558cc22

Browse files
committed
updated python programs
1 parent 5446c3f commit 558cc22

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Python Tutorial/Python.programs.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,47 @@ else:
131131
factorial = factorial*i
132132
print("The factorial of",num,"is",factorial)
133133
```
134+
#### MAX and Min numbers
135+
136+
```python
137+
# Given list of numbers
138+
list = [ 3,2,19,10]
139+
140+
# sorting the given list "list"
141+
# sort() function sorts the list in ascending order
142+
list.sort()
143+
# Displaying the first element of the list
144+
# which is the smallest number in the sorted list
145+
print("list of small number : ",list[0])
146+
print("list of Big number : ",list[-1])
147+
148+
## method 2
149+
list = [ 87,64,78,99,96 ]
150+
print("Maximum number in list : ", max(list))
151+
print("Minimum number in list : ", min(list))
152+
```
153+
154+
#### sum of cubes
155+
An efficient solution is to use direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2
156+
157+
```python
158+
## method 1
159+
def sumofcubes(n):
160+
sum = 0
161+
for i in range (1,n+1):
162+
sum += i*i*i
163+
return sum
164+
n = int(input("enter the value of n: "))
165+
print(sumofcubes(n))
166+
167+
## method 2
168+
# Returns the sum of series with mathematical formula is (n ( n + 1 ) / 2) ^ 2
169+
def sumofcubes(n):
170+
x = (n * (n + 1) / 2)
171+
return (int)(x * x)
172+
173+
# Driver Function
174+
n = int(input("Enter the value of number: "))
175+
print(sumofcubes(n))
176+
177+
```

0 commit comments

Comments
 (0)