0% found this document useful (0 votes)
2 views4 pages

Python Notes

The document contains Python code snippets demonstrating various programming concepts such as generating natural numbers, calculating sums, finding factorials, counting digits, reversing numbers, creating multiplication tables, checking for prime numbers, finding the LCM of two numbers, and generating Fibonacci series. Each concept is illustrated using both 'while' and 'for' loops. The code is structured to provide practical examples for each mathematical operation.

Uploaded by

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

Python Notes

The document contains Python code snippets demonstrating various programming concepts such as generating natural numbers, calculating sums, finding factorials, counting digits, reversing numbers, creating multiplication tables, checking for prime numbers, finding the LCM of two numbers, and generating Fibonacci series. Each concept is illustrated using both 'while' and 'for' loops. The code is structured to provide practical examples for each mathematical operation.

Uploaded by

codelab353
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# first n natural numbers

# using while

n = 1
while n <=9:
n +=1
print(n)

# using for

n = 0
for i in range(1 , 11):
n = 1 + i
print(n)

# sum of first n natural numbers

# using while

a = 0
value = 0
while a <= 10:
value += a
a +=1
print(value)

# using for

a = 0
for i in range (1, 11):
a += i
print(a)

# print first 10 odd natural numbers

a = 0

while a <20:
a +=1
if a%2 !=0:
print(a)

a=0
for i in range(0, 21):
a += 1
if a%2 != 0:
print(a)

# print sum of first n natural numbers


sum = 0
for i in range ( 0, 11 ):
sum += i
print(sum)

sum = 0
i = 0
while i<=10:
sum += i
i += 1
print(sum)

# factoral of n numbers

sum = 1
for i in range ( 1, 6 ):
sum *= i
print(sum)

sum = 1
i = 1
while i<=5:
sum *= i
i += 1
print(sum)

# COUNT DIGITS IN A GIVEN NUMBER

a = 123
count = 0
while a !=0:
a//=10
count +=1
print (count)

# print sum of digits

a = 987
add = 0
while a !=0:
c=a%10
a//=10
add += c
print(add)

# reverse of given number

a = 687
rev = 0
while a !=0:
c = a%10
rev=(rev * 10) + c
a //= 10
print(rev)

# multiplication table of any number

n = int(input("enter your number:"))


a = 1
while a <=10:
if n != 0:
print(n*a)
a+=1

n = int(input("enter your number:"))


for a in range(1,11):
if n != 0:
print(n*a)

# To check prime number

num = int(input("enter your number:"))


for i in range (2 , num ):
if num%i == 0:
print(" not a prime number")
break
else:
print("prime number")

num = 12
i = 2
while i < num:
if num%i == 0:
print("Not a prime number")
break
i +=1
else:
print("prime number")

# LCM of two number

n1 = 12
n2 = 7
if n1>n2:
max=n1
else:
max=n2
while True:
if max%n1==0 and max%n2==0:
print(max)
break
max+=1

# fibonacci series

num = 7
n1=0
n2=1
sum = 0
if num<=0:
print("ERroR")
else:
for i in range(0,num):
print(sum , end=" ")
n1=n2
n2=sum
sum = n1 + n2

num = 7
n1=0
n2=1
sum = 0
count=0
if num<=0:
print("ERroR")
else:
while count < num:
print(sum , end=" ")
n1=n2
n2=sum
sum = n1 + n2
count +=1

You might also like