Ashwin Python As1
Ashwin Python As1
(Computer Science)
SECC - I
CS-3510 PYTHON PROGRAMMING
Semester V
(From Academic Year 2021)
Assignment Evaluation
32
Ashwin_Practical_Assignment_1_[Roll_No_12]
Before swap: a = 5 b = 10
After swap: a = 10 b = 5
Random number: 48
[7]: #Set B Q1) Python Program to Check if a Number is Positive, Negative or Zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Enter a number: 12
1
The number is positive.
Enter a number: 12
The number is even.
Enter a number: 1
The number is not prime.
Enter a number: 12
The number is not an Armstrong number.
Enter a number: 12
2
The factorial of 12 is 479001600
Enter kilometers: 12
12.0 kilometers is equal to 7.46 miles
[23]: #PFP Q4) Python Program to Print all Prime Numbers in an Interval
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
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
for num in range(start, end + 1):
if is_prime(num):
print(num)
3
7
11
[27]: #PFP Q6) Write a Python Program to Find Armstrong Number in an Interval
def is_armstrong(n):
original = n
power = len(str(n))
sum = 0
while n > 0:
number = n % 10
sum += number ** power
n = n // 10
return original == sum
4
[29]: #PFP Q7) Function to calculate the sum of natural numbers
def sum_of_natural_numbers(n):
return n * (n + 1) // 2
n = int(input("Enter a natural number: "))
print("Sum of natural numbers up to", n, "is:", sum_of_natural_numbers(n))
[ ]: