Monday, March 22, 2021

to compute 1:square root of a numnber 2:square of a number 3:cube of a number 4)Check for prime 5)factorial of a number6)Prime factor of a number

To accept the number and compute using python

a)Square root a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:77

>>> num_sqrt = num ** 0.5

>>> print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

The square root of 77.000 is 8.775

>>> 

b)Squar of a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:12

>>> square = num * num

>>> print('The square of %0.3f is %0.3f'%(num ,square))

The square of 12.000 is 144.000

>>>

c)Cube of a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:4

>>> cube= num * num *num

>>> print('The cube of %0.3f is %0.3f'%(num ,cube))

The cube of 4.000 is 64.000

d)Check for Prime Number

>>> num = int(input("Enter a number: "))

Enter a number: 23

>>> if num > 1:

...     for i in range(2, num):

...             if(num % i) == 0:

...                     print(num,"IS NOT APRIME NUMBER")

...                     break

...             else:

...                     print(num,"IS A PRIME NUMBER")

...     else:

...             print(num,"IS NOT A PRIME NUMBER")

...

23 IS A PRIME NUMBER

5) factorial of number

>>> num = int(input('Enter a Number:'))

Enter a Number:23

>>> f = 1

>>> if(num < 0):

...     print("THE NUMBER IS NEGATIVE")

... elif(num == 0):

...     print("The factorial of 0 is 1")

... else:

...     for i in range (1, num + 1):

...             f =f * i

...     print("the factorial of",num,"is",f)

...

the factorial of 23 is 25852016738884976640000

>>>

6)Prime Factore of a Number
>>> Number = int(input(" Please Enter any Number: "))
 Please Enter any Number: 23
>>> i = 1
>>> while(i <= Number):
...     count = 0
...     if(Number % i== 0):
...             j = 1
...             while(j <= i):
...                     if(i % j== 0):
...                             count = count + 1
...                     j = j+ 1
...             if(count == 2):
...                     print("%d is a Prime Factor of a Given Number %d" %(i, Number))
...     i = i + 1
...
23 is a Prime Factor of a Given Number 23
>>>

No comments:

Post a Comment