Find factorial of a number by using recursion using python

  • Python program to find factorial of a number using recursion in python.
  • Algorithm to find factorial of a number using recursion function using python.

#1: Python program to find factorial of a given number using recursion

  1. #Factorial using recursion
  2. def fact(n):
  3.     if n==1:
  4.         return 1
  5.     else:
  6.         return n*fact(n-1)

  7. n=int(input("Enter the number: "))
  8. result=fact(n)
  9. print("Factorial of",n,"is", result)

Output:

  1. Enter the number: 6
  2. Factorial of 6 is 720



You Might like :
  1. Python try except else with example program
  2. Python finally block example with program
  3. Python try-except- else vs finally with example program
  4. Find factorial of a number without using recursion using python
  5. Python program to find factorial without recursion
  6. If statement in python example program
  7. Else statement in python with example program
  8. Types of operators in python
  9. python math operators
  10. python division operator
  11. Python modulo operator
  12. Python bitwise operators
  13. python comparison operators
  14. Logical operators in python
  15. Assignment operator in python
  16. Identity operator in python
  17. Python power operator
  18. Not equal operator in python
  19. python not operator
  20. python and operator
  21. python Ternary operator
  22. python string operations
  23. python string methods
  24. Spilt function in python
  25. Format function in python
  26. String reverse in python
  27. python tolower
  28. Remove spaces from string python
  29. Replace function in python
  30. Strip function in python
  31. Length of string python
  32. Concat python
  33. startswith python
  34. strftime python
  35. is numeric python
  36. is alpha python
  37. is digit python
  38. python substring

Java program to check valid Balanced parentheses

Balanced parentheses:
  •   A string which contains below characters in correct order.
  •  "{}" "[]" "()"
  • We need to check whether given string has valid order of parenthesis order.
  • If the parenthesis characters are placed in order then we can say its valid parentheses or balanced parentheses.
  • If the parentheses characters are not in correct order or open and close then we can say it is not balanced or invalid parenthesis.
  • Program to check given string has a valid parenthesis or not.

#1: Java example program to check given string has valid parenthesis or not.


  1. package instanceofjava;

  2. import java.util.Stack;

  3. public class BalancedParenthensies {

  4. public static void main(String[] args) {
  5. System.out.println(checkParenthensies("{(xxx,yyy)}"));
  6. System.out.println(checkParenthensies("{)(acd,bcvfs}"));
  7. System.out.println(checkParenthensies("{(xxx},yyy)"));
  8. System.out.println(checkParenthensies("[(xxx),yyy]"));
  9. }
  10. public static boolean checkParenthensies(String str) {
  11.         Stack<Character> object  = new Stack<Character>();
  12.         for(int i = 0; i < str.length(); i++) {
  13.             char ch = str.charAt(i);
  14.             if(ch == '[' || ch == '(' || ch == '{' ) {     
  15.             object.push(ch);
  16.             } else if(ch == ']') {
  17.                 if(object.isEmpty() || object.pop() != '[') {
  18.                     return false;
  19.                 }
  20.             } else if(ch == ')') {
  21.                 if(object.isEmpty() || object.pop() != '(') {
  22.                     return false;
  23.                 }           
  24.             } else if(ch == '}') {
  25.                 if(object.isEmpty() || object.pop() != '{') {
  26.                     return false;
  27.                 }
  28.             }
  29.         }
  30.         return object.isEmpty();
  31.     }
  32. }

Output:
  1. true
  2. false
  3. false
  4. true

check valid java balanced parenthesis


Python try-except- else vs finally with example program

  • For detail explanation of try and except block please check below link
  • Python try and except blocks with an example program
  • We will place all statements which are proved to generate exceptions in try block.
  • If any exception occurred then exception block will handle the exceptions.
  • If no exception raised in try block then except block wont be executed and else block will be executed.
  • In this scenario we might move the code from else to try block. If any exception raised in try block other statements in try block wont be executed and also else block will not be executed.
  • Finally block will always executes irrespective of any kind of exceptions.
  • So if we place any statements inside finally block of python script then those those statements will be executed for sure.
  • Else block will be executed when no exception occurred in try and finally will executes irrespective of any exception.
  • Lets see an example python program on try-except-else and try except-else-finally.




