Part 4: Conditional Program

Technical Terms-

Syntax: The set of rules that should be followed by the programmers while write programs.

Comments: Used to provide a piece of information about the code in the program.

Identifiers: The names that are defined by the users for variables, functions in the program.

Literals: Representations of fixed values in a program.

Keywords: The words which have their own specific function in the program.

Data type: Specifies the type of data that can be stored by the variable.

Variable: The name given to the memory location which is used to store values.

Operators: The sign or symbols which performs specific operations on constants and variables.

  

1. Check whether a number is positive or negative

num = float(input("Enter a number: "))

if num > 0:

    print("Positive")

else:

    print("Negative")

 

2. Check whether a number is positive, negative, or zero

num = float(input("Enter a number: "))

if num > 0:

    print("Positive")

elif num < 0:

    print("Negative")

else:

    print("Zero")

 

3. Display the greatest among two numbers

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

if a > b:

    print("Greatest number:", a)

else:

    print("Greatest number:", b)

 

4. Display the smallest among two numbers

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

if a < b:

    print("Smallest number:", a)

else:

    print("Smallest number:", b)

 

5. Display the greatest among three numbers

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if a > b and a > c:

    print("Greatest number:", a)

elif b > a and b > c:

    print("Greatest number:", b)

else:

    print("Greatest number:", c)

 

6. Display the smallest among three numbers

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if a < b and a < c:

    print("Smallest number:", a)

elif b < a and b < c:

    print("Smallest number:", b)

else:

    print("Smallest number:", c)

 

7. Display the middle number among three numbers

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if (a < b < c) or (c < b < a):

    print("Middle number:", b)

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

    print("Middle number:", a)

else:

    print("Middle number:", c)

 

8. Check whether a number is odd or even

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

if num % 2 == 0:

    print(num,"is even")

else:

    print(num,"is odd")

 

9. Check whether a number is divisible by 7

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

if num % 7 == 0:

    print("Divisible by 7")

else:

    print("Not divisible by 7")

 

10. Check whether triangle is equilateral, isosceles, or scalene

a = float(input("Enter first side: "))

b = float(input("Enter second side: "))

c = float(input("Enter third side: "))

if a == b == c:

    print("Equilateral Triangle")

elif a == b or b == c or a == c:

    print("Isosceles Triangle")

else:

    print("Scalene Triangle")

 

11. Check whether student is pass or fail (Pass mark: 40)

marks = float(input("Enter marks: "))

if marks >= 40:

    print("Pass")

else:

    print("Fail")

12. Profit or Loss based on Selling Price and Cost Price

cp = float(input("Enter Cost Price: "))

sp = float(input("Enter Selling Price: "))

if sp > cp:

    print("Profit =", sp - cp)

elif cp > sp:

    print("Loss =", cp - sp)

else:

    print("No Profit, No Loss")

 

Popular posts from this blog

Computer

Sequential Programs