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

Practical 6

Python

Uploaded by

rutujapawar.1408
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)
7 views3 pages

Practical 6

Python

Uploaded by

rutujapawar.1408
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

8/30/24, 10:32 AM Practical 6

1.Write a program to print first n natural numbers


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

Enter a number: 10

 In [2]: for i in range(1, n + 1):


print(i)

1
2
3
4
5
6
7
8
9
10

2. Write a program to find the factorial of given


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

Enter a number: 3

In [10]: factorial = 1
i=1
while i<=n:
factorial = factorial*i
i=i+1
print(f"Factorial of {n} is {factorial}")

Factorial of 3 is 6

3.Write a program to print first n Fibonacci


numbers.
In [11]: n = int(input("Enter a number: "))

Enter a number: 6

In [12]: a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

0 1 1 2 3 5

localhost:8888/notebooks/Desktop/FYBSc Python/Notes/Practical 6.ipynb 1/3


8/30/24, 10:32 AM Practical 6

4.Write a program to display multiplication table of


given number.
In [13]: n = int(input("Enter a number to display its multiplication table: "))

Enter a number to display its multiplication table: 12

In [14]: print(f"Multiplication Table of {n}:")


for i in range(1, 11):
print(f"{n} x {i} = {n * i}")

Multiplication Table of 12:


12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

5.Write a program to find the numbers of integers


between 1 to 1000, which are divisible by 7.
In [15]: count = 0

for i in range(1, 1001):
if i % 7 == 0:
count += 1

print(f"The number of integers between 1 and 1000 that are divisible by 7 is

The number of integers between 1 and 1000 that are divisible by 7 is: 142

6.Write a program to check whether number is


palindrome or not.
In [16]: n = int(input("Enter a number: "))

Enter a number: 1245

In [24]: # Convert the number to a string to check for palindrome


original_number = str(n)
reversed_number = original_number[::-1]

localhost:8888/notebooks/Desktop/FYBSc Python/Notes/Practical 6.ipynb 2/3


8/30/24, 10:32 AM Practical 6

In [25]: # Check if the original number is the same as the reversed number
if original_number == reversed_number:
print(f"{n} is a palindrome.")
else:
print(f"{n} is not a palindrome.")

1245 is not a palindrome.

7.Write a program to check whether given number is


Armstrong or not

Hint: An Armstrong number (or narcissistic number) is a number


that is equal to the sum of its own digits each raised to the power
of the number of digits.

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



# Convert the number to a string to get the number of digits
num_str = str(n)
num_digits = len(num_str)

# Calculate the sum of each digit raised to the power of num_digits
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)

Enter a number: 154

In [27]: # Check if the sum_of_powers is equal to the original number


if sum_of_powers == n:
print(f"{n} is an Armstrong number.")
else:
print(f"{n} is not an Armstrong number.")

154 is not an Armstrong number.

In [ ]: ​

localhost:8888/notebooks/Desktop/FYBSc Python/Notes/Practical 6.ipynb 3/3

You might also like