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

Programs 1

Uploaded by

Murugesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Programs 1

Uploaded by

Murugesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1) Add Two Numbers in Python

Input: num1 = 5, num2 = 3


Output: 8
Input: num1 = 13, num2 = 6
Output: 19

2) Python3 program to add two numbers


num1 = 15
num2 = 12

# Adding two nos


sum = num1 + num2

# printing values
print("Sum of", num1, "and", num2 , "is", sum)

3) Add Two Numbers with User Input

number1 = input("First number: ")


number2 = input("\nSecond number: ")

# Adding two numbers


# User might also enter float numbers
sum = float(number1) + float(number2)

# Display the sum


# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1,
number2, sum))

4) Add Two Numbers in Python Using Function

#To define a function that take two integers


# and return the sum of those two numbers
def add(a,b):
return a+b

#initializing the variables


num1 = 10
num2 = 5
#function calling and store the result into sum_of_twonumbers
sum_of_twonumbers = add(num1,num2)

#To print the result


print("Sum of {0} and {1} is {2};" .format(num1,
num2, sum_of_twonumbers))

5) Add Two Numbers Using operator.add() Method

# Python3 program to add two numbers

num1 = 15
num2 = 12

# Adding two nos


import operator
su = operator.add(num1,num2)

# printing values
print("Sum of {0} and {1} is {2}" .format(num1,
num2, su))

6) Add Two Number Using Lambda Function

# Define a lambda function to add two numbers


add_numbers = lambda x, y: x + y

# Take input from the user


num1 = 1
num2 = 2

# Call the lambda function to add the two numbers


result = add_numbers(num1, num2)

# Print the result


print("The sum of", num1, "and", num2, "is", result)

7) Add Two Numbers Using Recursive Function

Define a recursive function to add two numbers


def add_numbers_recursive(x, y):
if y == 0:
return x
else:
return add_numbers_recursive(x + 1, y - 1)

# Take input from the user


num1 = 1
num2 = 2

# Call the recursive function to add the two numbers


result = add_numbers_recursive(num1, num2)

# Print the result


print("The sum of", num1, "and", num2, "is", result)

8) Python Program 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

if num == 1:
print(num, "is not a prime number")
elif 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")
9) Example 2: Using a for...else statement

num = 407

# To take input from the user


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

if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")

# if input number is less than


# or equal to 1, it is not prime
else:
print(num,"is not a prime number")

10) Python Program to Find the Factors of a Number

# Python Program to find the factors of a number

# This function computes the factor of the argument passed


def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)

num = 320

Output
print_factors(num)
The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

11) Python Program to Find Armstrong Number in an Interval

lwr = 100

uppr = 2000

for numb in range(lwr, uppe + 1):

# order of number

odr = len(str(numb))

# initialize sum

sum = 0

tem = numb

while tem > 0:

digit = tem % 10
sum += digit ** odr

tem //= 10

if numb == sum:

print("The Armstrong numbers are: ",numb)


The Armstrong numbers are:

153

370

371

407

1634

12) Fahrenheit to Celsius without Function

print("Enter Temperature in Fahrenheit: ")

fahren = float(input())

celi = (fahren-32)/1.8

print("\nEquivalent Temperature in Celsius: ", celi)

Output:

Enter Temperature in Fahrenheit:

98

Equivalent Temperature in Celsius


36.6666666666

13) Factorial Program in Python

import math

n = int (input (“Enter the number whose factorial you want to find: ”))

print (“The factorial of the number is: “)

print (math.factorial (n))

14) Write a program to calculate the factorial of a number in Python using FOR
loop.

Copy Code

n = int (input (“Enter a number: “))

factorial = 1

if n >= 1:

for i in range (1, n+1):

factorial = factorial *i

print (“Factorial of the given number is: “, factorial)

15) Write the Python program to calculate the factorial of a number using
recursion.

Copy Code
n = int (input (“Enter the number for which the factorial needs to be calculated: “)

def rec_fact (n):

if n == 1:

return n

elif n < 1:

return (“Wrong input”)

else:

return n*rec_fact (n-1)

print (rec_fact (n))

16) Python Program to Find the Sum of Natural Numbers

