[ create a new paste ] login | about

Link: http://codepad.org/Nhw1s60f    [ raw code | output | fork | 3 comments ]

C, pasted on Aug 31:
#include <stdio.h>
#include <conio.h>
 
int  check(int n);
int main()
{
    int i;
    int max = 10000;
 
    printf("cac so co dang 2^k trong khoang [0, %d]:\n", max);
    for(i = 0; i < 10000; i++)
         check(i) ?   printf("%5d", i) : printf("");      
    getch();
    return 0;
}
/* Số có dạng 2^k
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16

VD: kiểm tra số 81
16 % 2 = 8 dư 0
8 % 2 = 4 dư 0
4 % 2 = 2 dư 0
2 % 2 = 1 dư 0 => hết

VD: kiểm tra số 12
12 % 2 = 6 dư 0
6 % 2 = 3 dư 0 
3 % 2 = 1 dư 1 => sai !
*/
int check(int n)
{
	int flag = 1;
    n = n < 0 ? -n : n;
    if(n < 2)
        return 1;
    /*while(n > 1)
    {
        if(n % 2)
            return 0;
        n /= 2;
    }
    return 1;*/
	while(n > 1)
	{
		int du = n % 2;
		if(du != 0)  // nếu dư mà khác 0. lập tức báo sai
			flag = 0;
		n /= 2;
	}
	if(flag==1)
      return 1;
    else
      return 0;
}


Output:
1
Line 18: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by dangtiendat86@gmail.com on Feb 13
#include<iostream>
using namespace std;
main(){
int n;
cout<<"n="; cin>>n;
while (n>2){
if ( n % 2!=0){
cout<<"false"<<endl;
break;
}
else n/=2;
}
if (n==2) cout<<"true"<<endl;
}
reply
posted by yonnon on Jul 12
#include<bits/stdc++.h>

using namespace std;
int main(){
int n;
cout <<"Enter your sentence: ";
cin >> n;
if(n==0 || n==1 || n==2){
cout<<"True";
return 0;
}
int number=1;
int temp=0;
for(int i=0; i<=n/2;i++){
//number*=2;
if(pow(2,i)==n){
temp=i;
}

}
if(temp != 0){
cout<<"True";
return 0;

}
else if(temp==0){
cout<<"False";
return 0;
}
cin.ignore();
return 0;

}
reply
posted by cuong.luucb8921 on Aug 28
#include<stdio.h>
#include<stdbool.h>
int main(){
int n;
bool check = false;
do
{
printf("Nhap vap n:\n");
scanf("%d",&n);
if(n < 1)
{
printf("n phai lon hon bang 1\n");
}
}while(n < 1);

int i;

for(i=0;i<=n;i++)
{
if(n==pow(2,i))
{
check = true;
}
}

if(check)
{
printf("Yes");
}
else
{
printf("No");
}

return 0;
}
reply