Exchange Values, Factorial
Exchange Values, Factorial
Contents
Fundamental Algorithms:
Introduction
Exchange the values
Counting
Summation
Factorial Computation
Fibonacci Sequence
Reverse
Base Conversion
Character to Number Conversion
Python Control Flow,
Functions :
Conditionals: Boolean values and
operators,
conditional (if, if- else, if-elif-else)
Iteration statements (state, while, for
break, ontinue, pass)
What is the Algorithm for
exchanging values of two variables?
Algorithm:
Step 1: Start
Step 2: Read the value of Number1, Number2
Step 3: Number1= Number1 + Number2
Step 4: Number2= Number1 - Number2
Step 5: Number1= Number1 - Number2
Step 6: PRINT the value of Number1 and Number2
Step 7: Stop
Q. Write a program for exchanging
values of two variables in python
Source Code: Without Using
Temporary Variable
x =5
y = 10
x, y = y, x
print(“value of x: =", x)
print(“value of y: =", y)
Output
2. Addition and Subtraction code to exchange the value
of variable
X=10
Y=50
x =x+y
y =x-y
x =x–y
print("Value of x:", x)
print("Value of y:", y)
Output
3. Multiplication and Division
code to exchange the value of
variable
X=10
Y=50
x = x * y
y = x / y
x = x / y
print("Value of x : ", x)
print("Value of y : ", y)
Output
4.XOR swap the value of
variables
This algorithm works for integers only.
X=2
Y=3
x = x ^ y # 2 ^3 =1
y = x ^ y # 2^ 3 = 2
x=x^y# 3
print("Value of x : ", x)
print("Value of y : ", y)
Output
Write an algorithm to exchange of
two numbers using third variable
Step 1: Start
Step 2: READ num1, num2
Step 3: temp = num1
Step 4: num1 = num2
Step 5: num2 = temp
Step 6: PRINT num1, num2
Step 7: Stop
Write a program to exchange of two
numbers using third variable in
python
X= int(input( “ enter the first no”))
Y=int(input(“enter the second no”))
Temp=x
X=y
y= temp
Print(“the value of x:”,x)
Print(“the value of y:”,y)
Output
Write an algorithm to count the
digits in a given number.
Step 1: Start
Step 2: READ number
Step3: Initialize the count 0
Step 4: repeat step 5 untill number become 0 other wise goto step 7
Step 5: divide number with 10
Step 6: count<-count+1
Step 7: write count
Step 8: Stop
Q. write a program to count the
digits in a given number in python
n=int(input("Enter number:")) #n =12546 five
count=0
while(n>0): # 12546>0
count=count+1 #cont =1 2 3 4 5
n=n//10 # 12546//10 = 1254//10=125 , 125//10= 12 12/10
1//10 0
print("The number of digits in the number are:",count)
Output
write a program to count the digits in a given
number in python by using inbuilt function
num= 123456
Print(len(str(num)))
Output
Write a program in python to find
the sum of digits of a number.
1234
rem = 123%10 # 4
Sum = sum+rem # 0+4= 4
Return= n//10 # 123
1234
Sum()
Factorial of a number is
denoted by n!, is the product
of all positive integers less
than or equal to n:
n! = n*(n-1)*(n-2)*…..3*2*1
Example
10 Factorial:-
So what is 10!? Multiply 10
with all the positive integers
which are less than 10.
10!
=10*9*8*7*6*5*4*3*2*1=3628
800
# Python program to find the
factorial of a number provided by
the
user.
num = 7 # To take input from the user #
num = int(input("Enter a number: "))
factorial = 1 # check if the number is negative, positive or zero
if num < 0:
print("Sorry, 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)
Fibonacci sequence
We will use a while loop for printing the sequence of the Fibonacci
sequence.
Step 1: Input the number of values we want to generate the
Fibonacci sequence
Step 2: Initialize the count = 0, n_1 = 0 and n_2 = 1.
Step 3: If the n_terms <= 0
Step 4: print "error" as it is not a valid number for series
Step 5: if n_terms = 1, it will print n_1 value.
Step 6: while count < n_terms
Step 7: print (n_1)
Step 8: nth = n_1 + n_2
n_terms = int(input ("How many terms the user wants to print? "))
# First two terms
n_1 = 0
n_2 = 1
count = 0
# Now, we will check if the number of terms is valid or not
if n_terms <= 0:
print ("Please enter a positive integer, the given number is not valid")
# if there is only one term, it will return n_1
elif n_terms == 1:
print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")
print(n_1)
# Then we will generate Fibonacci sequence of number
else:
print ("The fibonacci sequence of the numbers is:")
while count < n_terms:
print(n_1)
nth = n_1 + n_2
# At last, we will update values
n_1 = n_2
n_2 = nth
Fibonacci sequence
Method: 1 - By using a Far loop
Digit 0 1 0 1 1
def decimalToBinary(num):
if num > 0:
decimalToBinary(num // 2)
print(num % 2, end = “ ”)
number = int(input("Enter the decimal number: "))
#main()
function decimalToBinary(number)
Using bin() function to form decimal
to binary
number = int(input("Enter the decimal number: "))
print(bin(number))
Output
Enter the decimal number: 56
0b111000
Write a program to print binary to
decimal number?
# Taking binary input
binary = input("Enter a binary number:")
def BinaryToDecimal(binary):
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print("The decimal value is:", decimal)
# Calling the function
BinaryToDecimal(binary)
Decimal to octa conversion
Decimal to octa conversion
def dectoOct(decimal):
if(decimal > 0):
dectoOct((int)(decimal/8))
print(decimal%8, end='')
while (num):
# Extracting last digit
last_digit = num % 10
num = int(num / 10)
return decimal
# take inputs
num = int(input('Enter an octal number: '))
while(decimal>0):
remainder = decimal%16
hexadecimal = conversion_table[remainder]+ hexadecimal
decimal = decimal//16
print("Hexadecimal: ",hexadecimal)
Output
Entera number: 67
Hexadecimal: 43
Hexa decimal to decimal conversion
Write a program to convert
hexadecimal to decimal
# hexadecimal string
hex = '0F'
# conversion
dec = int(hex, 16)
Value in hexadecimal: 0F
Value in decimal: 15
Python Program to Convert Decimal to
Binary, Octal and Hexadecimal
dec = 344
text
= "a"
number = ord(text)
print(number)
Output
97
Number to character conversion
number = 97
ascii = chr(number)
print(ascii)
Output
a