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

Python Programs

The document contains Python programs for various tasks including adding two numbers, swapping variables, finding the largest among three numbers, checking if a number is prime, calculating factorials, printing Fibonacci sequences, and checking Armstrong numbers. Each program includes user input and outputs the result accordingly. Example outputs are provided for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Programs

The document contains Python programs for various tasks including adding two numbers, swapping variables, finding the largest among three numbers, checking if a number is prime, calculating factorials, printing Fibonacci sequences, and checking Armstrong numbers. Each program includes user input and outputs the result accordingly. Example outputs are provided for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Program to Add Two Numbers

num1 = input('Enter first number: ')


num2 = input('Enter second number: ')
sum = num1 + num2
print('The sum of two numbers’, sum)

Output
Enter first number: 5
Enter second number: 6
The sum of two numbers 11

2.Program to Swap Two Variables

# Python program to swap two variables

x=5
y = 10

temp = x
x=y
y = temp

print('The value of x after swapping:' x)


print('The value of y after swapping:' y)

Output
The value of x after swapping: 10
The value of y after swapping: 5

3. Program to Find the Largest Among Three Numbers

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

Output
The largest number is 14.0

4. Program to Check Prime Number

# Program to check if a number is prime or not

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


flag = False
if num == 1:
print(num, "is not a prime number")
elif num > 1:

for i in range(2, num):


if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Output

29 is a prime number

5. Find the Factorial of a Number

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


factorial = 1

if num < 0:
print("factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output

The factorial of 7 is 5040

6. Print the Fibonacci sequence

nterms = int(input("How many terms? "))


# first two terms
n1, n2 = 0, 1
count = 0

if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2

n1 = n2
n2 = nth
count += 1

Output
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8

7. Check Armstrong number (for 3 digits)

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


sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output 1
Enter a number: 663
663 is not an Armstrong number
Output 2
Enter a number: 407
407 is an Armstrong number

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy