Friday, February 26, 2021

Set-1

Add two numbers 

# Python program add two variables
num1 = 10
num2 = 20
# To take inputs from the user
#num1 = input("First number: ")
#num2 = input("\nSecond number: ")
sum = num1 + num2
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))

Find odd and even Number

print("Enter the Number: ")
num = int(input())
if num%2==0:
print("\nIt is an Even Number")
else:
print("\nIt is an Odd Number")

Swap two number provided by user input

# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
The value of x after swapping: 10
The value of y after swapping: 5

Swap two number provided by user input

# Python program to swap two variables
x = 5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)

Reverse of Integer

#Python Program to reverse of number
number = int(input("Enter a positive integer: "))
rev = 0
while(number!=0):
digit = number%10
rev = (rev*10)+digit
number = number//10
print(rev)

To check Prime Number

# Program to check if a number is prime or not

num = 29

# To take input from the user
#num = int(input("Enter a number: "))

# define a flag variable
flag = False

# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Check 3 digit Armstrong number

# Python program to check if the number is an Armstrong number or not

# take input from the user
num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

No comments:

Post a Comment