Else statement in python with example program

Else Statement in Python:


  • The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement.
  • We can use the else statement with if statement to execute a block of code when the condition is false

if (condition):

    # Executes this block if

    # condition is true

else:

    # Executes this block if

    # condition is false


#1: Python program to illustrate If else statement
  1.  i = 2;
  2. if i< 1:
  3.     print ("i is smaller than 1")
  4.     print ("i'm in if Block")
  5. else:
  6.     print ("i is greater than 1")
  7.     print ("i'm in else Block")
  8. print ("i'm not in if and not in else Block")

Output:
  1. I is greater than 1
  2. I'm in else Block
  3. I'm not in if and not in else Block



Fibonacci series in c without recursion

  • In Fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. For example Fibonacci series is 0, 1, 1, 2, 3, 5, 8,13, 21
  • C Program to Display Fibonacci Sequence 
  • Fibonacci series means next number will be generated as sum of previous two numbers.

#1: C example program on Fibonacci series

  1. #include <stdio.h>

  2. void main()
  3. {
  4.     int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;

  5.     printf("Enter the limit to generate the Fibonacci Series \n");
  6.     scanf("%d", &limit);
  7.     printf("Fibonacci Series is ...\n");
  8.     printf("%d\n", fib1);
  9.     printf("%d\n", fib2);
  10.     count = 2;
  11.     while (count < limit)
  12.     {
  13.         fib3 = fib1 + fib2;
  14.         count++;
  15.         printf("%d\n", fib3);
  16.         fib1 = fib2;
  17.         fib2 = fib3;
  18.     }
  19. }
Output:
  1. Enter the limit to generate the Fibonacci Series
  2. 20
  3. Fibonacci Series is ...
  4. 0
  5. 1
  6. 1
  7. 2
  8. 3
  9. 5
  10. 8
  11. 13
  12. 21
  13. 34
  14. 55
  15. 89
  16. 144
  17. 233
  18. 377
  19. 610
  20. 987
  21. 1597
  22. 2584
  23. 4181


If statement in python example program

Decision Making In Python (IF):


  • As we lead our life we have to take decisions. Similarly, as we make progress in programming and try to implement complicated logics, our program has to take decisions. But how? Well the answer is below
  • if statement
  • If statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.


 If condition:
Statement 1
Statement 2
Statement 3
………………..
…………………
………………..
Statement n

  • Here the condition is evaluated in terms of boolean values i.e. either the evaluation of condition results 1 or 0 i.e.. True(1) or False(0) 
  • If the condition evaluates to True(1) then the block of statements below the if statement gets executed 



#1: Python example program to illustrate if statement

  1. #Python program to illustrate if statement
  2. a=21
  3. If a<21:
  4. print(“if-1 statement is executed successfully”)
  5. If a>18:
  6. print(“if-2 statement is executed successfully”)

  7. Output:
  8. if-2 statement is executed successfully

Output:
  1. if-2 statement is executed successfully


Prime number program in c using for loop

Prime number program in C
  • A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole number that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. 
  • Numbers that have more than two factors are called composite numbers.

#1:  Prime number program in c using for loop
  1. #include <stdio.h>
  2. void main()
  3. {
  4.   int n1, n2,j, flag;
  5.   long int  i;
  6.   printf("Enter First  numbers(intevals): ");
  7.   scanf("%d", &n1);
  8.   printf("Enter Second  numbers(intevals): ");
  9.   scanf("%d", &n2);
  10.   printf("Prime numbers between %d and %d are: ", n1, n2);
  11.   for(i=n1+1; i<n2; ++i)
  12.   {
  13.       flag=0;
  14.       for(j=2; j<=i/2; ++j)
  15.       {
  16.         if(i%j==0)
  17.         {
  18.           flag=1;
  19.           break;
  20.         }
  21.       }
  22.       if(flag==0)
  23.         printf("%ld ",i);
  24.   }
  25. }


