Description
Exercise 1: Surface Area and Volume of a Hexagonal Pyramid
AIM:
Write a Python program that prompts the user for side length l of the base and height h in cm of a hexagonal pyramid (i.e. a pyramid with a hexagonal base and six isosceles triangular faces that intersect at the apex), computes the surface area A and volume V of the pyramid using the formulas
A and V , and finally outputs the results. Here are the sample input and output of this program:
Enter the side length l of the base of the pyramid in cm: 3
Enter the height h of the pyramid in cm: 4
The surface area of the pyramid is 66.3099499659424 cm^2.
The volume of the pyramid is 31.17691453623979 cm^3.
ALGORITHM:
PROGRAM:
OUTPUT:
2 Operation of Two Integers
Write a Python program that prompts for two integer operands and one of the binary operators +, −, , , or from the user, performs the operation on the operands with the operator using an ifelif-else statement, and finally prints the result. Your program should check whether the input operator is valid. Here are the sample input and output of this program:
Enter the integer A: 5
Enter the integer B: 4
Enter a binary operator (+,-,*,/,%): +
5 + 4 = 9
Enter the integer A: 2
Enter the integer B: 3
Enter a binary operator (+,-,*,/,%): =
Invalid input. The operator must be one of the followings: +,-,*,/,%.
ALGORITHM:
PROGRAM:
OUTPUT:
3 Taylor Series of x (1+x2)1/2
Write a Python program that prompts for a real number x in the open interval (−1, 1) and a positive integer n from the user, computes the sum of the first n terms of the Taylor series
n−1
x ∑ (i(−i!1))2i(−21i(2−i)1!) x2i+1
4 i=0
for any x (−1, 1) using a for loop, and finally output the result. Your program should check whether the user input is valid. Here are the sample input and output of this program:
Enter a real number x in (-1, 1): 0.6
Enter a positive integer n: 5
The sum of first 5 terms of the Taylor series of x*(1+x^2)^(1/2) for x = 0.6 is 0.6996359400000001
Enter a real number x in (-1, 1): 2.2
Enter a positive integer n: 7
Invalid input. x must have absolute value less than 1!
Enter a real number x in (-1, 1): -0.9
Enter a positive integer n: -5
Invalid input. n must be a positive integer!
ALGORITHM:
PROGRAM:
OUTPUT:
4 Greatest Common Divisor
The Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides both of them without leaving a remainder. An efficient way for finding the GCD of two natural numbers (i.e. non-negative integers) is the Euclidean algorithm which works as follows:
(a) If one of the numbers is zero, then the GCD is the other number and we can stop.
(b) Compute the remainder of the larger number divided by the smaller one and then replace the larger number by the remainder
(c) Repeat step (b) until the remainder is zero. The GCD is the larger number in this case.
Write a Python program that prompts the user for two natural numbers, find their GCD with the Euclidean algorithm using a while loop, an if-else statement, and if-elif-else statements, and finally outputs the result. Your program should check whether the user input is valid. Here are the sample input and output of this program:
Enter a natural number x: 168
Enter another natural number y: 180
The GCD of 168 and 180 is 12
Enter a natural number x: 25
Enter another natural number y: -10
Invalid input. Both x and y must be integers >= 0!
Enter a natural number x: 18
Enter another natural number y: 18
Invalid input. x and y must be different numbers!
ALGORITHM:
PROGRAM:
OUTPUT:
Exercise 5: Displaying a Number Pattern
Write a Python program that prompts the user for a line number n which is a positive integer < 10 and then displays a number pattern of 2n−1 lines with the following format:
1
121
12321
1234321
123454321
1234321
12321
121 1
using nested for loops. Your program should check whether the input value of n is valid.
ALGORITHM:
PROGRAM:
OUTPUT:




Reviews
There are no reviews yet.