Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views10 pages

Hanshuman Program File

The document contains eight Python programs that demonstrate various functionalities including checking for prime numbers, palindrome verification, ASCII code conversion, counting character statistics, computing GCD and LCM, calculating factorials, and simulating a lottery. Each program includes code snippets and prompts for user input, along with expected outputs. The programs utilize functions to encapsulate logic and improve code organization.

Uploaded by

Iphone 12
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)
14 views10 pages

Hanshuman Program File

The document contains eight Python programs that demonstrate various functionalities including checking for prime numbers, palindrome verification, ASCII code conversion, counting character statistics, computing GCD and LCM, calculating factorials, and simulating a lottery. Each program includes code snippets and prompts for user input, along with expected outputs. The programs utilize functions to encapsulate logic and improve code organization.

Uploaded by

Iphone 12
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/ 10

PROGRAM – 1

1. write a program in python to check a number whether it is prime or not


by using functions

CODE:-

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

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

if is_prime(number):
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")
OUTPUT:-
PROGRAM - 2
2. write a program in python to check a number whether it is palindrome or
not by using functions

CODE:-
def is_palindrome(n):
return str(n) == str(n)[::-1]

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

if is_palindrome(number):
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")

OUTPUT:-
PROGRAM - 3
3. write a program to display ASCII code of a character and vice versa

CODE:-

ch = input("Enter a character or ASCII code: ")

if ch.isdigit():
print("Character:", chr(int(ch)))
elif len(ch) == 1:
print("ASCII code:", ord(ch))
else:
print("Invalid input.")

Output:-
PROGRAM – 4

4.write a program using functions that reads a line and print its statics like:-
number of uppercase letters , number of lowercase letters ,number of digits.

CODE:-
def count_stats(line):
upper = lower = digits = 0
for char in line:
if char.isupper():
upper += 1
elif char.islower():
lower += 1
elif char.isdigit():
digits += 1
print("Uppercase:", upper, "Lowercase:", lower,
"Digits:", digits)
def main():
line = input("Enter a line: ")
count_stats(line)
main()
OUTPUT:-
PROGRAM – 5
5. Write a program to compute GCD & LCM of two numbers using functions.
CODE:-
def gcd(a, b):
while b: a, b = b, a % b
return a

def lcm(a, b):


return a * b // gcd(a, b)

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))

print("GCD:", gcd(x, y))


print("LCM:", lcm(x, y))

OUTPUT:-
PROGRAM – 6

6. Write a program to calculate fact() factorial using functions for an


integer

CODE:-
def FACT(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact
num = int(input("Enter a number: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = FACT(num)
print("Factorial of", num, "is:", result)

OUTPUT:-
PROGRAM – 7
7. Write a python program to generate random numbers between 1
and 6 and check whether a person has won a lottery or not

CODE:-
import random

def check_lottery():
number = random.randint(1, 6)
if number == 6:
print("You won the lottery!")
else:
print("Try again. Your number was", number)

check_lottery()

OUTPUT:-

PROGRAM – 8

You might also like