#1: Write a Python program which explains usage both else and finally blocks in python programming.

  1. #try_except with both else and finally blocks:
  2. string=input("enter some string: ")

  3. try:
  4.     x=string[5]
  5.     print("char at index 5 is: ",x)
  6.     print("no exception")
  7. except IndexError as e:
  8.     print("exception raised: ",e)
  9. else:
  10.     print("else block is executing becoz of no exception")
  11. finally:
  12.     print("finally block will always executes")


Output

  1. enter some string: python
  2. char at index 5 is:  n
  3. no exception
  4. else block is executing becoz of no exception
  5. finally block will always executes


python else vs python finally

Python finally block example with program

  • Before discussing about finally block, please check our previous articles on try, except and else blocks. 
  • Basic python programs on try , except and else
  • As we already know about try like its duty is to find any exceptions if occurs and pass it to python except block so that except block will assign exception class object to handle exceptions.
  • Else will be executed if no exception occurs.
  • Finally block will executes even exceptions occurs. so in some situations we need to execute some statements compulsory even if any exceptions occurs in that scenario we will use python finally block
  • We will place all statements which would be executes irrespective of exceptions.
  • Lets see an example program on python finally block.
  • Python try except finally example
     



#1: Write a python program which explains usage of finally block in exception handling of python programming.

  1. try:
  2.   x = int(input("enter first number: "))
  3.   y = int(input("enter second number: "))
  4.   result=x/y
  5. except ArithmeticError as e:
  6.    print("cannot devide a number by zero: ", e)
  7. finally:
  8.    print("finally block")

Output
  1. enter first number: 10
  2. enter second number: 0
  3. cannot devide a number by zero:  division by zero
  4. finally block

finally block python

Python try except else with example program

  • Please read below post to get an idea of try and except blocks in detail
  • Python try except example program
  • We will place all statements which are proven to generate exceptions in try block so that if any error occurred it will immediately enters into except and assign exception object to corresponding class.
  • If any exception occurs in try then except will be executed in order to handle the exception.
  • If no exception occurred in the try the it will executes else block.
  • So in Python exception handing else block will be executed if and only if no exceptions are occurred in try block and all the statements in try executed.
  • Lets see an example program on how can we use else in python exception handling
  • try-except-else 




#1: Write a python example program which explains usage of else block in exception handling of python programming.

  1. try:
  2.     x = int(input("enter 1st number: "))
  3.     y = int(input("enter 2nd number: "))
  4.     print(x/y)
  5.     string=input("enter some string: ")
  6.     print(string[8])
  7. except (IndexError, ZeroDivisionError) as e:
  8.     print("An error occurred :",e)
  9. else:
  10.     print("no error")

Output

  1. enter 1st number: 2
  2. enter 2nd number: 0
  3. An error occurred : division by zero

  4. enter 1st number: 2
  5. enter 2nd number: 1
  6. 2.0
  7. enter some string: python
  8. An error occurred : string index out of range

  9. enter 1st number: 2
  10. enter 2nd number: 1
  11. 2.0
  12. enter some string: python is very easy
  13. s
  14. no error  


python else example program

Python try except example program

  • Exceptions are the objects representing the logical errors that occur at run time.
  • When python script encounters any error at run time it will create python object.
  • So we need to handle this situation otherwise python program will terminates because of that run time error ( Exception Object).
  • So we can handle this by assigning this python exception object to corresponding python class.
  • For that Python provides try and except blocks to handle exceptions.
  • In try block we need to keep all the statements which may raise exceptions at run time.
  • except block will catch that exception and assigns to corresponding error class.
  • Lets see an example python program on exception handling using try and except blocks.




#1: Example Program to handle exception using try and except blocks in python programming.

  1. #use of try_catch in functions:
  2. def f():
  3.    x=int(input("enter some number: "))
  4.    print(x)

  5. try:
  6.     f()
  7.     print("no exception")
  8. except ValueError as e:
  9.     print("exception: ", e)
  10.     print("Rest of the Application")

Output

  1. enter some number: 2
  2. 2
  3. no exception
  4. >>> 



  5. enter some number: "python"
  6. exception:  invalid literal for int() with base 10: '"python"'
  7. Rest of the Application
  8. >>> 

try and except python example
Select Menu