02. Control Instructions

Problem

A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.

# Check whether a number and its reversed number are equal
revnum = 0 
n = int(input('Enter a 5-digit number: ')) 
num = n 
a = n % 10 
n = n // 10 
revnum = revnum + a * 10000 
a = n % 10 
n = n // 10 
revnum = revnum + a * 1000
a = n % 10 
n = n // 10 
revnum = revnum + a * 100
a = n % 10 
n = n // 10 
revnum = revnum + a * 10 
a = n % 10 
revnum = revnum + a 
if revnum == num :
    print('Given no. & its reversed number are equal') 
else :
    print('Given no. & its reversed number are not equal')

Interaction

Enter a 5-digit number: 22122
Given no. & its reversed number are equal

Problem

If ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

# Find the youngest amongst three friends 
r = int(input('Enter age of Ram: '))
s = int(input('Enter age of Shyam: '))
a = int(input('Enter age of Ajay: '))
if r < s < a :
    young = r
elif r > s < a :
    young = s 
else :
    young = a 
print('The youngest of Ram, Shyam and Ajay is = ', young)

Interaction

Enter age of Ram: 23
Enter age of Shyam: 11
Enter age of Ajay: 25
The youngest of Ram, Shyam and Ajay is =  11

Problem

Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

# Check whether a triangle is valid or not 
angle1 = float(input('Enter first angle of the triangle: '))
angle2 = float(input('Enter second angle of the triangle: '))
angle3 = float(input('Enter third angle of the triangle: ')) 
if angle1 + angle2 + angle3 == 180 :
    print('The triangle is a valid triangle') 
else :
    print('The triangle is an invalid triangle')

Interaction

Enter first angle of the triangle: 45
Enter second angle of the triangle: 45
Enter third angle of the triangle: 90
The triangle is a valid triangle

Problem

Write a program to find the absolute value of a number entered through the keyboard.

# To find absolute value of number entered through keyboard
no = int(input('Enter any number: ')) 
if no < 0 :
    no = no * (-1) 
print('The absolute value of given number is = ', no)

Interaction

Enter any number: -45
The absolute value of given number is =  45

Problem

Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.

# Find whether area of rectangle is greater than its perimeter
l = int(input('Enter length of the rectangle: '))
b = int(input('Enter breadth of the rectangle: '))
area = l * b 
peri = 2 * (l + b) 
if area > peri :
    print('Area is greater than perimeter') 
else :
    print('Area is lesser than perimeter')

Interaction

Enter length of the rectangle: 5
Enter breadth of the rectangle: 8
Area is greater than perimeter

Problem

Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

# Check whether three points are co-linear
x1 = int(input('Enter the values of x1 coord. of first point: '))
y1 = int(input('Enter the values of y1 coord. of first point: '))
x2 = int(input('Enter the values of x2 coord. of second point: '))
y2 = int(input('Enter the values of y2 coord. of second point: '))
x3 = int(input('Enter the values of x3 coord. of third point: '))
y3 = int(input('Enter the values of y3 coord. of third point: '))
s1 = abs(x2 - x1) / abs(y2 - y1)   
s2 = abs(x3 - x1) / abs(y3 - y1)  
s3 = abs(x3 - x2) / abs(y3 - y2) 
if s1 == s2 and s1 == s3 :
    print ('Points are Co-linear') 
else :
    print ('Points are NOT Co-linear')

Interaction

Enter the values of x1 coord. of first point: 2
Enter the values of y1 coord. of first point: 3
Enter the values of x2 coord. of second point: 4
Enter the values of y2 coord. of second point: 5
Enter the values of x3 coord. of third point: 6
Enter the values of y3 coord. of third point: 7
Points are Co-linear

Problem

Given the coordinates (x, y) of center of a circle and its radius, write a program that will determine whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( ) and pow( ) functions)

# Determine position of point with respect to a circle 
# The center of the circle has been assumed to be at (0, 0)
r = int(input('Enter radius of circle: '))
x = int(input('Enter x coordinate of point (x, y): '))
y = int(input('Enter y coordinate of point (x, y): '))
dis = x * x + y * y 
d = r * r 
if dis == d :
    print('Point is on the circle') 
