Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

WEEK - 2

1. .Print the below triangle using for loop.

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

occurrences of each number in a row matches the number itself.

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:

Enter Integer for Size of Triangle: 5

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

of the triangle is completed.

2. .Write a program to check whether the given input is digit or lowercase character or uppercase

character or a special character (use 'if-else-if' ladder)

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

input is a special character.

PROGRAM:

# Program to check the type of character entered by the user

# Prompt the user to enter a character

char = input("Enter a character: ")

# Check if the character is a digit

if char.isdigit():

print("The character is a digit.")

# Check if the character is a lowercase letter

elif char.islower():

print("The character is a lowercase letter.")

# Check if the character is an uppercase letter

elif char.isupper():

print("The character is an uppercase letter.")

# If none of the above, it's a special character

else:

print("The character is a special character.")

OUTPUT:

Enter a character: R
The character is an uppercase letter.

Enter a character: r

The character is an lowercase letter.

Enter a character: #

The character is an Special 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.

3. .Python Program to Print the Fibonacci sequence using while loop

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:

# Python program to print the Fibonacci sequence using a while loop

n = int(input("Enter the number of terms: "))

a, b = 0, 1

count = 0

# Check if the number of terms is valid

if n <= 0:

print("Please enter a positive integer")

elif n == 1:

print("Fibonacci sequence up to", n, ":")

print(a)

else:

print("Fibonacci sequence:")

while count < n:


print(a, end=' ')

nth = a + b

# update values

a=b

b = nth

count += 1

OUTPUT:

Enter the number of terms: Fibonacci sequence:

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:

# Python program to print all prime numbers in a given interval

# Input for the interval

start = int(input("Enter start of interval: "))

end = int(input("Enter end of interval: "))

print(f"Prime numbers between {start} and {end} are:")

for num in range(start, end + 1):

# all prime numbers are greater than 1

if num > 1:

for i in range(2, num):


if (num % i) == 0:

break

else:

print(num, end=' ')

OUTPUT:

Enter start of interval: 0

Enter end of interval: 10

Prime numbers between 0 and 10 are:

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.

You might also like