Can someone help me translate this c program to python? #include <stdio.h> #incl
ID: 3722018 • Letter: C
Question
Can someone help me translate this c program to python?
#include <stdio.h>
#include <math.h>
long long convertDecimalToBinary(int n);
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n, convertDecimalToBinary(n));
return 0;
}
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %d ", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
Explanation / Answer
Hi.. I have written python program for above c program.
abc.py
def convertToBinary(n):
"""Function to print binary number
for the input decimal using recursion"""
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')
# decimal number
dec = input("Enter Decimal number ")
convertToBinary(dec)
Output:
Enter Decimal number 34
100010
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.