1. Program to sort 3 numbers in descending order using if-else statements

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

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

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

 

if a>b:

    if b>c:

        print( "Numbers in sorted order:" ,a, " " ,b, " " ,c)

    else :

        if c>a:

            print( "Numbers in sorted order:" ,c, " " ,a, " " ,b)

        else :

            print( "Numbers in sorted order:" ,a, " " ,c, " " ,b)

elif b>a:

    if a>c:

          print( "Numbers in sorted order:" ,b, " " ,a, " " ,c)

    else :

            print( "Numbers in sorted order:" ,b, " " ,c, " " ,a)

else :

    print( "Numbers in sorted order:" ,c, " " ,b, " " ,a)

 

Output:

[Class 11] Important programs in Python - Code + Examples - Concepts

 

2. Program to generate the pattern given below by accepting the input number from user

part 2 - Important programs - Concepts - Chapter 6 Class 11 - Flow of Control - Computer Science - Class 11

 

num = int(input( "Enter a number to generate its pattern = " ))

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

    for j in range( 1 ,i + 1 ):

        print(j, end = " " )

    print()

 

Output:

part 3 - Important programs - Concepts - Chapter 6 Class 11 - Flow of Control - Computer Science - Class 11

 

3. Program to find sum of series

(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)

num = int(input( "Enter value for n = " ))

sum= 0

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

        sum += (i * i)

print( "Sum of series = " ,sum)

 

Output:

part 4 - Important programs - Concepts - Chapter 6 Class 11 - Flow of Control - Computer Science - Class 11

 

4. Program to find factorial of a given number

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

fact = 1

if num < 0 :

print( "Sorry, factorial does not exist for negative numbers" )

elif num == 0 :

print( "The factorial of 0 is 1" )

else:

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

        fact = fact * i

print("factorial of ", num, " is ", fact)

 

Output:

part 5 - Important programs - Concepts - Chapter 6 Class 11 - Flow of Control - Computer Science - Class 11

Remove Ads Share on WhatsApp
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo