0% found this document useful (0 votes)
3 views

Python Ex4

Python 4

Uploaded by

agnelsusai05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Ex4

Python 4

Uploaded by

agnelsusai05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex.No.

4
User defined function Reg.No: URK23CS7103
06.02.24

1. Write a python program to check whether a number is a palindrome or not using a function.

Aim:
To Write a python program to check whether a number is a palindrome or not using a function.

Algorithm:

Step1: Define a function is_palindrome(number):


• Take a number as input
Step2: while loop is used to reverse the number.
Step3: check if the original number equal to reverse using the if and else statement.
Step4: get input from the user and the input is stored in num
Step5: depending on the return value of the function,the program prints whether the input number is a
palindrome or not

Program:

def is_palindrome(num):
original_num = num
reverse_num = 0

while num > 0:


digit = num % 10
reverse_num = (reverse_num * 10) + digit
num = num // 10

if original_num == reverse_num:
return True
else:
return False

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

if is_palindrome(num):
print(num, "is a palindrome.")
else:
print(num, "is not a palindrome.")

Output:

Result:
Thus, the program was successfully excuted

1
2. .Write a python program to check Armstrong number using functions.

Aim: To Write a python program to check Armstrong number using functions.

Algorithm:

Step1: start the program


Step2: Take a number as input.
Step3: Convert the number to a string to iterate over its digits.
Step4: Calculate the number of digits in the input number.
Step5: Iterate over each digit and compute the sum of the cubes of these digits.
Step6: Check if the sum of the cubes is equal to the original number.
Step7: If they are equal, the number is an Armstrong number; otherwise, it's not.

Program:

def is_armstrong(num):
num_str = str(num)
num_digits = len(num_str)
sum_of_cubes = 0
for digit in num_str:
sum_of_cubes += int(digit) ** num_digits
if sum_of_cubes == num:
return True
else:
return False
num = int(input("Enter the number to
find to check wheather the give
number is Armstrong or not: "))
if is_armstrong(num):
print(The entered number is Armstrong
number")
else:
print(num, "is not an Armstrong number")

Output:

Result:
Hence the output is verified and excecuted.

2
3. Write python functions to print the no. of uppercase, lowercase, and whitespaces.

Aim: To Write python functions to print the no. of uppercase, lowercase, and whitespaces.

Algorithm:

Step1: start a program


Step2: .Input: Prompt the user to enter a string.
Step3: Function: Define a function count_chars to count uppercase letters, lowercase letters, and
whitespaces in the input string.
Step4: Processing: Iterate through each character in the string, incrementing counters for uppercase,
lowercase, and whitespace characters.
Step5: Output: Print the counts of uppercase letters, lowercase letters, and whitespaces.
Step6: User Interaction: Get input from the user, call the function with the input string, and display
the results.

Program:

def count_chars(string):
uppercase_count = 0
lowercase_count = 0
whitespace_count = 0

for char in string:


if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
elif char.isspace():
whitespace_count += 1

print("Uppercase:", uppercase_count)
print("Lowercase:", lowercase_count)
print("Whitespaces:", whitespace_count)

user_input = input("Enter any string: ")


count_chars(user_input)

Output:

Result:
Hence the output is verified and excecuted

3
4

You might also like