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

08 SubhasishBagchi Day1

Uploaded by

Subhasish Bagchi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

08 SubhasishBagchi Day1

Uploaded by

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

1. Write a python program that reads three integer numbers.

Check whether these


numbers can be considered as the three sides of a triangle. If so, find the type
(isosceles, equilateral or right-
angled) and area of the triangle.

In [26]:
import math

# Taking inputs from user

a=float(input("Enter value for Side-1 :"))

b=float(input("Enter value for Side-2 :"))

c=float(input("Enter value for Side-3 :"))

s=(a+b+c)/2;

# Triangle area formula

area=math.sqrt(s*(s-a)*(s-b)*(s-c))

# check whether the triangle is possible or not

if(area!=0):

print("RESULT: the triangle is possible.")

# checking wheter the triangle is equilateral triangle or not---

if(a==b and a==c and b==c):

print("It is a Equilateral Triangle.",end=("\n"))

# checking wheter the triangle is Isosceles triangle or not---

elif(a==b or a==c or b==c):

print("It is a Isosceles Triangle.",end=("\n"))

# checking wheter the triangle is Right-angle Triangle or not---

elif((a*a)==(b*b)+(c*c) or (b*b)==(a*a)+(c*c) or (c*c)==(a*a)+(b*b)):

print("It is a Right-angle Triangle.",end=("\n"))

if(area==0):

print("Area of a triangle cant't be zero hence triangle is not formed.")

print("\n area of the triangle=",area)

Enter value for Side-1 :3

Enter value for Side-2 :3

Enter value for Side-3 :5

RESULT: the triangle is possible.

It is a Isosceles Triangle.

area of the triangle= 4.14578098794425

In [ ]:
2. Write a python to check the number is power of 3.

In [6]:
# Check the number is power of 3 or not---------

# define the function for n ---------------------

def isPower_of_Three(n):

if (n <= 0):

return False # returnig the value ---------

if (n % 3 == 0):

return isPower_of_Three(n // 3) # performing floor division-----

if (n == 1):

return True # returnig the value ---------

return False

# Driver code ---------------------------

num1 =int(input("Enter the number =")) # Taking input from user ------------

if (isPower_of_Three(num1)): # condition checking --------------


print("Yes this number is power of 3")

else:

print("No it is not a power of 3 number")

Enter the number =27

Yes this number is power of 3

In [ ]:
# 3. Write a python program to print the following patterns for n lines.

i)

**

***

****

*****

In [7]:
# 1st pattern

n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i>=j ): # condition part ---------

print("*", end="")

else:

print(" ", end=" ")

print()

**

***

****

*****

In [ ]:
ii)

*****

****

***

**

In [8]:
# 2nd pattern

n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i<=j ): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

*****
****
***
**
*

In [ ]:
iii)

*****

****

***

**

In [9]:
n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i+j<=n-1): # condition part ---------

print("*", end="")

else:

print(" ", end=" ")

print()

*****
****

***

**

In [ ]:
iv)

**

***

****

*****

In [10]:
n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i+j>=n-1): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

*
**
***
****
*****

In [ ]:
v)

*****

***

***

*****

In [11]:
# 5th pattern

n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i<=j and i+j<=n-1 or i>=j and i+j>=n-1 ): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

*****
***
*
***
*****

In [ ]:
vi)

* *

** **

*****

** **

* *

In [12]:
# 6th pattern

n=5 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i<=j and i+j>=n-1 or i>=j and i+j<=n-1 ): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

* *
** **
*****
** **
* *

In [ ]:
vii)

***

*****

*******

*********

In [13]:
n=9 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i>=j and i+j>=n-1): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

***

*****

*******

*********

In [ ]:
viii)

*********

*******

*****

***

In [14]:
n=9 # statically initialize the n -----------

for i in range(n):

for j in range(n):

if(i<=j and i+j<=n-1 ): # condition part ---------

print("*", end="")

else:

print(" ", end="")

print()

*********

*******

*****

***

In [ ]:
4. Write a python program to evaluate the following series

(i) S = 1 + 3 + 5 + 7 + ………… + N (Input N).

In [15]:
# (i) S = 1 + 3 + 5 + 7 + ………… + N (Input N).

n=int(input("enter the range upto which you want to print : ")) # Taking input from user -----

s=0.0

for i in range(1,n+1,2):

s=s+i

print("sum of the series is : ",s)

enter the range upto which you want to print : 7

