Home > Software design >  How to Print remainders of a number(userInput) when divide by 2 at each iteration
How to Print remainders of a number(userInput) when divide by 2 at each iteration

Time:09-26

I am trying to print remainder of a number(userInput) when divide by 2 at each iteration in Python. I have similar code in C look like this:

#include <stdio.h>
#include <stdlib.h>

main()
{
    int i;
    printf("Enter a number: ");
    scanf("%d",&i);

    for(i=i; i <= 1000; i = i/2){
        if (i==0){
            break;
        }
        printf("%d\n",i%2);

    }
    system("pause");
}

CodePudding user response:

How about something like this:

user_number = int(input('Please enter a number\n'))

while (user_number > 1):
    print('{}%2 = {}'.format(user_number, user_number % 2))
    user_number = user_number / 2

Please correct me if I misunderstood the purpose of the code.

  • Related