Professional Documents
Culture Documents
Week 2
Week 2
44
333
2222
11111
The program provided is designed to print a descending triangle pattern of numbers, where the
largest number starts at the top and decreases by 1 with each subsequent row, and the number of
PROGRAM:
n = int(input(“Enter Interger for Size of Triangle:”)) # This line prompts the user to enter an integer,
which sets the size of the triangle.
for i in range(n, 0, -1): # This outer loop iterates from 'n' down to 1.
for j in range(n, i - 1, -1): # The inner loop runs enough times to match the current number 'i'.
print(i, end=' ') # This prints the current number 'i', followed by a space, without moving to a new
line.
print() # After printing all numbers in a row, this moves to the next line for the next row of numbers.
OUTPUT:
44
333
2222
11111
When this program runs, it first waits for the user to input a number, which determines the
size of the triangle. For example, if the user inputs 5, the program will print a triangle that starts with
5 at the top, followed by a row of 4 4, then 3 3 3, and so on, until it reaches the last row of 1 1 1 1 1.
The print(i, end=' ') statement ensures that each number is printed with a space after it on the same
line, and the print() statement without any arguments is used to move to the next line after each row
2. .Write a program to check whether the given input is digit or lowercase character or uppercase
To check whether the given input is a digit, lowercase character, uppercase character, or a
special character using an 'if-else-if' ladder, you can use the following Python program. This program
prompts the user for an input and then checks the type of character using the built-in methods
isdigit(), islower(), and isupper() provided by Python. If none of these conditions match, it assumes the
PROGRAM:
if char.isdigit():
elif char.islower():
elif char.isupper():
else:
OUTPUT:
Enter a character: R
The character is an uppercase letter.
Enter a character: r
Enter a character: #
This code first checks if the input is a digit using isdigit(). If not, it then checks if it's a lowercase
character using islower(), followed by a check for an uppercase character using isupper(). If none of
these conditions are met, the program concludes that the input is a special character and prints the
appropriate message.
To print the Fibonacci sequence using a while loop in Python, you can follow the structure below.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding
ones, usually starting with 0 and 1. Here's how you can implement it:
Program:
a, b = 0, 1
count = 0
if n <= 0:
elif n == 1:
print(a)
else:
print("Fibonacci sequence:")
nth = a + b
# update values
a=b
b = nth
count += 1
OUTPUT:
0 1 1 2 3 5 8 13 21 34
This program first takes the number of terms (n) to be printed in the Fibonacci sequence
from the user. It initializes the first two terms of the Fibonacci sequence (a and b) as 0 and 1,
respectively. Using a while loop, it calculates each new term in the sequence by adding the previous
two terms and updates the values of a and b accordingly until it reaches the user-specified number of
terms.
4 . . Python program to print all prime numbers in a given interval (use break)
To print all prime numbers within a given interval using a break statement in Python, you can use the
following program. This program checks each number in the interval to see if it is prime. A number is
considered prime if it is greater than 1 and has no divisors other than 1 and itself.
Program:
if num > 1:
break
else:
OUTPUT:
2357
This program works by iterating through each number in the given interval (from start to
end, inclusive). For each number, it checks whether there is any divisor other than 1 and itself. It does
this by iterating from 2 up to the number itself (excluding the number) and checking if the remainder
of the division of the number by the current iterator is 0. If such a divisor is found, the loop is exited
using the break statement, indicating the number is not prime. If no such divisor is found, the else
block associated with the for loop is executed, indicating the number is prime, and the number is
printed.