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

Assignments

Assignment for bca

Uploaded by

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

Assignments

Assignment for bca

Uploaded by

papai sarkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment- 4

1. Write a program in python to check whether a number is Aron strong or not.

An Aron strong number is a number whose sum of digits raised to the power of the number of digits equals the
original number. Below is a Python program to check if a given number is an Aron strong number or not:

```python

def is_aron_strong(number):

# Convert number to string to count digits

num_str = str(number)

# Count the number of digits

num_digits = len(num_str)

# Calculate the sum of digits raised to the power of the number of digits

digit_sum = sum(int(digit)**num_digits for digit in num_str)

# Check if the sum is equal to the original number

return digit_sum == number

# Test the function

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

if is_aron_strong(number):

print(f"{number} is an Aron strong number.")

else:

print(f"{number} is not an Aron strong number.")

```

This program first defines a function `is_aron_strong` that takes a number as input and returns `True` if it's an Aron
strong number, otherwise `False`. Then it prompts the user to enter a number, checks if it's an Aron strong number
using the defined function, and prints the result.
2. Write a program in python to check whether a number entered by the user is palindrome or not.

You can use the following Python program to check if a number entered by the user is a palindrome or not:

```python

def is_palindrome(number):

# Convert number to string for comparison

num_str = str(number)

# Reverse the string

reversed_str = num_str[::-1]

# Check if the original number is equal to its reverse

return num_str == reversed_str

# Test the function

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

if is_palindrome(number):

print(f"{number} is a palindrome.")

else:

print(f"{number} is not a palindrome.")

```

This program defines a function `is_palindrome` that takes a number as input and returns `True` if it's a palindrome,
otherwise `False`. It then prompts the user to enter a number, checks if it's a palindrome using the defined function,
and prints the result.
3. Write a program in python to check whether a number krishnamurthi or not.

A Krishnamurthy number (also known as a Strong number) is a number that is equal to the sum of the factorials of its
digits. For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

Here's a Python program to check if a given number is a Krishnamurthy number:

```python

def is_krishnamurthy(number):

# Function to calculate factorial of a number

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

# Convert number to string to extract digits

num_str = str(number)

# Calculate the sum of factorials of digits

factorial_sum = sum(factorial(int(digit)) for digit in num_str)

# Check if the sum is equal to the original number

return factorial_sum == number

# Test the function

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

if is_krishnamurthy(number):

print(f"{number} is a Krishnamurthy number.")

else:

print(f"{number} is not a Krishnamurthy number.")


```

This program first defines a nested function `factorial` to calculate the factorial of a number. Then, it defines the
`is_krishnamurthy` function, which takes a number as input and returns `True` if it's a Krishnamurthy number,
otherwise `False`. Finally, it prompts the user to enter a number, checks if it's a Krishnamurthy number using the
defined function, and prints the result.

Assignment- 5

1. Write a program in python to print the following series 1,2,3, …..n

You can use a simple loop to print the series of numbers from 1 to n. Here's a Python program to do that:

```python

def print_series(n):

for i in range(1, n + 1):

print(i, end=" ")

# Test the function

n = int(input("Enter the value of n: "))

print("The series is:")

print_series(n)

```

This program defines a function `print_series` that takes an integer `n` as input and uses a `for` loop to iterate from 1
to `n`, printing each number separated by a space. Finally, it prompts the user to enter the value of `n`, calls the
`print_series` function with that value, and prints the series.

2. Write a program in python to print the factorial of a number.


You can use a recursive function to calculate the factorial of a number in Python. Here's a program to do that:

```python
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

# Test the function


number = int(input("Enter a number: "))
print(f"The factorial of {number} is: {factorial(number)}")
```

This program defines a recursive function `factorial` that takes an integer `n` as input and returns the factorial
of `n`. If `n` is 0 or 1, the function returns 1 (base case). Otherwise, it calculates `n * factorial(n - 1)` to compute
the factorial. Finally, it prompts the user to enter a number, calls the `factorial` function with that number, and
prints the result.

3. Write a program in python to print the Fibonacci series.

You can use a loop to generate the Fibonacci series in Python. Here's a program to do that:

```python

def fibonacci_series(n):

# Initialize the first two Fibonacci numbers

a, b = 0, 1

count = 0

# Check if the number of terms is valid

if n <= 0:

print("Please enter a positive integer.")

elif n == 1:

print("Fibonacci series upto", n, ":")

print(a)

else:

print("Fibonacci series:")

while count < n:

print(a, end=" ")

nth = a + b

# Update values for the next iteration


a=b

b = nth

count += 1

# Test the function

n = int(input("Enter the number of terms in the Fibonacci series: "))

fibonacci_series(n)

```

This program defines a function `fibonacci_series` that takes an integer `n` as input and prints the Fibonacci series up
to the `n`th term. It initializes the first two Fibonacci numbers `a` and `b` to 0 and 1, respectively, and then uses a loop
to calculate and print the Fibonacci series. The loop runs `n` times to generate the series up to the `n`th term.

You might also like