sum of the series is : 16.0

(ii) S = 1 - 1/3 + 1/9 - 1/27 + 1/81 - 1/243 …. Up to Nth Term. (Input N)

In [16]:
import math # Import of math module -----------

n=int(input("enter the range upto which you want to print : ")) # Taking input from user -----

s=0.0

for i in range(1,n+1):

s=s+1/math.pow(-3,(i-1))

print("sum of the series is : ",s)

enter the range upto which you want to print : 2

sum of the series is : 0.6666666666666667

(iii) S = 1/1! - 2/2! + 3/3! – 4/4!............................. Up to Nth Term. (Input N)

In [17]:
import math # Import of math module -----------

n =int(input("Enter number of terms =")) # Taking input from user -----

s=0

print ("Terms are :")

for i in range (1,n+1):

t = (i/(math.factorial(i))) * math.pow(-1,i-1) # using the pow & factorial functions -----

print (t)

s = s + t

print ("Sum = ",s)

Enter number of terms =5

Terms are :

1.0

-1.0

0.5

-0.16666666666666666

0.041666666666666664

Sum = 0.37500000000000006

(iv) Sin(x) = x – x^3/3! + x^5/5! - ..............(Input X)

In [21]:
import math # Import of math module -----------

i=1

x=int(input("Enter the value of X")) # Taking input from user -----

x=(x*math.pi)/180 # converting to radian ----------

term=x

sum1=x

while True:

term=(term*(-1)*x*x)/(2*i*(2*i+1))

sum1=sum1+term

i=i+1

if (math.fabs(term)<0.000000000000000000001): # using the fabs function for getting the absoulate vale of term--------------

break

print (sum1)

Enter the value of X90

1.0000000000000002

In [ ]:
5. Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

(Input: n =10 Output: 2 3 5 7)

In [22]:
num=int(input("Enter the number : ")) # Taking input from user ----------

for n in range(num+1):

if(n>1):

for i in range (2,n):

if(n%i==0): # condition part -----------------

break

else:

print(n)

Enter the number : 10

In [ ]:
6. Write a python program for determining how many Armstrong numbers exist within a given range.

In [23]:
import math #Import the math module --------

def arm(n): # define a function arm --------------

sum=0

size=int(math.log10(n))+1 # using the log function -----------


n1=n

while(n1>0):

temp=n1%10

sum=sum+math.pow(temp,size)

n1=n1//10

if sum==n:

return True # returing the value -------------

else:

return False # returing the value -------------

n1=int(input("Enter the starting range: ")) # Taking input from user --------

n2=int(input("Enter the ending range: ")) # Taking input from user --------

print("The Armstrong numbers between "+str(n1)+" and "+str(n2)+" are: ")

for i in range(n1,n2+1):

if(arm(i)):

print(i)

Enter the starting range: 100

Enter the ending range: 500

The Armstrong numbers between 100 and 500 are:

153

370

371

407

In [ ]:
7. Write a Program in python to find the sum of all the Prime numbers between a given ranges

of numbers.

In [1]:
def prime(n): # define prime functio for n ------------------

count=0

for i in range(2,n):

if(n%i==0):

count=1

if(count==1):

return False

else:

return True

def Prime_sum(): # define sum function for printing the sum of prime numbers--------

sum=0

for i in range(n1+1,n2+1):

if(prime(i)):

sum=sum+i

return sum

n1=int(input("Enter the starting range: ")) # Taking inputs from the user ---------

n2=int(input("Enter the ending range: ")) # Taking inputs from the user ---------

for i in range(n1,n2+1):

if(i==1):

continue;

elif(prime(i)):

print(i)

print("sum = ",Prime_sum())

Enter the starting range: 10

Enter the ending range: 20

11

13

17

19

sum = 60

In [ ]:
8. Given a maximum of four digit to the base 17(10 -> A, 11 -> B, 12 -> C, 16 -> G) as input, output its decimal value.

(Input: 23GF Output:10980)

In [27]:
while(True):

hexn=input("Enter a 4 digit base 17 number where G stands for 16: ").upper() # Taking input from user -----------

lhexn=len(hexn)

if(lhexn<=4):

break

hstr="0123456789ABCDEFG" # asign the value for hstr ---------------

num=0

for i in range(lhexn):

ch=hexn[i]

n=hstr.index(ch)

num=17*num+n

print(num)

Enter a 4 digit base 17 number where G stands for 16: 23GF

10980

In [ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy