#To find the quotient and remainder
dividend=101
divisor=11
quotient=dividend/divisor #To find quotient
remainder=dividend%divisor #To find remainder
dividend=(divisor*quotient)+remainder
print "The quotient when",dividend,"is divided by",divisor,"is",quotient,"=",dividend,"div",divisor,"and the remainder is",remainder,"=",dividend,"mod",divisor
#To find the quotient and remainder
dividend=-11
divisor=3
quotient=dividend/divisor
remainder=dividend%divisor
dividend=(divisor*quotient)+remainder
print "The quotient when",dividend,"is divided by",divisor,"is",quotient,"=",dividend,"div",divisor,"and the remainder is",remainder,"=",dividend,"mod",divisor
#To convert binary to decimal equivalent
binary_num= raw_input('enter a number: ')
decimal = 0
for digit in binary_num:
decimal = decimal*2 + int(digit)
print decimal
#To convert decimal to hexadecimal
dec= raw_input('enter a number: ')
print "The conversion of",dec,"to hexadeimal is",int(dec,16)
#To compute decimal to octal
numbers= []
dec=input("Enter a number");
num=dec
while dec!=0:
rem=dec%8
dec=dec/8
numbers.append(rem)
print "The decimal number",num,"is converted to its octal equivalent : ",
for i in reversed(numbers):
print i,
#To convert Decimal to hexadecimal
num=[]
def ChangeHex(n): #function to convert
if (n < 0):
num.append("")
elif (n<=1):
num.append(n)
else: #for numbers greater than 9
x =(n%16)
if (x < 10):
num.append(x)
if (x == 10):
num.append("A")
if (x == 11):
num.append("B")
if (x == 12):
num.append("C")
if (x == 13):
num.append("D")
if (x == 14):
num.append("E")
if (x == 15):
num.append("F")
ChangeHex( n / 16 )
dec_num=input("Enter the decimal number which is to be converted to hexadecimal");
ChangeHex(dec_num)
print "The hexadecimal equivalent of decimal",dec_num,"is",
for i in reversed(num):
print i,
#Compute Decimal to Binary
array=[]
def conv(n):
if n==0:
print ''
else:
array.append(str(n%2)) #to compute remainder and append it to the result
return conv(n/2)
dec_num=input("Enter a decimal number")
conv(dec_num)
print "The binary equivalent of decimal",dec_num,"is",
for i in reversed(array):
print i,
#To compute the binary addition
def binAdd(bin1, bin2): #function to add two binary numbers
if not bin1 or not bin2:#checks if both the numbers are binary
return ''
maxlen = max(len(bin1), len(bin2))
bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width
bin2 = bin2.zfill(maxlen)
result = ''
carry = 0
i = maxlen - 1
while(i >= 0):
s = int(bin1[i]) + int(bin2[i])#adding bit by bit
if s == 2: #1+1
if carry == 0:
carry = 1
result = "%s%s" % (result, '0')
else:
result = "%s%s" % (result, '1')
elif s == 1: # 1+0
if carry == 1:
result = "%s%s" % (result, '0')
else:
result = "%s%s" % (result, '1')
else: # 0+0
if carry == 1:
result = "%s%s" % (result, '1')
carry = 0
else:
result = "%s%s" % (result, '0')
i = i - 1;
if carry>0:
result = "%s%s" % (result, '1')
return result[::-1]
bin1 = raw_input('enter the first number: ')
bin2 = raw_input('enter the second number: ')
print "The sum of binary numbers",bin1,"and",bin2,"is",binAdd(bin1,bin2)
#to find the prime factors
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i: #modulp division to check of the number is prime or not
i += 1
else:
n //= i
factors.append(i) #append those numbers which readily divides the given number
if n > 1:
factors.append(n)
return factors
number=input("Enter the number for which the prime factors have to be found");
a=prime_factors(number)
print a
#To say if a number is prime or not
globals() ['count']=0
n=input("Enter the number");
for i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)
if n%i==0:
count=count+1
num=i
if count==0:
print n,"is prime"
else:
print n,"is not prime because its divisible by",num
#to find the prime factors
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i: #modulp division to check of the number is prime or not
i += 1
else:
n //= i
factors.append(i) #append those numbers which readily divides the given number
if n > 1:
factors.append(n)
return factors
number=input("Enter the number for which the prime factors have to be found");
a=prime_factors(number)
print a
#To compute GCD
def gcd(a,b):#fuction computes gcd
if b > a:
return gcd(b,a)
r = a%b
if r == 0:
return b
return gcd(r,b)
n1=input("Enter the first number");
n2=input("Enter the second number");
print "GCD(",n1,",",n2,") is",gcd(n1,n2)
#To compute GCD
def gcd(a,b):#fuction computes gcd
if b > a:
return gcd(b,a)
r = a%b
if r == 0:
return b
return gcd(r,b)
n1=input("Enter the first number");
n2=input("Enter the second number");
print "GCD(",n1,",",n2,") is",gcd(n1,n2)
#to find gcd using euclidean algorithm
def gcd(a,b):#euclidean algithm definition
x=a
y=b
while y!=0:
r=x%y
x=y
y=r
print "gcd(",a,",",b,")is",x
num1=input("Enter the first number");
num2=input("Enter the second number");
gcd(num1,num2)
#to find gcd using euclidean algorithm
def gcd(a,b):#euclidean algithm definition
x=a
y=b
while y!=0:
r=x%y
x=y
y=r
print "gcd(",a,",",b,")is",x
num1=input("Enter the first number");
num2=input("Enter the second number");
gcd(num1,num2)