Input an integer to determine whether it is 2 ^ n. If it is not, it will output the integer that is closest to 2 ^ n.

Source: Internet
Author: User
Input an integer to determine whether it is 2 ^ n. If yes, output

// Number. If not, the output is the integer closest to 2 ^ n.

Additional source code 1:

# Include <stdio. h> # include <stdlib. h> # include <math. h> int main () {int input; // input an integer inputint I, j on the keyboard; // I, j. The difference between input and 2 ^ N on the left and right will be stored in the future: int M, N; // Save the value on the left, 2 ^ nprintf nearest to inout ("enter an integer input ="); scanf ("% d", & input); If (input <= 0) {printf ("not 2 ^ N"); printf ("2 ^ n nearest to it is 1");} else {for (int K = 0; k <1000; k ++) // assume that N in 2 ^ n will not exceed 1000 {If (input = POW (2.0, k )) {printf ("% d is 2 ^ K", input); break; // jumps out for loop} else if (input> POW (2.0, k-1) & input <POW (2.0, k) {I = input-Pow (2.0, k-1); j = POW (2.0, k)-input; M = POW (2.0, k-1); n = POW (2.0, k); if (I <j) // The number closest to him is the number on his left {printf ("% d ", m); break;} else {printf ("% d", n); break ;}}} system ("pause"); // include <stdlib. h> return 0 ;}

========================================================== ========================================================== =

Additional source code 2:

// If the number is the power of Npower of 2, the number can be first modulo 2 to 0, then perform Division 2, and then modulo 2,
// Until the last number is 2. If you cannot do this, the integer is not the Npower of 2.

<Span style = "color: #000000;"> # include <stdio. h> # include <stdlib. h> int main () {int input; // input an integer input int COUNT = 1 on the keyboard; // The integer to be judged is the count power of 2 printf ("enter an integer input ="); scanf ("% d", & input); While (input) {If (2 = input) // divide by 2 in sequence. If there is only 2 left, it indicates the power of 2 {printf ("yes: it is % d power \ n ", count); break;} If (0 = input % 2) of 2) // if the remainder of the number and 2 is 0 {input = input/2; // take half of the remainder and continue the loop count ++; // idempotent addition 1} else {printf ("NO \ n"); // This loop class can determine the closest 2 ^ n to it and output the break ;}} return 0 ;}</span>

Input an integer to determine whether it is 2 ^ n. If it is not, it will output the integer that is closest to 2 ^ n.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.