CEIL AND FLOOR FUNCTION IN CPP:
CEIL:
Ceil(x) returns the largest integer that is greater than or equal to x
ex- ceil(2.5) will return 3
ex-ceil(-2.3) will return -2
ex- ceil(8) will return 8
FLOOR:
floor(x) returns the largest number that is smaller than or equal to x.
ex- floor(2.5) will return 2
ex-floor(-2.3) will return -3
ex-floor(8) will return 8
PROGRAM TO CHECK WHETHER A NUMBER IS POWERS OF 2 OR NOT:
#include<bits/stdc++.h>
using namespace std;
bool powertwo(int n)
{
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n))); //ceil and floor of whole numbers is equal
}
int main()
{ int t,i;
cin>>t;
for(i=0;i<t;i++)
{
int n;
cin>>n;
powertwo(n)?cout<<"YES"<<endl:cout<<"NO"<<endl;
}
return 0;
}
OUTPUT:
COUT AND PRINT THE NUMBERS UPTO n WHICH ARE POWERS OF TWO:
#include<bits/stdc++.h>
using namespace std;
void powertwo(int n)
{
int c=0,i;
for(int i=0;i<=n;i++)
{
if ((ceil(log2(i)) == floor(log2(i)))&&i!=0) //ceil and floor of whole numbers is equal
{
c++;
cout<<i<<" ";
}
}
cout<<endl;
cout<<c<<endl;
}
int main()
{ int t,i;
cin>>t;
for(i=0;i<t;i++)
{
int n;
cin>>n;
powertwo(n);
}
return 0;
}
OUTPUT:
0 Comments