0% found this document useful (0 votes)
24 views3 pages

Ex No 3

Uploaded by

reorffggjhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Ex No 3

Uploaded by

reorffggjhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SCIENTIFIC PROBLEMS USING CONDITIONALS AND ITERATIVE LOOPS

Ex. No: 3a
Number Series

Aim

To write a python program to calculate the number series.

Algorithm

Step 1: Start.
Step 2: Take a value from the user and store it in a variable n.
Step 3: Use a for loop where the value of i ranges between the values of 1 and n.
Step 4: Print the value of i and ‘+’ operator while appending the value of i to a list.
Step 5: Then find the sum of elements in the list.
Step 6: Print ‘=’ followed by the total sum.
Step 7: Stop.

Program

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


a=[]
for i in range(1,n+1):
print(i,sep=" ",end=" ")
if(i<n):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))

Output

Conclusion

Thus, the python program to calculate the number series is written and executed.
Ex. No: 3b
Number Pattern

Aim

To write a python program to print a number pattern.

Algorithm

Step 1: Start
Step 2: First, we get the height of the pyramid rows from the user.
Step 3: In the first loop, we iterate from i = 0 to i = rows.
Step 4: In the second loop, we print numbers starting from 1 to j, where j ranges from 0 to i.
Step 5: After each iteration of the first loop, we print a new line.
Step 6: Stop

Program

n=int(input("Enter the height for the number pattern: "))


for i in range(1,n+1):
print()
for j in range(1,i+1):
print(j,end=' ')

Output

Conclusion

Thus, the python program to print a number pattern is written and executed.
Ex. No: 3c
Pyramid Pattern

Aim

To write a python program to print a pyramid pattern.

Algorithm

Step 1: Start
Step 2: The outermost loop starts from i = 1 to i = row + 1.
Step 3: Among the two inner loops, the for loop prints the required spaces for each row using
formula (rows-i)+1, where rows is the total number of rows and i is the current row number.
Step 4: The while loop prints the required number stars using formula 2 * i - 1. This formula
gives the number of stars for each row, where row is i.
Step 5: Stop

Program

rows = int(input("Enter number of rows: "))


k=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k=0
print()

Output

Conclusion

Thus, the python program to print the pyramid pattern is written and executed.

You might also like