Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why my factorial is showing 0

0 votes
asked Apr 29, 2021 by Faiyaz Mahmud (130 points)
#include <iostream>
#include <cmath>
using namespace std;

void power(int *a,int *b,int *c);
void factorial(int *a ,int *b,int *c);
int main()
{
    int x,y,z1,z2;
    
    cout<<"Enter the value of x :" ;
    cin>>x;
    cout<<"Enter the value of y :";
    cin>>y;
    
    power(&x,&y,&z1);
    
    cout<<z1<<endl;
    factorial(&x,&y,&z2);
     cout<<z2<<endl;
    
    if(z1>z2){
        cout<<x<<"^"<<y<<"is greater than "<<x<<"!"<<"+"<<y<<"!";
    }
    else{
        cout<<x<<"^"<<y<<"is smaller than "<<x<<"!"<<"+"<<y<<"!";
    }
    return 0;
}  
void power(int *a,int *b,int *c){
    
    *c=pow(*a,*b);
    
}

void factorial(int *a ,int *b,int *c){
    
    int *i,*fact1,*fact2 ;
    i=new int;
    fact1 =new int;
    fact2 =new int;
    
    *fact1=1;
    *fact2=1;
    
    if(*a>1){
        for(*i=1;*i<=*a;*i++){
            *fact1 =(*fact1) * (*i);
        }
    }

    if(*b>1){
        for(*i=1;*i<=*b;*i++){
            *fact2 =(*fact2) * (*i);
        }
    }

    
    cout<<*fact1<<endl<<*fact2<<endl;
    
    *c= *fact1 + *fact2;
    
    
}

2 Answers

+3 votes
answered May 3, 2021 by Peter Minarik (86,040 points)
edited May 5, 2021 by Peter Minarik

You are way overusing pointers unnecessarily. This also somewhat slows down your code.

Why your code prints the wrong value is that you got confused on the operator precedence. Post-increment comes before dereferencing pointes, so 

for (*i = 1; *i <= *a; *i++)

doesn't do what you think it does. The above code actually moves the pointer (i) to the next int in the memory, then dereferences it.

What you should have done is

for (*i = 1; *i <= *a; (*i)++)

Nevertheless, your code is way overcomplicated.

Here's my enhancement of your code:

#include <iostream>
#include <cmath>

static long int factorial(int n)
{
    long int product = 1;
    while (n > 1)
        product *= n--;

    return product;
}

int main()
{
    int x, y;
    
    std::cout << "Enter the value of x: ";
    std::cin >> x;
    std::cout << "Enter the value of y: ";
    std::cin >> y;
    
    double power = pow(x, y);
    long int factSum = factorial(x) + factorial(y);

    std::cout << x << "^" << y << " = " << power;
    if (power > factSum)
        std::cout << " is greater than ";
    else if (power < factSum)
        std::cout << " is smaller than ";
    else
        std::cout << " is the same as ";
    std::cout << x << "!" << " + " << y << "! = " << factSum << std::endl;

    return 0;
}
commented May 7, 2021 by Faiyaz Mahmud (130 points)
THANK YOU :)
0 votes
answered May 4, 2021 by Vipul Kumar (140 points)
#include <iostream>
using namespace std;

int main()
{
    int num,fac= 1,i;

    cout << "Enter a number";
    cin >> num;
        for(i = 1; i <=num; i++)
        {
            fac =fac* i;
        }
        cout << "Factorial of " <<fac;    

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...