[go: up one dir, main page]

0% found this document useful (0 votes)
32 views26 pages

Lab 2

This document contains a lab report for an individual assignment on NumPy basics. It includes 25 programs covering topics like string concatenation with NumPy, calculating factorials and sums of digits, checking prime and perfect numbers, Fibonacci sequences, and pattern printing. For each program, the student included the title, input/output screenshots, and source code. The report is signed off with the student's name, roll number, date and day of the lab.

Uploaded by

358PRAKHAR DUBEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views26 pages

Lab 2

This document contains a lab report for an individual assignment on NumPy basics. It includes 25 programs covering topics like string concatenation with NumPy, calculating factorials and sums of digits, checking prime and perfect numbers, Fibonacci sequences, and pattern printing. For each program, the student included the title, input/output screenshots, and source code. The report is signed off with the student's name, roll number, date and day of the lab.

Uploaded by

358PRAKHAR DUBEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Tool & Technique Laboratory (T&T Lab.

)
[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

Program title: Write a NumPy program to concatenate element-wise two arrays of


string.

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

Program title: WAP to calculate the sum of digits of a given number.

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

Program title: WAP to find the GCD/HCF of two numbers .

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

Program title: WAP to check whether a number n is prime number or not.

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

Program title:WAP to check whether an input integer is perfect number or not

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)

n = int(input("enter a number "))


primeFactors(n)

Program No:1.11

Program title: WAP to find the first n numbers of a Fibonacci sequence.

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

Program title: WAP to print the series as 1 2 7 15 31 ..........n, where n is given by


user.

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

Program title: WAP to print the series as 3 5 7 11 13 17..........n, where n is given by


user.

Input/Output Screenshots:
RUN-1:
RUN-2:

Source code

n= int(input("Enter the rnage"))

for num in range(3, n+1):


for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

Program No:1.14

Program title: . WAP to sum the following series S=1+(1+2)+(1+2+3)+...+(1+2+3+...+n)

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

n = int(input("Enter number of rows: "))


r=0
while r<n:
j=0
while j<=r:
print("* ", sep = " ",end ="")
j += 1
print()
r += 1
Program No:1.16

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

n = int(input("Enter the number of rows: "))


for i in range(0,n+1):
for j in range(i,0,-1):
print(chr(j+64)," ",end="")
print("\n")

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

n= int(input("Enter number of rows: "))


r=0

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

n= int(input("Enter number of rows: "))


r=0

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()

n=int(input("enter a number "))


pascal(n)

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

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


b = int(input("Enter the base"))
if b==2:
print("Decimal to Binary: ",bin(n)[2:])
elif b==8:
print("Decimal to Octal: ",oct(n))
elif b==16:
print("Decimal to Hexadecimal: ",hex(n))

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

print(f"The Equvalent decimal number = {dec}")


Program No:1.25

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

print("\nEquivalent Hexadecimal Value = ", end="")


while i>=0:
print(end=hnum[i])
i = i-1
print()
print(f"Equivalent octal number = {octalnum}")
# print(f"the Equivalent octal number is {hex_num}")

You might also like