Feed? What do you mean by that
#include <math.h> inline unsigned int get_num_digits(const unsigned int n) { return ((unsigned int) log10(n) + 1); }
A signed int can take a value between -32768 to 32767 and an unsigned int can take a value 0 to 65535.
/* note that neither of these functions have been tested, so there may be typos and the like *//* if you're looking to return a real value: */unsigned int complement(unsigned int value){unsigned int returnvalue = 0;while(value){returnvalue = 1;}return returnvalue;}/* if you're looking for a string representing the binary number: */char *complement(unsigned int value){int numchars = 8 * sizeof(unsigned int);int n;char *returnvalue = malloc((numchars + 1) * sizeof(char));for(n = 0; n < numchars; n++){if(value & (1
The value range. Example for 16-bit integers: signed: -32768..32767 unsigned: 0..65535
There are far more than 4 integral types in C++. As of C++11, there were 27 integral types: bool char signed char unsigned char wchar_t char16_t char32_t short signed short unsigned short short int signed short int unsigned short int int signed int unsigned int long signed long unsigned long long int signed long int unsigned long int long long signed long long unsigned long long long long int signed long long int unsigned long long int
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
#include<iostream> #include<vector> unsigned count_digits (unsigned num, const unsigned base=10) { unsigned count=1; while (num/=base) ++count; return count; } class number { std::vector<unsigned> value; unsigned base; public: number (const unsigned _value, const unsigned _base=10): value {}, base {_base} { *this = _value; } number& operator= (const unsigned _value); operator unsigned () const; bool is_narcissistic () const; }; number& number::operator= (unsigned _value) { unsigned count = count_digits (_value, base); value.resize (count); while (count) { value[value.size()-count--] = _value%base; _value/=base; } return *this; } number::operator unsigned () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += value[index]*static_cast<unsigned>(std::pow (base, index)); return num; } bool number::is_narcissistic () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += static_cast<unsigned>(std::pow (value[index], value.size())); return num == static_cast<unsigned> (*this); } unsigned main() { const unsigned min=1; const unsigned max=100; std::cout << "Narcissistic numbers in the range " << min << " through " << max << ":\n\t"; for (unsigned n=min; n<=max; ++n) if (number(n).is_narcissistic()) std::cout << n << ' '; std::cout << '\n' << std::endl; }
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a<=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i<=n;i++) f*=i ; return f; }
#include <iostream> #include <time.h> unsigned int reverse( unsigned int num ) { const unsigned int base=10; unsigned int rev=0; while( num ) {rev*=base;rev+=num%base;num=num/base;} return( rev ); } int main () { srand(( unsigned ) time( NULL )); // Print 10 random numbers in reverse. for( int i=0; i<10; ++i ) {unsigned int r = ( unsigned int ) rand();std::cout << r << " = " << reverse(r) << std::endl;} return(0); }
== == using recursions: unsigned int Factorial( unsigned int x) { if(x>0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x>0) { u32fact = u32fact *x; x--; } } }