0% found this document useful (0 votes)
7 views8 pages

Python Docx1

The document contains a series of Python programming assignments that cover various topics such as calculating the sum of digits, checking for Armstrong and perfect numbers, calculating powers, checking for palindromes, counting even, odd, and zero digits, converting binary to decimal, checking number ranges, displaying prime numbers, and generating patterns. Each assignment includes a brief description followed by the corresponding Python code implementation. The assignments are organized into three sets (A, B, and C) with multiple tasks in each set.

Uploaded by

www.rushidhuri
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)
7 views8 pages

Python Docx1

The document contains a series of Python programming assignments that cover various topics such as calculating the sum of digits, checking for Armstrong and perfect numbers, calculating powers, checking for palindromes, counting even, odd, and zero digits, converting binary to decimal, checking number ranges, displaying prime numbers, and generating patterns. Each assignment includes a brief description followed by the corresponding Python code implementation. The assignments are organized into three sets (A, B, and C) with multiple tasks in each set.

Uploaded by

www.rushidhuri
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
You are on page 1/ 8

Python

Assignment No :- 1
SET :- A
1)Write python script to calculate sum of digits of a given input number.

def calculate_sum_of_digits(num):
sum = 0
for digit in str(num):
sum += int(digit)
return sum
number = input("Enter a number: ")
sum_of_digits = calculate_sum_of_digits(number)
print(sum_of_digits)

2) Write python script to check whether a input number is Armstrong


number or not.

def check_armstrong(num):
temp = num
sum = 0
while temp > 0:
rem = temp % 10
sum += rem ** 3
temp //= 10
if sum == num:
return "Armstrong Number."
else:
return "Not an Armstrong Number."
number = int(input("Enter a number: "))
print(check_armstrong(number))

3) Write python script to check whether a input number is perfect


number of not.

def isPerfect(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
if sum == n:
return "Perfect Number."
else:
return "Not a perfect NUmber."
number = int(input("Enter anumber: "))
print(isPerfect(number))
4) Write a program to calculate X^y.

x = int(input("Enter the value of X: "))


y = int(input("Enter the value of Y: "))
print(pow(x, y))

5) Write a program to check whether a input number is palindrome or not.

def check_palindrome(num):
temp = num
rev = 0
while temp > 0:
rem = temp % 10
rev = rev*10 + rem
temp //= 10
if rev == num:
return "palindrome Number."
else:
return "Not an palindrome Number."
number = int(input("Enter a number: "))
print(check_palindrome(number))
6) Write a program to calculate sum of first and last digit of a number.

number = input("Enter the number: ")

total = int(number[0]) + int(number[-1])


print(total)
SET :- B

1)Write a program to accept a number and count number of even, odd,


zero digits within that number.

def counter(n):
odd = 0
even = 0
zero = 0
for i in n:
if int(i) == 0:
zero += 1
elif int(i) % 2 != 0:
odd += 1
elif int(i) % 2 == 0:
even += 1
print(f"Even: {even}\nOdd: {odd}\nZero: {zero}")
number = input("Enter a number: ")
counter(number)

2) Write a program to accept a binary number and convert it into


decimal number.
binary_string = input("Enter a binary number :")

decimal = int(binary_string, 2)

print(f"The decimal value is {decimal}")

3) Write a program which accepts an integer value as command line


and print “Ok” if value is between 1 to 50 (both inclusive) otherwise it
prints ”Out of range”.

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


if number > 0 and number < 51:
print("Ok")
else:
print("Out of range")

4) Write a program which accept an integer value ‘n’ and display all
prime numbers till ‘n’.

def isPrime(n):
if(n == 1 or n == 0):
return False
for i in range(2, n):
if(n % i == 0):
return False
return True
n = int(input("Enter a number: "))
for i in range(1, n + 1):
#check if current number is prime
if(isPrime(i)):
print(i, end=" ")

5) Write python script to accept two numbers as range and display


multiplication table of all numbers within that range.

def multiplication_table(n):
for i in range(1, 11):
print(f"{i} X {n} = {i*n}")
print("\n\n")
number = int(input("Enter a number: "))
for i in range(1, number + 1):
multiplication_table(i)
SET :- C

1)Write a python script to generate the following pattern upto n lines


1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1

def pattern_generator(row):
for i in range(1, row+1):
for j in range(1, row + 1 - i):
print(' ', end='')
# for increasing pattern
for j in range(1, i + 1):
print(j, end='')
# for decreasing pattern
for j in range(i - 1, 0, -1):
print(j, end='')
# Moving to next line
print()
row = int(input("Enter number of rows: "))
pattern_generator(row)

You might also like