Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Python Program List

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)
13 views

Python Program List

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/ 7

1.

Factorial number using recursion p

Def recur_factorial(n):

If n == 1:

Return n

Else:

Return n*recur_factorial(n-1)

Num = int(input(“Enter a number: “))

If num < 0:

Print(“Sorry, factorial does not exist for negative numbers”)

Elif num == 0:

Print(“The factorial of 0 is 1”)

Else:

Print(“The factorial of”,num,”is”,recur_factorial(num))

Output :

Enter a number: 12

The factorial of 12 is : 479001600

2. Fibonacci series:

def Fibonacci_ser(m):

If (m <= 1):

Return m

Else:

Return Fibonacci_ser(m-1) + Fibonacci_ser(m-2)

m= int(input(“enter number of terms :”))

Print (“fibonacci series : “)

For i in range(m):

Print(Fibonacci_ser(i)),
Output:

Enter the number of terms: 5

3. Convert temperature from Fahrenheit to Celsius:

Celsius_1 = float(input(“Temperature value in degree Celsius: “ ))

Fahrenheit_1 = (celsius_1 * 1.8) + 32

Print(‘The %.2f degree Celsius is equal to: %.2f Fahrenheit’

%(celsius_1, Fahrenheit_1))

Print (“or”)

Fahrenheit_1 = float( input(“Temperature value in degree Fahrenheit: “ ))

Celsius_1 = (Fahrenheit_1 – 32) / 1.8

Print (‘The %.2f degree Fahrenheit is equal to: %.2f Celsius’

%(Fahrenheit_1, celsius_1))

Output:

Temperature value in degree Celsius: 34

The 34.00 degree Celsius is equal to: 93.20 Fahrenheit

Or

Temperature value in degree Fahrenheit: 113

The 113.00-degree Fahrenheit is equal to: 45.00 Celsius

4. Sorting ascending order:


Arr = [5, 2, 8, 7, 1];

Temp = 0;

Print(“Elements of original array: “);

For I in range(0, len(arr)):

Print(arr[i], end=” “);

For I in range(0, len(arr)):

For j in range(i+1, len(arr)):

If(arr[i] > arr[j]):

Temp = arr[i];

Arr[i] = arr[j];

Arr[j] = temp;

Print();

Print(“Elements of array sorted in ascending order: “);

For I in range(0, len(arr)):

Print(arr[i], end=” “);

Output:

Elements of original array:

52871

Elements of array sorted in ascending order:

12578

5.program to check if the given number is happy number:

Def isHappyNumber(num):

Rem = sum = 0;

While(num > 0):

Rem = num%10;

Sum = sum + (rem*rem);


Num = num//10;

Return sum;

Num = 82;

Result = num;

While(result != 1 and result != 4):

Result = isHappyNumber(result);

If(result == 1):

Print(str(num) + “ is a happy number”);

Elif(result == 4):

Print(str(num) + “ is not a happy number”);

Output :

82 s a happy number

6.addition of two matrices :

X = [[1,2,3],

[4,5,6],

[7,8,9]]

Y = [[10,11,12],

[13,14,15],

[16,17,18]]

Result = [[0,0,0],

[0,0,0],

[0,0,0]]

For I in range(len(X)):

For j in range(len(X[0])):

Result[i][j] = X[i][j] + Y[i][j]

For r in result:

Print r
Output:

[11, 13, 15]

[17, 19, 21]

[23, 25, 27]

7. String palindrome:

Str = ‘JaVaJ’

Strstr = str.casefold()

Rev = reversed(str)

If list(str) == list(rev):

Print(“PALINDROME !”)

Else:

Print(“NOT PALINDROME !”)

Output:

PALINDROME!

8. program to sort words in alphabetical order:

My_str = input(“Enter a string: “)

Words = my_str.split()

Words.sort()

For word in words:

Print(word)

Output:

Enter a string: My name Jessi


Jessi

My

name

9. Program to sum all the elements in list :

Lst = [1, 2, 3, 4, 5];

Sum = 0;

For I in range(0, len(lst)):

Sum = sum + lst[i];

Print(“Sum of all the elements of an array: “ + str(sum));

Output:

Sum of all the elements of an array: 15

10 . Create a list and check if the item is present or not:

Lst=[ 1, 6, 3, 5, 3, 4 ]

I=int (input(“enter the element to search: ”))

found = False

For i in lst:

If( i == I ):

found = True

Break

If(found):

Print(“exist”)

Else:

Print(“not exist”)
Output:

Enter the element to search: 9

Not exist

You might also like