Computer Science, asked by gururajsubramanian05, 9 months ago

Program to accept a number convert to positive to negative and vice-versa in python

Answers

Answered by SP85
2

abs() is a built-in function in Python programming language which gives a positive value of any number in return. It means it converts any negative number into a positive number and positive numbers remain unchanged.

#Python 3 program to convert n into

# negative base form

# Method to convert n to base negBase

def toNegativeBase(n, negBase):

# If n is zero then in any base it

# will be 0 only

if (n == 0):

return "0"

converted = "01"

while (n != 0):

# Get remainder by negative base,

# it can be negative also

remainder = n % (negBase)

n = int(n/negBase)

# if remainder is negative, add

# abs(base) to it and add 1 to n

if (remainder < 0):

remainder += ((-1) * negBase)

n += 1

# convert remainder to string add

# into the result

converted = str(remainder) + converted

return converted

# Driver Code

if __name__ == '__main__':

n = 13

negBase = -2

print(toNegativeBase(n, negBase))

# This code is contributed by

#SP85 (this is by me)

From c++ program vice versa - python

Similar questions