Lab 2
Lab 2
)
[CS-3096]
Individual Work
Lab. No: 2 , Date: 27/01/2023 , Day: Friday
Topic: numPy basics
Roll Number: 2006358 Branch/Section: IT-3
Name in PRAKHAR DUBEY
Capital:
Program No:1.1
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("enter a number"))
fact=1
for x in range(1,n+1):
fact=fact*x
Program No:1.2
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("enter a number"))
sum=0
while n>0:
d=n%10
sum=sum+d
n=int(n/10)
print(sum)
Program No:1.3
Program title: WAP to display the reverse of a number entered through keyboard.
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("enter a number"))
sum=0
while n>0:
d=n%10
sum=(sum*10)+d
n=int(n/10)
print(sum)
Program No:1.4
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n1=int(input("enter a number"))
n2=int(input("enter second number"))
mini=min(n1,n2)
hcf=1
for i in reversed(range(mini+1)):
if n1%i==0 and n2%i==0:
hcf=i
break
print(hcf)
Program No:1.5
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("enter a number"))
c=0
for i in range(2,n):
if n%i==0:
c=c+1
if c>0:
print("not a prime number")
else:
print("prime number")
Program No:1.6
Program title: WAP to print all odd and even numbers separately within a given
range. The range is input throughuser.
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
odd=[]
even=[]
n1=int(input("enter a number"))
n2=int(input("enter second number"))
for i in range(n1,n2):
if i%2==0:
even.append(i)
else:
odd.append(i)
print("even numbers")
for i in even:
print(i)
print("odd numbers")
for i in odd:
print(i)
Program No:1.7
Program title: WAP to evaluate the equation y=x n where n is a non-negative integer.
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
x=int(input("enter x "))
n=int(input("enter n "))
print(pow(x,n))
Program No:1.8
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("enter a number"))
divisors=[]
for i in range(1,n):
if n%i == 0:
divisors.append(i)
sum=0
for i in divisors:
sum+=i
if sum==n:
print("perfect number")
else:
print("not a perfect number")
Program No:1.9
Program title: WAP to check whether an input integer is strong number or not
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
def factorial(n):
fact=1
for x in range(1,n+1):
fact=fact*x
return fact
sum=0
num=int(input("enter a number"))
num1=num
while num>0:
d=num%10
fact=factorial(d)
sum=sum+fact
num=int(num/10)
if sum==num1:
print("strong number")
else:
print("not a strong number")
Program No:1.10
Program title: WAP to find out the prime factors of a number entered through
keyboard
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
import math
def primeFactors(n):
while n % 2 == 0:
print (2),
n=n/2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
print (i),
n=n/i
if n > 2:
print (n)
Program No:1.11
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
n = int(input("Enter a number"))
print(fibonacci(n))
Program No:1.12
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n=int(input("Enter the range of number(Limit):"))
i=1
pr=0
while i<=n:
pr = (pr * 2) + 1
print(pr)
i+=1
Program No:1.13
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
Program No:1.14
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n = int(input("Enter the range: "))
sum = (n * (n+1))/2
print(sum)
Program No:1.15
Program title: WAP to print the following pattern for n rows. Ex. for n=5 rows
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
Program title: WAP to print the Following pattern for n rows. Ex. for n=5 rows
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n = int(input("Enter the number of rows: "))
k=n-1
for i in range(0,n):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0,i+1 ):
print("* ",end="")
print("\r")
Program No:1.17
Program title: WAP to print the following pattern for n rows. Ex. for n=6 rows
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
n = int(input("Enter the number of rows: "))
k=0
for i in range(0,n+1):
if(i%2==0):
k=0
else:
k=1
for j in range(0,i):
print(k ," ",end=" ")
if(k==0):
k=1
else:
k=0
print("\n")
Program No:1.18
Program title: WAP to print the following pattern for n rows. Ex. for n=5 rows
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
Program No:1.19
Program title:
WAP to print the following pattern for n rows. Ex. for n=5 row
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
while r<n:
if r%2 == 0:
c=0
while c <= r:
print(c+1, end = "")
c += 1
else:
c = r+1
while c>0:
print(c, end = "")
c -= 1
print()
r += 1
Program No:1.20
Program title:
WAP to form reverse pyramid of numbers for a given number. Ex. for
number 4
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
while r<n:
s=0
while s<2*r:
print(" ",end = "")
s+=1
c=0
while c < n - r:
print(c+1, end = "")
c += 1
c -= 1
while c >= 1:
print(c, end = "")
c -= 1
print()
r += 1
Program No:1.21
Program title:
WAP to generate the pascal triangle pyramid of numbers for a given number.
Ex. for number 4
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
def pascal(n):
for i in range(n+1):
for j in range(n-i):
print(' ', end='')
C=1
for j in range(1, i+1):
print(C, ' ', sep='', end='')
C = C * (i - j) // j
print()
Program No:1.22
Program title: WAP to display the following style o/p for a given string input through
keyboard.(Ex.for astring“KIITCSIT”)
Input/Output Screenshots:
RUN-1:
Source code
string = "KIITCSIT"
n = len(string)
for i in range(n):
print(string[:n-i] + " "*(i*2) + string[n-i-1::-1])
for i in range(n-2, -1, -1):
print(string[:n-i] + " "*(i*2) + string[n-i-1::-1])
Program No:1.23
Program title: WAP to convert a decimal number into its equivalent number with
base b. Decimal number andbarethe user input
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
Program No:1.24
Program title: WAP to convert a number with base b into its equivalent decimal
number. Number withbaseb&barethe user input.
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
import math
j=0
b = int(input("Enter the base of the number "))
num = int(input("Enter the number "))
dec = 0
while(num != 0):
rem = num % 10
dec = dec + rem * pow(2,j)
num /= 10
Program title:WAP to convert a binary number to its equivalent octal & hexa-
decimal number system.
Input/Output Screenshots:
RUN-1:
RUN-2:
Source code
j=1
k=1
octalnum = 0
hexa = 0
binarynum = int(input("Enter a binary num "))
binarynum1 = binarynum
while (binarynum != 0):
remainder = binarynum % 10
octalnum = octalnum + remainder * j
j=j*2
binarynum = binarynum / 10
hex = 0
mul = 1
chk = 1
i=0
hnum = []
bnum = 1
while bnum!=0:
rem = bnum%10
hex = hex + (rem*mul)
if chk%4==0:
if hex<10:
hex = hex+48
val = chr(hex)
hnum.insert(i, val)
else:
hex = hex+55
val = chr(hex)
hnum.insert(i, val)
mul = 1
hex = 0
chk = 1
i = i+1
else:
mul = mul*2
chk = chk+1
bnum = int(bnum/10)
if chk!=1:
hex = hex+48
val = chr(hex)
hnum.insert(i, val)
if chk==1:
i = i-1