# Write a Python Program to Find the Sum of Natural Numbers - Python

Categories:
Viewed: 3 - Published at: a few seconds ago

### Write a Python Program to Find the Sum of Natural Numbers

``````num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
``````

### Write a python program to Shuffle Deck of Cards

``````import itertools, random
random.shuffle(deck)
print(deck)
``````

### Write a Python function to Convert Decimal to Binary

``````def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')
``````

# wrtie a python function to solve Tower Of Hanoi and print necessary statements

``````def TowerOfHanoi(n , source, destination, auxiliary):
if n==1:
print("Move disk 1 from source",source,"to destination",destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print("Move disk",n,"from source",source,"to destination",destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
``````

### Write a python function to find the number of times every day occurs in a Year and print them

``````import datetime
import calendar

def day_occur_time(year):
days = [ "Monday", "Tuesday", "Wednesday",
"Thursday",  "Friday", "Saturday",
"Sunday" ]
L = [52 for i in range(7)]

pos = -1
day = datetime.datetime(year, month = 1, day = 1).strftime("%A")
for i in range(7):
if day == days[i]:
pos = i
if calendar.isleap(year):
L[pos] += 1
L[(pos+1)%7] += 1
else:
L[pos] += 1

for i in range(7):
print(days[i], L[i])
``````

### Write a python Program to Determine all Pythagorean Triplets in the Range

``````limit= 50
c=0
m=2
while(c<limit):
for n in range(1,m+1):
a=m*m-n*n
b=2*m*n
c=m*m+n*n
if(c>limit):
break
if(a==0 or b==0 or c==0):
break
print(a,b,c)
m=m+1
``````

# function to Convert Binary to Gray Code

def binarytogray(n): n = int(n, 2) n ^= (n >> 1) return bin(n)[2:]

``````### Write a Python function to Find the Intersection of Two Lists
``````

def intersection(a, b): return list(set(a) & set(b)) ```