elif dis > d :
    print('Point is outside the circle') 
else :
    print('Point is inside the circle')

Interaction

Enter radius of circle: 4
Enter x coordinate of point ( x, y ): 2
Enter y coordinate of point ( x, y ): 3
Point is inside the circle

Problem

Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or on the origin.

# Determine position of a point with respect to X and Y axes
x = int(input('Enter the x coordinate of a point: '))
y = int(input('Enter the y coordinate of a point: ')) 
if x == 0 and y == 0 :
    print('Point lies on origin') 
elif x == 0 and y != 0 :
    print('Point lies on Y axis') 
elif x != 0 and y == 0 :
    print('Point  lies on X axis') 
else :
    print('Point neither lies on any axis, nor origin' )

Interaction

Enter the x coordinate of a point: 3
Enter the y coordinate of a point: 4
Point neither lies on any axis, nor origin

Problem

According to Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

# Calculate the day on 1st January of any year 
yr = int(input('Enter year: ')) 
normaldays = (yr - 1) * 365
leapdays = (yr - 1) // 4 - (yr - 1) // 100 + (yr - 1) // 400 
totaldays = normaldays + leapdays 
firstday = totaldays % 7 
if firstday == 0 :
    print('Monday') 
if firstday == 1 :
    print('Tuesday') 
if firstday == 2 :
    print('Wednesday') 
if firstday == 3 : 
    print('Thursday') 
if firstday == 4 :
    print('Friday') 
if firstday == 5 :
    print('Saturday')
if firstday == 6 :
    print('Sunday')

Interaction

Enter year: 1996
Monday

Problem

If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right-angled triangle.

# Determine the type of triangle 
s1 = int(input('Enter first side of a triangle: '))
s2 = int(input('Enter second side of a triangle: '))
s3 = int(input('Enter third side of a triangle: '))
if s1 != s2 and s2 != s3 and s3 != s1 :
    print('Scalene triangle') 
if s1 == s2 and s2 != s3 :
    print('Isosceles triangle') 
if s2 == s3 and s3 != s1 :
    print('Isosceles triangle') 
if s1 == s3 and s3 != s2 :
    print('Isosceles triangle') 
if s1 == s2 and s2 == s3 :
    print('Equilateral triangle') 
a = (s1 * s1) == (s2 * s2) + (s3 * s3) 
b = (s2 * s2) == (s1 * s1) + (s3 * s3) 
c = (s3 * s3) == (s1 * s1) + (s2 * s2) 
if a or b or c :
    print('Right-angled triangle')

Interaction

Enter first side of a triangle: 3
Enter second side of a triangle: 3
Enter third side of a triangle: 3
Equilateral triangle

Problem

In digital world colors are specified in Red-Green-Blue (RGB) format, with values of R, G, B varying on an integer scale from 0 to 255. In print publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with values of C, M, Y, and K varying on a real scale from 0.0 to 1.0. Write a program that converts RGB color to CMYK color as per the following formulae:

White = Max(Red/255,Green/ 255,Blue/255)
Cyan = (White – (Red/255)/white)
Magenta = (White – (Green/255)/White)
Yellow = (White – (Green/255)/White)
Black = 1 -White

Note that if the RGB values are all 0, then the CMY values
are all 0 and the K value is 1.

# Color conversion from RGB to CMYK format
red = int(input('Enter Red values (0 to 255): '))
green = int(input('Enter Green values (0 to 255): '))
blue = int(input('Enter Blue values (0 to 255): '))
if red == 0 and green == 0 and blue == 0 :
    cyan = magenta = yellow = 0 
    black = 1 
if red > 0 and green > 0 and blue > 0 :
    red = red / 255 
    green = green / 255 
    blue = blue / 255 
    max = red
    if green > max :
        max = green
    if blue > max :
        max = blue 
    white = max 
    cyan = (white - red) / white 
    magenta = (white - green) / white 
    yellow = (white - blue) / white 
    black = 1 - white 
