Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Reversed Binary Numbers (Kattis) - C Language I have problems when input is 1000000000 (upper limit); my code is posted after the problem. #include #include

Reversed Binary Numbers (Kattis) - C Language

I have problems when input is 1000000000 (upper limit); my code is posted after the problem.

image text in transcribed

#include

#include

int main(void) {

int remainder, i = 0;

long long decimal, binary = 0, reversed = 0, multiplier = 1;

scanf("%lld", &decimal); // decimal can reach up to 1000000000, which is 30-digit long in binary

while(decimal != 0) {

remainder = decimal % 2;

decimal /= 2;

binary += remainder * multiplier;

multiplier *= 10;

}

while(binary != 0) {

remainder = binary % 10;

reversed = (reversed * 10) + remainder;

binary /= 10;

}

while(reversed != 0) {

remainder = reversed % 10;

decimal += (remainder * pow(2, i));

reversed /= 10;

i++;

}

printf("%lld ", decimal);

return 0;

}

Reversed Binary Number:s Yi has moved to Sweden and now goes to school here. The first years of schooling she got in China, and the curricula do not match completely in the two countries. Yi likes mathematics, but now... The teacher explains the algorithm for subtraction on the board, and Yi is bored. Maybe it is possible to perform the same calculations on the numbers corresponding to the reversed binary representations of the numbers on the board? Yi dreams away and starts constructing a program that reverses the binary representation, in her mind. As soon as the lecture ends, she will go home and write it on her computer Task Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11. Input The input contains a single line with an integer N, 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions