0% found this document useful (0 votes)
7 views

Slip 3

This document contains Python code examples for testing divisibility, repeating strings, finding the sum of natural numbers, constructing matrices, finding eigenvectors and eigenvalues, generating prime numbers, estimating integrals using Simpson's 1/3 rule and Trapezoidal rule.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Slip 3

This document contains Python code examples for testing divisibility, repeating strings, finding the sum of natural numbers, constructing matrices, finding eigenvectors and eigenvalues, generating prime numbers, estimating integrals using Simpson's 1/3 rule and Trapezoidal rule.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.Write python code to test whether given number is divisible by 2 or 3 or 5.

import math as m
n = int(input("Enter a number:\n"))
if n%3==0:
print("%d is divisible by 3"%n)
elif n%5 == 0& n%10==0:
print("%d is divisible by 5 and 10"%n)

2. Repeat the following string 11 times using the string operator on Python.
a. LATEX
b. MATLAB

repeated_latex = "LATEX" * 11
print(repeated_latex)

repeated_matlab = "MATLAB" * 11
print(repeated_matlab)

3. Use Python code to find sum of first thirty natural numbers.

sum = 0
for i in range(1, 31):
sum += i

print(sum)

4. Using Python construct the following matrices.


1. An identity matrix of order 10 x 10.
2. Zero matrix of order 7 x 3.
3. Ones matrix of order 5 x 4.

import numpy as np
a=np.identity(10)
b=np.zeros([7,3])
c=np.ones([5,4])
print(a,b,c)

Q.2 (2) eigenvector eigen value


import numpy as np
A=np.array([ [3,-2],[6,-4]])
print("A= ",A)
E,V=np.linalg.eig(A)
print("Eigenvalues: \n",E)
print("Eigenvectors: \n",V)

3. Generate all the prime numbers between 1 to 100 using Python code.

for Number in range (1, 101):


count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

Q.Write Python program to estimate the value of the integral fo sin(x)dx using
Simp- son's (1/3)rd rule (n=6).

# Define function to integrate


import math as mt
def f(x):
return mt.sin(x)

# Implementing Simpson's 1/3


def simpson13(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h

if i%2 == 0:
integration = integration + 2 * f(k)
else:
integration = integration + 4 * f(k)

# Finding final integration value


integration = integration * h/3

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call trapezoidal() method and get result


result = simpson13(lower_limit, upper_limit, sub_interval)
print("Integration result by Simpson's 1/3 method is: %0.6f" % (result) )

Q. write the python program to estimate the value of integral 1/(1+x) using
trapezoidal rule (n=5)

# Define function to integrate


def f(x):
return 1/(1 + x)

# Implementing trapezoidal method


def trapezoidal(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)

# Finding final integration value


integration = integration * h/2

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call trapezoidal() method and get result


result = trapezoidal(lower_limit, upper_limit, sub_interval)
print("Integration result by Trapezoidal method is: %0.6f" % (result) )

You might also like