print('cyan = ', cyan, '\nmagenta = ', magenta, '\nyellow = ', yellow, '\nblack = ', black)

Interaction

Enter Red values (0 to 255): 23
Enter Green values (0 to 255): 120
Enter Blue values (0 to 255): 10
cyan =  0.8083333333333333 
magenta =  0.0 
yellow =  0.9166666666666667 
black =  0.5294117647058824

Problem

A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600

The grades are as follows:

Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met

Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

# Check the grade of steel
hard = float(input('Enter Hardness of steel: '))
carbon = float(input('Enter Carbon content: '))
tensile = float(input('Enter Tensile strength: '))
if hard > 50 and carbon < 0.7 and tensile > 5600 :
    print('Grade 10') 
    exit(0)
if hard > 50 and carbon < 0.7 and tensile <= 5600 :
    print('Grade 9')
    exit(0)
if hard <= 50 and carbon < 0.7 and tensile > 5600 :
    print('Grade 8')
    exit(0)
if hard > 50 and carbon >= 0.7 and tensile > 5600 :
    print('Grade 7')
    exit(0)
if hard > 50 or carbon < 0.7 or tensile > 5600 :
    print('Grade 6')
    exit(0)
else :
    print('Grade 5')
    exit(0)

Interaction

Enter Hardness of steel: 60
Enter Carbon content: 0.6
Enter Tensile strength: 5700
Grade 10

Problem

The Body Mass Index (BMI) is defined as ratio of the weight of a person (in kilograms) to the square of the height (in meters). Write a program that receives weight and height, calculates the BMI, and reports the BMI category as per the following table:

# Determine BMI category
wt = float(input('Enter weight in kg: '))
ht = float(input('Enter height in meters: '))
bmi = wt / (ht * ht) 
print('Body Mass Index = ', bmi)
if bmi < 15 :
    print('BMI Category: Starvation') 
elif bmi < 17.5 :
    print('BMI Category: Anorexic\n')
elif bmi < 18.5 : 
    print('BMI Category: Underweight') 
elif bmi < 25 :
    print('BMI Category: Ideal')
elif bmi < 30 :
    print('BMI Category: Overweight') 
elif bmi < 40 :
    print('BMI Category: Obese') 
else :
    print('BMI Category: Morbidly Obese')

Interaction

Enter weight in kg: 58
Enter height in meters: 1.54
Body Mass Index =  24.456063417102378
BMI Category: Ideal

Problem

Whether the character entered through the keyboard is a lower case alphabet or not.

# Determine character case using conditional operators 
ch = input('Enter character: ')   
print('Character is lower case') if ch >= 'a' and ch <= 'z' else print('Character is not lower case')

Interaction

Enter character: a
Character is lower case

Problem

Whether a character entered through the keyboard is a special symbol or not.

# Determine whether a character is a special symbol 
ch = input('Enter character: ')   
print('Character entered is not a special symbol') if ch >= 'A' and ch <= 'Z' or ch >= 'a' and ch <= 'z' or ch >= '0' and ch <= '9' else print('Character entered is a special symbol')

Interaction

Enter character: @
Character entered is a special symbol

Problem

Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.

# Determine whether a year is leap or not 
year = int(input('Enter Year: '))
print('Leap Year') if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else print('Not a Leap Year')

Interaction

Enter Year: 2004
Leap Year

Problem

Write a program to find the greatest of three numbers entered through the keyboard. Use conditional operators.

# Determine greatest of 3 numbers using conditional operators 
n1 = int(input('Enter any numbers: '))
n2 = int(input('Enter any numbers: '))
n3 = int(input('Enter any numbers: '))
great = n1 if n1 > n2 and n1 > n3 else n2 if n2 > n3 else n3    
print('Greatest number is = ' , great)

Interaction

Enter any numbers: 45
Enter any numbers: 74
Enter any numbers: 12
Greatest number is = 74

Problem

Write a program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

