answersLogoWhite

0


Best Answer

It isn't actually possible to generate factorials for negative and/or non-integer numbers.

However, a function for non-negative integers is:

unsigned int factorial(unsigned int n)

{

if(n==0)

{

return 1;

}

return n * factorial(n-1);

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i;

clrscr();

printf("Enter a Number:");

scanf("%d",&n);

printf("\n\nFactors of %d is:\n",n);

for(i=1;i<=n;i++)

{

if(n%i==0)

printf("%d, ",i);

}

getche();

}

this program will find all the factors a given number

any assistance

mail at:- devilllcreature@Yahoo.com

thank you

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

std::vector<unsigned> get_factors (unsigned num)

{

std::vector<unsigned> factors;

unsigned max = num/2 + 1;

for (unsigned factor=1; factor<max; ++factor)

if (num%factor==0)

factors.push_back (factor);

factors.push_back (num);

return factors;

}

void print_array (std::vector<unsigned>& arr)

{

unsigned index=0;

while (index<arr.size())

{

std::cout << arr[index++];

if (index==arr.size()-1)

std::cout << " and ";

else if (index<arr.size())

std::cout << ", ";

}

}

int main()

{

for (unsigned num=1; num<65; ++num)

{

std::cout << num << " has the following factors:\t";

std::vector<unsigned> factors (get_factors(num));

print_array (factors);

std::cout<<std::endl;

}

std::cout << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include <iostream>

using namespace std;

int fact(int x);

int main()

{

int x, total;

cout << "Enter a number: ";

cin >> x;

total = fact(x);

cout << "The factorial of " << x << " is: " << total << endl;

char wait;

cin >> wait;

return 0;

}

int fact(int x)

{

if(x == 1)

{

return 1;

}

return fact(x - 1) * x;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The following example returns a list of all factors for a given natural number, n. Zero is not a natural number thus an empty list is returned in this case.

list<unsigned int> nfactor(unsigned int n)

{

// list of factors to return

list<unsigned int> f;

// n must be >=1 (natural numbers only)

if( n )

{

// 1 is always a factor

f.push_back(1);

// natural number 1 is a special case (only 1 factor)

if( n>1 )

{

// Calculate remaining factors from 2 to n/2

for(unsigned int c=2; c<=n/2; ++c)

{

if( n%c==0 )

f.push_back(c);

}

// n is always a factor

f.push_back(n);

}

}

return( f );

}

The following example returns a list of factors for any given integer, i. The integer zero returns an empty list because the integer zero has infinite factors (every integer is a factor of zero).

list<int> ifactor(int i)

{

// list of factors to return

list<int> f;

// i must be non-zero

if( i )

{

// 1 and -1 are always factors

f.push_back(1);

f.push_front(-1);

// determine absolute value of i

int a=abs(i);

// integer -1 and 1 are special cases (only 2 factors)

if( a>1 )

{

// Calculate remaining factors from 2 to a/2

for( int c=2; c<=a/2; ++c)

{

if( i%c==0 )

{

f.push_back(c);

f.push_front(c*(-1));

}

}

// i and -i are always factors

f.push_back(a);

f.push_front(a*(-1));

}

}

return( f );

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A factor is any positive integer greater than 1 that wholly divides another integer. E.g., 2, 3, 4, 6 and 12 are all factors of 12. We can use the following function to prove this:

std::vector<unsigned> factors (unsigned n) {

std::vector<unsigned> r {};

for (unsigned u {2}; u<=n; ++u) if ((n%u)==0) r.push_back (u);

return r;

}

// Example usage:

std::vector<unsigned> x {factors (12)};

Here, the returned vector will hold the values 2, 3, 4, 6 and 12.

However, when working with factors we're usually not interested in all of them, we're only interested in the prime factors. In this case, we're only interested in 2 and 3. This is because 2 is a prime factor of 4, 6 and 12 while 3 is a prime factor of 6 and 12. In other words we can compute all the factors from the prime factors alone.

The largest prime factor of any given value can be no larger than the square root of the value. So instead of testing every value as we did above we can simply stop when we exceed the square root. The following function will do that:

std::vector<unsigned> prime_factors (unsigned n) {

std::vector<unsigned> r {};

unsigned max {std::sqrt ((double) n)};

for (unsigned u {2}; u<=max; ++u) if ((n%u)==0) r.push_back (u);

return r;

}

Now our vector only contains 2 and 3. However, for a larger value n, the vector will still include some non-prime factors. For instance, prime_factors(36) will include the values 2, 3, 4 and 6, but 4 and 6 are not prime. The easy way to exclude these non-primes is simply to test if they are prime:

bool is_prime (unsigned n) {

if (n<2) return false; // note that 0 and 1 are neither prime nor composite

if ((n%2)==0) return n==2; // 2 is the only even prime

unsigned max {std::sqrt ((double) n)};

for (unsigned f=3; f<=max; f+=2) if ((n%f)==0) return false; // test odd factors only

return true; // if we get this far, n has no factors (other than itself) so it must be prime

}

There are more efficient algorithms to determine if a value is prime or not, but for trivial programs this will suffice. With this function in place we can now modify our prime_factors function as follows:

std::vector<unsigned> prime_factors (unsigned n) {

std::vector<unsigned> r {};

unsigned max {std::sqrt ((double) n)};

for (unsigned u {2}; u<=max; ++u) if ((n%u)==0 && is_prime (u)) r.push_back (u);

return r;

}

Now we get the expected result for any value.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The biggest problem with factorials is that primitive data types are insufficient to store the results. For instance, factorial 21 (denoted as 21!) is 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 which is 51,090,942,171,709,440,000. But a 64-bit unsigned integer is only capable of storing numbers up to 18,446,744,073,709,551,615. Therefore any program you write using primtives will be limited to factorials in the range 1 to 20.

To resolve this problem we need a more dynamic user-defined data type, one that is capable of utilising as many bytes as necessary to store a given number. The following program makes use of an user-defined bigint data type that is capable of handling extremely large integers. The implementation is incomplete but provides enough functionality to calculate factorials of any size, the only limit being available memory. The implementation has much room for improvement, particularly with regards the multiplication operator which uses a naive addition operation to gain the product. A more efficient method would utilise a more human-like multiplication method.

#include<iostream>

#include<vector>

class bigint

{

private:

std::vector<unsigned char>* m_vector;

size_t m_used;

bool m_signed;

static const unsigned char get_digit(const unsigned char& digits, const size_t& index);

static const size_t bits_per_digit;

static const size_t digits_per_byte;

static const size_t digit_mask;

public:

~bigint(void){delete(m_vector);}

bigint(unsigned char n=0,const bool sign=false);

bigint(const bigint& copy):m_vector(new std::vector<unsigned char>(*copy.m_vector)),m_used(copy.m_used),m_signed(copy.m_signed){}

bigint operator*(const bigint& rhs){bigint n(*this); return(n*=rhs);}

bigint& operator*=(const bigint& rhs);

bigint operator+(const bigint& rhs);

bigint& operator++(void);

bigint operator++(const int);

bigint& operator+=(const bigint& rhs){return(operator=(*this+rhs));}

const bool operator<(const bigint& rhs)const;

const bool operator<=(const bigint& rhs)const{return(operator<(rhs)operator==(rhs));}

bigint& operator=(bigint rhs){swap(rhs);return(*this);}

bigint& operator=(unsigned char n){swap(bigint(n));return(*this);}

const bool operator==(const bigint& rhs)const;

const std::string as_string(void)const;

const bool is_signed(void)const{return(m_signed);}

const size_t length(void)const{return(m_used);}

void swap(bigint& rhs)throw(){std::swap(m_vector,rhs.m_vector);std::swap(m_used,rhs.m_used);std::swap(m_signed,rhs.m_signed);}

};

// Static attributes:

const size_t bigint::digits_per_byte=sizeof(unsigned char)*2; // 2 digits per byte

const size_t bigint::bits_per_digit=4;

const size_t bigint::digit_mask=0xf; // bin:00001111

// Returns the number as a string

const std::string bigint::as_string(void)const

{

const unsigned char zero='0';

size_t used=m_used;

std::string result;

if( !used )

result+=zero;

else

{

result.resize(used,' ');

for(std::vector<unsigned char>::const_iterator it=m_vector->begin(); it!=m_vector->end(); ++it)

{

unsigned char digits=*it;

for(size_t i=0; i<digits_per_byte && used--; ++i)

{

unsigned char value=(unsigned char)(digits&digit_mask);

_ASSERTE( value<10 );

result[used]=value+zero;

digits>>=bits_per_digit;

}

}

if(m_signed && result.front()!=zero)

result.insert(0,"-");

}

return( result );

}

// Default constructor:

bigint::bigint(unsigned char n/*=0*/,const bool sign/*=false*/):m_vector(new std::vector<unsigned char>),m_used(0),m_signed(sign)

{

while(n)

{

unsigned char digits=0;

for(size_t i=0; n&&i<digits_per_byte; n/=10, ++i)

{

unsigned char digit=n%10;

digit<<=(i*bits_per_digit);

digits|=digit;

++m_used;

}

m_vector->push_back(digits);

}

}

// Static function.

// Returns the digit within the given digits at the given index.

const unsigned char bigint::get_digit(

__in const unsigned char& digits,

__in const size_t& index)

{

_ASSERTE(index<digits_per_byte);

const size_t offset=(digits_per_byte-index-1)*bits_per_digit;

unsigned char mask=digit_mask;

mask<<=offset;

unsigned char result=digits&mask;

result>>=offset;

return(result);

}

// Multiply operator overload

bigint& bigint::operator*=(const bigint& n)

{

bigint mult(1);

bigint old(*this);

while(mult<n)

{

operator+=(old);

++mult;

}

return(*this);

}

// Plus operator overload

bigint bigint::operator+(const bigint& n)

{

_ASSERTE(m_vector);

_ASSERTE(n.m_vector);

bigint result;

unsigned char carry=0;

std::vector<unsigned char>::const_iterator first = m_vector->begin();

std::vector<unsigned char>::const_iterator second = n.m_vector->begin();

while( first!=m_vector->end() second!=n.m_vector->end() carry )

{

unsigned char digits=0;

unsigned char digits_1=first!=m_vector->end()?*first++:0;

unsigned char digits_2=second!=n.m_vector->end()?*second++:0;

for(size_t i=0; i<digits_per_byte; ++i )

{

unsigned char digit=carry;

digit+=digits_1&digit_mask; digits_1>>=bits_per_digit;

digit+=digits_2&digit_mask; digits_2>>=bits_per_digit;

carry=digit/10;

digit%=10;

digits|=(digit<<(i*bits_per_digit));

++result.m_used;

if( !carry && !digits_1 && !digits_2 && first==m_vector->end() && second==n.m_vector->end() )

break;

}

result.m_vector->push_back( digits );

}

return( result );

}

// Prefix increment operator overload

bigint& bigint::operator++(void)

{

bigint one(1);

return(operator+=(one));

}

// Postfix increment operator overload

bigint bigint::operator++(const int)

{

bigint old(*this);

operator++();

return(old);

}

// Less than operator overload

const bool bigint::operator<(const bigint& rhs)const

{

_ASSERTE(m_vector);

_ASSERTE(rhs.m_vector);

bool result=m_used<rhs.m_used;

if(!result && m_used==rhs.m_used)

{

_ASSERTE(m_vector->size()==rhs.m_vector->size());

std::vector<unsigned char>::const_reverse_iterator it1=m_vector->rbegin();

std::vector<unsigned char>::const_reverse_iterator it2=rhs.m_vector->rbegin();

for(;it1!=m_vector->rend()&&!result;++it1,++it2)

{

unsigned char digits1=*it1;

unsigned char digits2=*it2;

if(digits1==digits2)

continue;

for(size_t i=0; i<digits_per_byte&&!result; ++i)

{

unsigned char digit1=get_digit(digits1,i);

unsigned char digit2=get_digit(digits2,i);

if(!(result=digit1==digit2))

result=digit1<digit2;

}

}

}

return(result);

}

// Equality operator overload

const bool bigint::operator==(const bigint& rhs)const

{

_ASSERTE(m_vector);

_ASSERTE(rhs.m_vector);

bool result=m_used==rhs.m_used;

if(result)

{

_ASSERTE(m_vector->size()==rhs.m_vector->size());

std::vector<unsigned char>::const_reverse_iterator it1=m_vector->rbegin();

std::vector<unsigned char>::const_reverse_iterator it2=rhs.m_vector->rbegin();

for(;it1!=m_vector->rend()&&result;++it1,++it2)

{

unsigned char digits1=*it1;

unsigned char digits2=*it2;

for(size_t i=0; i<digits_per_byte&&result; ++i)

{

unsigned char digit1=get_digit(digits1,i);

unsigned char digit2=get_digit(digits2,i);

result=digit1==digit2;

}

}

}

return(result);

}

// Non-member operator overload:

// Output stream insertion

std::ostream& operator<<(std::ostream& os,const bigint& n)

{

os<<n.as_string().c_str();

return( os );

}

// Non-member ADL

void swap(bigint& a, bigint& b)

{

a.swap(b);

}

int main()

{

std::cout<<"Factorials from 1 to 100\n"<<std::endl;

bigint num(0);

while(num++<100)

{

bigint factorial(1);

bigint multi(1);

while(multi<num)

factorial*=(++multi);

std::cout<<num<<"! = "<<factorial<<std::endl;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

To print the factors of a given integer:

void print_factors (int n) { int max, f;

unsigned count;

n = abs (n);

max = sqrt (n);

for (count=0, f=1; f<=max; ++f)

if (!(n%f)) printf ("%d\n", f); // f is a factor of n

}

To count the number of odd values in an array a of length n:

unsigned odd_count (int* a, unsigned n) {

int i;

unsigned odd;

for (i=0; i<n; ++i)

if (abs (a[i]) & 1) ++odd;

return odd;

}

The number of even values in an array a of length n is:

n - odd_count (a, n);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write c plus plus program to find the factors of a given number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program to find given number is prime or not?

Yes, do write, or if you're too lazy to your homework, use google.


Could you write a assembly language program in tasm To check whether a given number present in a sequence of given memory location containing the string to be checked in 8086?

8086 assembly language program to check wether given number is perfect or not


Write a C program to extract a given word from a file?

program to extract a given word from a file


One of two factors of a given number?

If the given number has two factors, one of them is 1.


Lab manual in mca 1st sem from ignou?

write a program in C to check whether a given number is apalindrome or not


How can you write a program specification for a given report?

It is your face


Write a C program called MonthDays to find the number of days in a given month Test your code with different input values to demonstrate that your function is robust?

Write a C program called MonthDays to find the number of days in a given month Test your code with different input values to demonstrate that your function is robust?


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


Shell program for gcd of three given numbers?

write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html


How do you know a number if its factors are given?

There are two ways in which the factors can be given. You are given all the prime factors (and their multiplicity). In that case simply multiply them all together. Or You are given each factor. In this case, the biggest of these is the number.


Is the number of factors of a given number is limited?

Yes.


How many distinct strings of factors are there for a given number?

It varies with each given number.