n = int (input (“Enter any natural number: “)

if n < 0:

print (“Wrong input. Please enter a positive number.”)

else:

sum = 0

while (n > 0):

sum +=n

n -=1

print (“The sum of the natural numbers is: “, sum)

Output:
Enter any natural number:

100

The sum of the natural numbers is 5500.

17) Alternative Method

n = int (input (“Enter any natural number: “)

if n < 0:

print (“Wrong input. Please enter a positive number.”)

else:

sum = n * (n+1) / 2

print (“The sum of the natural numbers is: “, sum)

18) Python Program to Find ASCII Value of Character

c = input (“Enter the number whose ASCII code needs to be found: “)

print (“The ASCII value of given character is”, ord (c ))

>>> chr (94)

‘a’

>>> chr (ord (‘S’) + 1)

‘T’
19) Python Program to Convert Decimal to Binary Using Recursion

def convertToBin(a):

if a > 1:

convertToBin(a//2)

print(a % 2,end = '')

# decimal number

d = 34
convertToBin(d)

print()

Output

100010

Let us consider 17 as the decimal number.

Step 1: When 17 is divided by 2, the remainder is one. Therefore, arr[0] = 1.

Step 2: Now we divide 17 by 2. New number is 17/2 = 8.

Step 3: When 8 is divided by 2, the remainder is zero. Therefore, arr[1] = 0.

Step 4: Divide 8 by 2. New number is 8/2 = 4.

Step 5: When 4 is divided by 2, the remainder is zero. Therefore, arr[2] = 0.

Step 6: Divide 4 by 2. New number is 4/2 = 2.

Step 7 : Remainder when 2 is divided by 2 is zero. Therefore, arr[3] = 0.

Step 8: Divide 2 by 2. New number is 2/2 = 1.

Step 9: When 1 is divided by 2, the remainder is 1. Therefore, arr[4] = 1.


Step 10: Divide 1 by 2. New number is 1/2 = 0.

Step 11: Since number becomes = 0. Now we consider the remainders in the reversed
order. Therefore the equivalent binary number is 10001.

What does 10101 mean in binary?

10101 is a binary number whose decimal representation is 21.

20) Python Program to Display Fibonacci Sequence Using Recursion

Source Code:

def FibRec(a):

if a <= 1:

return a

else:

return(FibRec(a-1) + FibRec(a-2))

aterms = int(input("Enter the terms? "))

if aterms <= 0:

print("Please enter a positive integer")

else:

print("Fibonacci sequence:")

for z in range(aterms):

print(FibRec(z))

Output:

Enter the terms?

5
01123

21) How do you write a Fibonacci Series for a loop in Python?

Source Code:

i=int(input("Enter the terms"))

first=0

second=1

if i<=0:

print("The requested series is

",first)

else:

print(first,second,end=" ")

for x in range(2,i):

n=first+second

print(n,end=" ")

first=second

second=n

Output: Enter the terms

1
2

22) Python Program to Count the Number of Each Vowel

def Check_Vow(string, vowels):

# The term "casefold" has been used to refer to a method of ignoring cases.

string = string.casefold()

count = {}.fromkeys(vowels, 0)

# To count the vowels

for character in string:

if character in count:

count[character] += 1

return count

# Driver Code

vowels = 'aeiou'

string = "Hi, I love eating ice cream and junk food"

print (Check_Vow(string, vowels))

Output:

{'a': 2, 'e':4 , 'i':2 , 'o':3 , 'u':1}

23) Is vowel function in Python?

l = input("Enter the character: ")

if l.lower() in ('a', 'e', 'i', 'o', 'u'):


print("Vowel")

elif l.upper() in ('A', 'E', 'I', 'O', 'U'):

print("Vowel")

else:

print("Consonant")

Output:

Enter the character

Vowel

ch = input("Enter the character : ")

if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A' or ch == 'E' or ch


== 'I' or ch == 'O' or ch == 'U'):

print("Vowel")

else:

print("Consonant")

Output:

Enter the character:

Consonant

24) Python Multiplication Table using the for Loop

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


# using the for loop to generate the multiplication tables

print("Table of: ")

for a in range(1,11):

print(num,'x',a,'=',num*a)

Output

Enter the number : 7

Multiplication Table of :

7x1=7

7 x 2 = 14

You might also like