# Determine whether sum of squares of sine and cosine of an angle is equal to 1 
import math 
angle = float(input('Enter angle in degrees: ')) 
sum = pow(math.sin(angle), 2) + pow(math.cos(angle), 2) 
if sum == 1 :
    print('Sum of squares of sin & cos is equal to 1') 
else :
    print('Sum of squares of sin & cos is not equal to 1')

Interaction

Enter angle in degrees: 45
Sum of squares of sin & cos is equal to 1

Problem

Rewrite the following programs using conditional operators.

sal = float ( input ( ‘Enter the salary: ‘ ))
if sal >= 25000 and sal <= 40000 : print ( ‘Manager’ ) elif sal >= 15000 and sal < 25000 :
print ( ‘Accountant’ )
else :
print ( ‘Clerk’ )

sal = float(input('Enter the salary: ' ))
print('Manager') if sal >= 25000 and sal <= 40000 else print('Accountant') if sal >= 15000 and sal < 25000 else print('Clerk')

Interaction

Enter the salary: 24000
Accountant

Problem

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

# Print ASCII values and their corresponding characters
ch = 1
while ch <= 255 :
    print(ch, '=', chr(ch)) 
    ch += 1

Interaction

1 = 
2 = 
.....
253 = ý
254 = þ
255 = ÿ

Problem

Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ).

# Generate all Armstrong numbers between 1 & 500 
print('Armstrong numbers between 1 & 500 are: ') 
i = 1
while i <= 500 :
    a = i % 10 
    b = i % 100 
    b = (b - a) // 10 
    c = i // 100
    if a * a * a + b * b * b + c * c * c == i :
        print(i)
    i += 1

Interaction

Armstrong numbers between 1 & 500 are: 
1
153
370
371
407

Problem

Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:

  • There are 21 matchsticks.
  • The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
  • After the person picks, the computer does its picking.
  • Whoever is forced to pick up the last matchstick loses the game.
# Match stick game
m = 21
while 1 :
    print('No. of matches left = ', m) 
    p = int(input('Pick up 1, 2, 3 or 4 matches = '))
    if p > 4 or p < 1 :
        continue
    m = m - p 
    print('No. of matches left = ', m) 
    c = 5 - p 
    print('Out of which computer picked up = ', c) 
    m = m - c 
    if m == 1 :
        print('Number of matches left = ', m)
        print('You lost the game !!' )
        break

Interaction

No. of matches left =  21
Pick up 1, 2, 3 or 4 matches = 3
No. of matches left =  18
Out of which computer picked up = 2
No. of matches left =  16
Pick up 1, 2, 3 or 4 matches = 3
No. of matches left =  13
Out of which computer picked up = 2
No. of matches left =  11
Pick up 1, 2, 3 or 4 matches = 2
No. of matches left =  9
Out of which computer picked up = 3
No. of matches left =  6
Pick up 1, 2, 3 or 4 matches = 2
No. of matches left =  4
Out of which computer picked up = 3
Number of matches left = 1
You lost the game !!

Problem

Write a program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.

# Count number of positives, negatives and zeros 
ans = 'y'
pos = neg = zero = 0
while ans == 'y' or ans == 'Y' :
    num = int(input('Enter a number: '))
    if num == 0 :
        zero += 1
    elif num > 0 :
        pos += 1
    elif num < 0 :
        neg += 1
    ans = input('Do you want to continue? ') 
print('You entered', pos, 'positive numbers') 
print('You entered', neg, 'negative numbers') 
print('You entered', zero, 'zeros')

Interaction

Enter a number: 34
Do you want to continue? y
Enter a number: 100
Do you want to continue? y
Enter a number: -1
Do you want to continue? y
Enter a number: 0
Do you want to continue? y
Enter a number: 0
Do you want to continue? y
Enter a number: -89
Do you want to continue? n
You entered 2 positive numbers
You entered 2 negative numbers
You entered 2 zeros

Problem

Write a program to receive an integer and find its octal equivalent. Hint: To obtain octal equivalent of an integer, divide it continuously by 8 till dividend doesn’t become zero, then write the remainders obtained in reverse direction.

