DSP LABs
DSP LABs
Program Code:
output:
Enter a Number:
4
Program Code:
Output:
Enter value of x: 55
Enter value of y: 77
The value of x before swapping: 55
The value of y before swapping: 77
The value of x after swapping: 77
The value of y after swapping: 55
Program code:
import random
rand_list = []
#Generating 50 Random numbers
for i in range(0,50):
n = random.randint(1,50)
rand_list.append(n)
print(rand_list)
Output:
[7, 28, 7, 1, 8, 50, 4, 19, 16, 37, 45, 45, 9, 48, 40, 35, 25, 29, 20, 7, 24, 27, 45, 45, 37, 6, 43,
30, 37, 19, 21, 7, 19, 9, 22, 41, 43, 45, 23, 35, 6, 32, 4, 24, 15, 40, 1, 40, 40, 34]
Program code:
Output:
Enter a Number:15
Given number is Odd
Enter a Number:78
Given number is Even
Program code:
Output:
Program Code:
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
Output:
Enter a number: 55
55 is not a prime number
Enter a number: 13
13 s a prime number
Program Code:
Output:
Program Code:
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output:
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num=num-1
print("The sum is",sum)
Output:
Enter a number: 5
The sum is 15
10. Python Program to Find Factorial of Number Using Recursion
def fact(n):
if n == 1:
return n
else:
return n*fact(n-1)
output:
Enter a number 5
The factorial of 5 is 120