Home > Net >  write a program that computes and displays the factorial of a positive integer n entered by the user
write a program that computes and displays the factorial of a positive integer n entered by the user

Time:10-13

Been working on this question for class. Not sure what I'm doing wrong.

Seems like I just don't have the correct format. My professor wants the output to look like "5!=1 * 2* 3* 4* 5=120"

Can someone help me with this? Below is what I have so far:

#include <iostream> 
#include <stdio.h>

int main() {
    int n, i, fact;

    printf("Enter a positive integer: \n");
   scanf("%d", &n);
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for( i = 1; i <= n;   i) {
            fact *= i;
            n= n * (n-1);
        }
        printf("Factorial of %d = ", &n, &fact) ;    
    }

    return 0;
}

CodePudding user response:

  1. remove #include <iostream> if it is the C code
  2. Use functions.
  3. Initialize local variables (you do not and it is undefined behaviour)
  4. Compile C program using C compilers
unsigned long long fact(unsigned val, char *buff)
{
    unsigned result = 1;
    *buff   = '1';
    *buff = 0;
    for(unsigned c = 2; c <= val; c  ) 
    {
        buff  = sprintf(buff, "x%u", c);
        result *= c;
    }
    return result;
}

int main(void)
{
    unsigned x;
    char y[1000];
    unsigned long long res;

    if(scanf("%u", &x) == 1){res = fact(x, y); printf("factoral(%u) = %s = %llu\n", x, y, res);}
    else printf("Invalid number!!!\n");
}

or without printing steps

unsigned long long fact(unsigned val)
{
    unsigned result;
    switch(val)
    {
        case 0: 
        case 1:
            result = 1;
            break;
        case 2:
            result = 2;
            break;
        default:
            result = 2;
            while(val > 2) result *= val--;
            break;
    }
    return result;
}

int main(void)
{
    unsigned x;

    if(scanf("%u", &x) == 1) printf("factioral(%u) = %llu\n", x, fact(x));
    else printf("Invalid number!!!\n");
}

https://godbolt.org/z/Tf5431zco

CodePudding user response:

For starters it does not make a sense to declare the variable n as having a signed integer type. It is better to declare it as having unsigned integer type. For example

unsigned int n, i, fact;

Secondly the variable fact was not initialized. You should initialize it by the value equal to 1.

So the above declaration could look like

unsigned int n, i;
unsigned long long fact = 1;

In this for loop

    for( i = 1; i <= n;   i) {
        fact *= i;
        n= n * (n-1);
    }

the statement

        n= n * (n-1);

does not make a sense and shall be removed.

In the printf call you have to use values of variables instead of pointers to the variables like

printf("Factorial of %u = %llu\n", n, fact) ;

Pay attention to that neither declaration from the header <iostream> is used in your program So whether it is a C program or a C program nevertheless remove this header.

Here is a demonstration C program.

#include <stdio.h>

int main( void )
{
    unsigned int n = 0;
    unsigned long long int fact = 1;

    printf( "Enter a positive integer: " );
    scanf( "%u", &n );

    printf( "%u! = ", n );

    for ( unsigned int i = 1; i <= n; i   )
    {
        fact *= i;

        if ( i != 1 ) printf( " * " );

        printf( "%u", i );
    }

    printf( " = %llu\n", fact );
}

The program output might look like

Enter a positive integer: 5
5! = 1 * 2 * 3 * 4 * 5 = 120

If you are allowed to use only variables of the type int as you wrote in a comment to the answer then the program can look like

#include <stdio.h>

int main( void )
{
    int n = 0;
    int fact = 1;

    printf( "Enter a positive integer: " );
    scanf( "%d", &n );

    if  ( n  <  0 )
    {
        puts( "Error! Factorial of a negative number doesn't exist."); 
    }
    else 
    {
        printf( "%d! = ", n );

        for ( int i = 1; i <= n; i   )
        {
            fact *= i;

            if ( i != 1 ) printf( " * " );

            printf( "%d", i );
        }

        printf( " = %d\n", fact );
    }   
}
  • Related