# Find octal equivalent of a number
n1 = int(input('Enter any number: ')) 
n2 = p = oct = 0 
n2 == n1 
while n1 > 0 :
    rem = n1 % 8 
    n1 = n1 // 8 
    oct = oct + rem * pow(10.0, p) 
    p += 1
print('The octal equivalent of', n2,'is = ', oct)

Interaction

Enter any number: 24
The octal equivalent of 0 is =  30.0

Problem

Write a program to find the range of a set of numbers. Range is the difference between the smallest and biggest number in the list.

# Program to find the range of a set of numbers 
big = small = rang = 0
n = int(input('How many numbers are there in a set? '))
while n > 0 :
    no = int(input('Enter no: '))
    if no == 0 :
        small = big = no 
    elif no > big :
        big = no 
    elif no < small :
        small = no
    n -= 1
if small < 0 :
    rang = small + big 
else:
    rang = big - small 
if rang < 0 :
    rang = rang * -1 
print('The range of given set of numbers is = ', rang)

Interaction

How many numbers are there in a set? 5
Enter no: 23
Enter no: 100
Enter no: 56
Enter no: 10
Enter no: 9
The range of given set of numbers is = 100

Problem

Write a program to print the multiplication table of the number entered by the user. The table should get displayed in the following form:
29 * 1 = 29
29 * 2 = 58

# Generate and print table of a given number 
num = int(input('Enter the number: ')) 
for i in range(1, 11) :
    print(num, 'x', i, '=', num * i)

Interaction

Enter the number: 10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Problem

According to a study, the approximate level of intelligence of a person can be calculated using the following formula:
i = 2 + ( y + 0.5 x )
Write a program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.

# Generate and print intelligence table 
import numpy
for y in range(1, 7) :
    for x in numpy.arange(5.5, 13.5) :
        i = 2 + (y + 0.5 * x)
        print ('y =' , y, 'x =' , x, 'i =' , i)
        x += 0.5
    y += 1

Interaction

y = 1 x = 5.5 i = 5.75                                                                        
y = 1 x = 6.5 i = 6.25
...                                                                      
y = 6 x = 12.5 i = 14.25

Problem

When interest compounds q times per year at an annual rate of r % for n years, the principal p compounds to an amount a as per the following formula

a = p ( 1 + r / q ) nq

Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.

# Compound interest calculation
for i in range(10) :
    p = float(input('Enter value of p: ')) 
    r = float(input('Enter value of r: ')) 
    n = float(input('Enter value of n: '))  
    q = float(input('Enter value of q: '))
    a = float(p * (1 + r / q) ** (n * q))
    print('Compound Interest = Rs.'+str(a))

Interaction

Enter value of p: 100
Enter value of r: 1
Enter value of n: 2
Enter value of q: 2
Compound Interest = Rs.506.25
...

Problem

The natural logarithm can be approximated by the following series.

x-1/x + 1/2(x -1/x)^2 + 1/2(x -1 /x)^3 + 1/2(x – 1/x)^4 + …..
If x is input through the keyboard, write a program to calculate the sum of first seven terms of this series.

# Compute natural logarithm 
result = 0 
x = int(input('Enter the value of x: '))
for i in range(1, 8) :
    if i == 1 :
        result = result + pow((x - 1.0) / x, i) 
    else :
        result = result + (1.0 / 2) * pow((x - 1.0) / x, i) 
print('Log', x, '=', result)

Interaction

Enter the value of x: 3
Log 3 = 1.274805669867398

Problem

Write a program to generate all Pythagorean Triplets with side length less than or equal to 30.

# Generate Pythagorean Triplets
n = 31
print('Pythagorean Triplet:')
for i in range(1, n) :
	for j in range((i + 1), (n + 1), 1) :
		t = (i * i) + (j * j) 
		for k in range((i + 2), (n + 1), 1) :
			if t == k * k :
				print(i , j , k)

Interaction

