Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

QUESTION 1

Create a pandas series from a dictionary of values and an ndarray

Input:

import pandas as pd

import numpy as np

# Dictionary of values

data_dict = {'a': 10, 'b': 20, 'c': 30}

# NumPy ndarray

array_data = np.array([40, 50, 60])

# Create a Pandas Series

series_from_dict = pd.Series(data_dict)

series_from_array = pd.Series(array_data)

print("Series from dictionary:")

print(series_from_dict)

print("\nSeries from ndarray:")

print(series_from_array)

Output:
Series from dictionary:

a 10

b 20

c 30

dtype: int64

Series from ndarray:

0 40

1 50

2 60

dtype: int64
Question 2

Write a program to calculate the simple interest given principal amount, rate,
and time

Input:

principal = float(input("Enter principal amount: "))

rate = float(input("Enter rate of interest: "))

time = float(input("Enter time in years: "))

simple_interest = (principal * rate * time) / 100

print("Simple Interest:", simple_interest)

Output:

Enter principal amount: 1000

Enter rate of interest: 5

Enter time in years: 3

Simple Interest: 150.0


Question 3
Write a program to check if a given number is even or odd

Input:

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

if num % 2 == 0:

print(num, "is even")

else:

print(num, "is odd")

Output:

Enter a number: 7

7 is odd
Question 4
Write a program to find the factorial of a given number

Input:

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

factorial = 1

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

factorial *= i

print("Factorial:", factorial)

Output:

Enter a number: 5

Factorial: 120
Question 5
Write a program to calculate the greatest common divisor(GCD) of two numbers

Input:

def gcd(a, b):

while b:

a, b = b, a % b

return a

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

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

result = gcd(num1, num2)

print("GCD:", result)

Output:

Enter first number: 12

Enter second number: 18

GCD: 6
Question 6
Write a program to check if a given number is a prime number

Input:

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

is_prime = True

if num <= 1:

is_prime = False

else:

for i in range(2, int(num**0.5) + 1):

if num % i == 0:

is_prime = False

break

if is_prime:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

Output:

Enter a number: 17

17 is a prime number
Question 7
Write a program to generate the Fibonacci series up to a given number of terms

Input:

num_terms = int(input("Enter number of terms: "))

fibonacci_series = [0, 1]

while len(fibonacci_series) < num_terms:

next_term = fibonacci_series[-1] + fibonacci_series[-2]

fibonacci_series.append(next_term)

print("Fibonacci Series:", fibonacci_series)

Output:

Enter number of terms: 6

Fibonacci Series: [0, 1, 1, 2, 3, 5]


Question 8
Write a program to calculate the power of a number using recursion

Input:

def power(base, exponent):

if exponent == 0:

return 1

else:

return base * power(base, exponent - 1)

base = float(input("Enter base: "))

exponent = int(input("Enter exponent: "))

result = power(base, exponent)

print(base, "raised to the power of", exponent, "is", result)

Output:

Enter base: 2

Enter exponent: 4

2.0 raised to the power of 4 is 16.0


Question 9
Write a program to find the sum of the digits of a given number

Input:

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

sum_of_digits = 0

while num > 0:

digit = num % 10

sum_of_digits += digit

num //= 10

print("Sum of digits:", sum_of_digits)

Output:

Enter a number: 12345

Sum of digits: 15
Question 10
Write a program to count the number of digits, letters, and special characters in a string

Input:

string = input("Enter a string: ")

digits = letters = special = 0

for char in string:

if char.isdigit():

digits += 1

elif char.isalpha():

letters += 1

else:

special += 1

print("Digits:", digits)

print("Letters:", letters)

print("Special characters:", special)

Output:

Enter a string: Hello123$#

Digits: 3

Letters: 5

Special characters: 2
Question 11
Write a program to check if a given string is a palindrome

Input:

def is_palindrome(string):

string = string.lower().replace(" ", "")

return string == string[::-1]

input_string = input("Enter a string: ")

if is_palindrome(input_string):

print("Palindrome")

else:

print("Not a palindrome")

Output:

Enter a string: Racecar

Palindrome
Question 12

Write a program to calculate the factorial of a given number using recursion

Input:

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

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

result = factorial(num)

print("Factorial:", result)

Output:

Enter a number: 6

Factorial: 720
Question 13
Write a program to swap the values of two variables without using a temporary variable

Input:

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

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

a=a+b

b=a-b

a=a-b

print("After swapping: a =", a, "b =", b)

Output:

Enter first number (a): 5

Enter second number (b): 10

After swapping: a = 10 b = 5
Question 14
Write a program to reverse a given number

Input:

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

reversed_num = 0

while num > 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

print("Reversed number:", reversed

Output:

Enter a number: 12345

Reversed number: 54321


Question 15
Write a program to check if a given number is an Armstrong number

Input:

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

original_num = num

num_of_digits = len(str(num))

sum_of_digits_cubed = 0

while num > 0:

digit = num % 10

sum_of_digits_cubed += digit ** num_of_digits

num //= 10

if original_num == sum_of_digits_cubed:

print(original_num, "is an Armstrong number")

else:

print(original_num, "is not an Armstrong number")

Output:

Enter a number: 153

153 is an Armstrong number

You might also like