Output:
  1. Enter First  numbers(intevals): 1
  2. Enter Second  numbers(intevals): 500
  3. Prime numbers between 1 and 500 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
  4. 71 73 79 83 89 97 101 103 107
  5. 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
  6. 233 239 241 251 257 263 269 271
  7. 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
  8. 419 421 431 433 439 443 449 457
  9. 461 463 467 479 487 491 499
  10. Process returned 500 (0x1F4)   execution time : 4.833 s
  11. Press any key to continue.


prime numbers program


Switch case in c example program

  • switch case in c programming questions
  • switch case with do you want to continue in c
  • A switch case is a control statement in C that allows you to choose between several different options based on the value of a particular expression. 
  • C has a built-in multi-way decision statement known as a switch. It is a keyword we can select a single option from multiple options. The switch  is having expression; each and every option is known as case and must be followed by colon.
  • Each and every case ends with break statement.
  • The last case is default even that will have break.
  •  The break is a keyword. Each and every case ends with break statement

 SYNTAX:-

 switch(expression)
 {
    case value-1 :block-1 break;
    case value-2 :block-2 break;
    case value-3 :block-3 break;
    .........
    .........
    .........
    .........
    default :default-block break;
 }

Here is an example of how to use a switch case in a C program:

#1; switch case in c programming questions

  1. # include <stdio.h>
  2. void main() {
  3.     char operator;
  4.     double a,b;
  5.     printf("Enter an operator (+, -, *,): ");
  6.     scanf("%c", &operator);
  7.     printf("Enter two numbers ");
  8.     scanf("%lf %lf",&a, &b);
  9.     switch(operator)
  10.     {
  11.         case '+':
  12.             printf("%.1lf + %.1lf = %.1lf",a, b, a + b);
  13.             break;
  14.         case '-':
  15.             printf("%.1lf - %.1lf = %.1lf",a, b, a - b);
  16.             break;
  17.         case '*':
  18.             printf("%.1lf * %.1lf = %.1lf",a, b, a * b);
  19.             break;
  20.         case '/':
  21.             printf("%.1lf / %.1lf = %.1lf",a, b, a / b);
  22.             break;
  23.         default:
  24.             printf("Error! operator is not correct");
  25.     }
  26. }

Output:
  1. Enter an operator (+, -, *,): +
  2. Enter two numbers 12
  3. 12
  4. 12.0 + 12.0 = 24.0
  5. Process returned 18 (0x12)   execution time : 7.392 s
  6. Press any key to continue.



  • In the below  example, the program prompts the user to enter a number and then uses a switch case to determine what to do based on the value of the number. If the user enters 1, the program will print "You entered 1.
  • " If the user enters 2, the program will print "You entered 2," and so on. If the user enters a value that is not covered by any of the cases, the default case will be executed.



Python program to find factorial without recursion

The factorial of a positive integer n is the product of all positive integers less than or equal to n. The factorial of a number is represented by the symbol "!" . For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.

Here is a Python function that calculates the factorial of a given number using a for loop:

def factorial(n):
    if n < 0:
        return None
    if n == 0:
        return 1
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

This function takes a single argument, n, and returns the factorial of n. It first checks if n is less than 0, if so it returns None. If n is equal to 0, it returns 1 (since the factorial of 0 is defined to be 1). It then initializes a variable named result to 1, then uses a for loop to iterate over the range of integers from 1 to n inclusive and multiply each number to result variable. Finally, the function returns the value of result, which is the factorial of n.

You can call this function and pass in any positive integer to calculate its factorial:

>>> factorial(5)
120
>>> factorial(3)
6
>>> factorial(10)
3628800

Alternatively python has math.factorial function which you can use without writing your own function.

import math
math.factorial(5)

Do note that factorial of number can be very large, even for relatively small numbers and python integers may not be large enough to store those values.

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

  1. #Factorial without recursion

  2. n=int(input("Enter the number: "))
  3. fact=1
  4. if n<0:
  5.     print("factorial doesn't exist for negative numbers")
  6. else:
  7.     for i in range(1,n+1):
  8.         fact=fact*i
  9.     print("factorial of", n, "is", fact)

Output:

  1. Enter the number: 5
  2. factorial of 5 is 120





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


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


Select Menu