Pythagorean Triplet:
3 4 5
5 12 13
6 8 10
7 24 25
8 15 17
9 12 15
10 24 26
12 16 20
15 20 25
18 24 30
20 21 29

Problem

Population of a town today is 100000. The population has increased steadily at the rate of 10 % per year for last 10 years. Write a program to determine the population at the end of each year in the last decade.

# Determine population growth over last decade
population = 100000
for i in range(10) :
	population += (int(population * 10 / 100))
	print('Year', i+1, ':', population)

Interaction

Year 1 : 110000
Year 2 : 121000
Year 3 : 133100
Year 4 : 146410
Year 5 : 161051
Year 6 : 177156
Year 7 : 194871
Year 8 : 214358
Year 9 : 235793
Year 10 : 259372

Problem

Ramanujan number is the smallest number that can be expressed as sum of two cubes in two different ways. Write a program to print all such numbers up to a reasonable limit.

# Generate Ramanujan numbers 
i = j = k = l = 1
print('Ramanujan Numbers: ')
for i in range(1, 31) :
    for j in range(1, 31) :
        for k in range(1, 31) :
            for l in range(1, 31) :
                if i != j and i != k and i != l and j != k and j != l and k != l :
                    if i * i * i + j * j * j == k * k * k + l * l * l :
                        print(i, j, k, l)

Interaction

Ramanujan Numbers:
1 12 9 10
1 12 10 9
2 16 9 15
2 16 15 9
2 24 18 20
2 24 20 18
9 10 1 12
9 10 12 1
9 15 2 16
9 15 16 2
10 9 1 12
10 9 12 1
10 27 19 24
10 27 24 19
12 1 9 10
12 1 10 9
15 9 2 16
15 9 16 2
16 2 9 15
16 2 15 9
18 20 2 24
18 20 24 2
19 24 10 27
19 24 27 10
20 18 2 24
20 18 24 2
24 2 18 20
24 2 20 18
24 19 10 27
24 19 27 10
27 10 19 24
27 10 24 19

Problem

Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.

# Print hours of the day with suitable suffixes
for hours in range(24) :
	if hours == 0 :
   		print('12 Midnight')
	if hours < 12 :
  		print(hours,'AM')
	if hours == 12 :
		print('12 Noon')
	if hours < 12 :
  		print(hours % 12,'PM')

Interaction

12 Midnight
0 AM
0 PM
1 AM
1 PM
2 AM
2 PM
3 AM
3 PM
4 AM
4 PM
5 AM
5 PM
6 AM
6 PM
7 AM
7 PM
8 AM
8 PM
9 AM
9 PM
10 AM
10 PM
11 AM
11 PM
12 Noon

Problem

Write a program to produce the following output:

            1               
        2       3           
    4       5       6       
7       8       9       10  
# Produce the given pattern 
number = 1
size = 4
m = (2 * size) - 2
for i in range(0, size) :
    for j in range(0, m) :
        print(end = ' ')
    m = m - 1
    for j in range(0, i + 1) :
        print(number, end = ' ')
        number += 1
    print(" ")

Interaction

	  1  
     2 3  
    4 5 6  
   7 8 9 10  

Problem

Write a program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in. Use the following logic:

  • If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
  • If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 4 marks per subject.
  • If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. Otherwise the grace is of 5 marks.
# Determine the grace marks obtained by student 
c = int(input('Enter the class: '))
sub = int(input('Enter the number of subjects failed: ')) 
while c :
    if c == 1 :
        if sub <= 3 :
            print('Student gets total of', 5 * sub, 'grace marks')
        else :
            print('No grace marks')
        break 

    if c == 2 :
        if sub <= 2 :
            print('Student gets total of', 4 * sub, 'grace marks')
        else :
            print('No grace marks')
        break 

    if c == 3 :
        if sub == 1 :
            print('Student gets 5 grace marks')
        else :
            print('No grace marks')
        break

    if c > 3 or c <= 0 :
        print('Wrong class entered')
        break

Interaction

Enter the class: 2
Enter the number of subjects failed: 2
Student gets total of 8 grace marks

Problem

xyz

Interaction