0% found this document useful (0 votes)
30 views

Loops Assignment

The document contains 10 programming problems involving loops in Python. The problems cover topics such as checking if a number is prime, determining the case of a character, calculating sums and factorials with loops, printing patterns with nested loops, calculating the Fibonacci sequence, checking if angles form a triangle, and finding the lowest common multiple and highest common factor of two numbers. The document provides the Python code to solve each problem and sample outputs.

Uploaded by

DAKSH SACHDEV
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)
30 views

Loops Assignment

The document contains 10 programming problems involving loops in Python. The problems cover topics such as checking if a number is prime, determining the case of a character, calculating sums and factorials with loops, printing patterns with nested loops, calculating the Fibonacci sequence, checking if angles form a triangle, and finding the lowest common multiple and highest common factor of two numbers. The document provides the Python code to solve each problem and sample outputs.

Uploaded by

DAKSH SACHDEV
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

Loops Assignment

-By Daksh Sachdev XI - A

1.Write a program to enter a number and check if it is a prime number or not.

In [ ]: num = int(input("Enter a number:"))


if num > 1:
for i in range(2,num//2 + 1):
if num%i==0:
print(num,"is a composite number")
break
else:
print(num,"is a prime number")
else:
print(num,"is neither prime nor composite")

Enter a number:3697
3697 is a prime number

2. Write a program to print whether a given character is an uppercase or a lowercase


character or a digit or any other character.

In [ ]: char = input("Enter a character: ")


if char.isupper():
print(char,"is an Uppercase character.")
elif char.islower():
print(char,"is a Lowercase character.")
elif char.isdigit():
print(char, "is a Digit.")
else:
print(char, "is an other character.")

Enter a character: D
D is an Uppercase character.

3. Write a program to print the sum of natural numbers between 1 to 7. Print the sum
progressively i.e. after adding each natural number,print the sum so far.

In [ ]: sum = 0
for i in range(1, 8):
sum +=i
print("Sum so far=",sum)
print("Final Sum:",sum)

Sum so far= 1
Sum so far= 3
Sum so far= 6
Sum so far= 10
Sum so far= 15
Sum so far= 21
Sum so far= 28
Final Sum: 28

4.Write a program to calculate the factorial of a number.


In [ ]: num = int(input("Enter a number: "))
factorial=1
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
for i in range(1, num + 1):
factorial *= i
print("The factorial of ",num, "is:",factorial)

Enter a number: 8
The factorial of 8 is: 40320

5.Write a program to create a triangle of stars using a nested loop.

In [ ]: for i in range(1,7):
print()
for j in range(1,i):
print('*',end=" ")

*
* *
* * *
* * * *
* * * * *

6.Write a Python script to print Fibonacci series’ first 20 elements

In [ ]: a, b = 0, 1
for i in range(20):
print(a, end=" ")
a, b = b, a + b

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

7.Input three angles and determine if they form a triangle or not.

In [ ]: a=eval(input('Enter the first angle in degree measure:'))


b=eval(input('Enter the second angle in degree measure:'))
c=eval(input('Enter the third angle in degree measure:'))
if a+b+c==180:
print('The angles form a triangle')
else:
print('The angles do not form a triangle')

Enter the first angle in degree measure:50


Enter the second angle in degree measure:90
Enter the third angle in degree measure:40
The angles form a triangle

8.Write a python script to print the following pattern.

In [ ]: for a in range(3,10,2):
print()
for b in range(1,a,2):
print(b, end=" ")

1
1 3
1 3 5
1 3 5 7
9.Write a python script to input two numbers and print their LCM and HCF.

In [ ]: x=int(input('Enter the first number:'))


y=int(input('Enter the second number:'))
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if x%i==0 and y%i==0:
hcf=i
lcm=(x*y)/hcf
print('The HCF of',x,'and',y,'is:',hcf)
print('The LCM of',x,'and',y,'is:',lcm)

Enter the first number:21


Enter the second number:56
The HCF of 21 and 56 is: 7
The LCM of 21 and 56 is: 168.0

10. Write a program to print a pattern like:

In [ ]: for i in range(0,4):
print()
for j in range(4,i,-1):
print(j,end= " ")

4 3 2 1
4 3 2
4 3
4

In [ ]: !jupyter nbconvert --to html /content/LoopsAssignment.ipynb

[NbConvertApp] Converting notebook /content/LoopsAssignment.ipynb to html


[NbConvertApp] Writing 601065 bytes to /content/LoopsAssignment.html

You might also like