Computer Practical File 1
Computer Practical File 1
OUTPUT
Enter a number: 5
The factorial of 5 is 120
OUTPUT
Enter the base of the triangle: 5
Enter the height of the triangle: 8
The area of the triangle is 20.00
OUTPUT:
1
2
3
4
5
**
***
****
*****
8) While Loop to Find the Sum of Natural
Numbers
n = 5 sum
=0
i = 1 while i
<= n:
sum += i
i += 1
print("The sum of the first {n} natural numbers is
{sum}")
OUTPUT:
The sum of the first 5 natural numbers is 15
OUTPUT:
The number of vowels in the string is 3
9) Printing a Pattern (Inverted Right
Triangle) n=5
for i in range(n, 0, -1):
for j in range(i):
print("*", end=" ")
print()
OUTPUT:
** * * *
** * *
** *
**
*
10)Reversing a String
string = "Python"
reversed_string = ""
for char in string:
reversed_string = char + reversed_string
print("The reversed string is: {reversed_string}")
Output:
The reversed string is: nohtyP
11) Sum of Natural Numbers
upto a given number
num = int(input("Enter a positive integer: "))
sum = 0
if num < 1:
print("Please enter a positive integer.")
else:
for i in range(1, num + 1):
sum += i
print("The sum of natural numbers from 1 to
{num} is
{sum}")
OUTPUT
Enter a positive integer: 5
The sum of natural numbers from 1 to 5 is 15