UAB – 2005: Problem 1: Factorial Calculator


If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1) * (n-2) * ( n-3)… *

By convention, 0! = 1.

You must write a program that allows a user to enter an integer between 1 and 7.
Your program must then compute the factorial of the number entered by the user.

Your solution MUST actually perform a computation (i.e., you may not simply print “5040” to the screen as a literal value if the input is 7).

Example 1

Enter a number: 4
4! = 24

Example 2

Enter a number: 7
7! = 5040


Enter a number: 0
0! = 1
Enter a number: 1
1! = 1
Enter a number: 2
2! = 2
Enter a number: 3
3! = 6
Enter a number: 4
4! = 24
Enter a number: 5
5! = 120
Enter a number: 6
6! = 720
Enter a number: 7
7! = 5040

// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main (int argc, char *argv [])
{
    printf ("Enter a number: ");
    
    int n; 
    scanf ("%d", &n);
    
    int factorial = 1;
    
    for ( int i = 1; i <= n; i++ ) 
        factorial *= i;
    
    printf ("%d! = %d\n", n, factorial);
    
    return 0;
}

// @END_OF_SOURCE_CODE
// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int factorial(int n)
{
    if (n <= 1) return 1;

    return n * factorial(n - 1);
}

int main(int argc, const char * argv[])
{
    printf ("Enter a number: ");

    int n;

    scanf ("%d", &n);

    printf ("%d! = %d\n", n, factorial(n));

    return 0;
}

// @END_OF_SOURCE_CODE

import sys
 
sys.stdout.write ('Enter a number: ')
 
n = int (input ())
factorial = 1
 
for i in range (1, n + 1) :
    factorial *= i
 
print ('%d! = %d' % (n, factorial))

16 thoughts on “UAB – 2005: Problem 1: Factorial Calculator

  1. There are another way to solve this problem

    solution:

    #include <stdio.h>
    //#include
    
    int rec(int num);
    
    int main()
    {
        int number;
        int rezult;
        printf("Enter a number: ");
        scanf("%d",&number);
        rezult = rec(number);
        printf("%d! = %d\n",number,rezult);
        //system(“PAUSE”);
        return 0;
    }
    int rec(int num)
    {
        if(num == 1)
            return 1;
        /* else */
        return (num * rec(num-1));
    }
    
    
  2. #include
    int func(int);
    int main()
    {int a,b,c,i;
    printf(“enter a number “);
    while(scanf(“%d”,&a)!=EOF)
    {
    c=func(a);
    printf(“%d!=%d”,a,c);
    }
    return 0;}
    int func(int a)
    {int b;
    if(a==1)
    return 1;
    else
    while(a!=1)
    {
    b=a*func(a-1);
    return b;
    }
    }

  3. Here is an example written in VB

            Console.Write("Enter a number: ")
            Dim num As Integer = Console.ReadLine
            Dim total As Integer = num
    
            For i As Integer = 1 To num - 1
                total = total * (num - i)
            Next
    
            Console.WriteLine(total.ToString)
            Console.ReadLine()
    
  4. #include
    int main()
    {
    int a, n, factorial;
    printf(” n = “);
    scanf(“%d”, &n);
    for(a = 1, factorial = 1; a <= n; a++){
    factorial = factorial * a;
    }
    printf("n ! = %d", factorial);

    return 0;

    }

  5. typical beginner’s program:

    #include
    int main(){
    int x;
    int *p=malloc(sizeof(*p));
    p=&x;
    printf(“please enter an integer number between 1 and 7: “);
    scanf(“%d”,p);
    printf(“\n”);
    printf(“the number you entered is: %d\n”,*p);
    printf(“\n”);
    if(x==1){
    printf(“the factorial of 1 is: %d\n”,x);
    }
    else if(x==2){
    printf(“the factorial of 2 is: %d\n”,x*(x-1));
    }
    else if(x==3){
    printf(“the factorial of 3 is: %d\n”,x*(x-1)*(x-2));
    }
    else if(x==4){
    printf(“the factorial of 4 is: %d\n”,x*(x-1)*(x-2)*(x-3));
    }
    else if(x==5){
    printf(“the factorial of 5 is: %d\n”,x*(x-1)*(x-2)*(x-3)*(x-4));
    }
    else if(x==6){
    printf(“the factorial of 6 is: %d\n”,x*(x-1)*(x-2)*(x-3)*(x-4)*(x-5));
    }
    else if(x==7){
    printf(“the factorial of 7 is: %d\n”,x*(x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6));
    }
    else{
    printf(“please enter a number between 1 and 7 to get its factorial. Thanks.\n”);
    }
    printf(“\n”);
    free(p);
    return 0;
    }

  6. solution:
    #include
    int main()
    {
    int a,i,f=1,j;
    printf(“Enter a number:”);
    for(j=0;j7)
    break;
    else
    {
    for(i=1;i<a+1;i++)
    {
    f*=i;
    }
    }
    }
    printf("%d!=%d",a,f);
    return 0;
    }

  7. as I am more advanced now….

    #include
    int main(){
    int i,x,sum,total=1;
    printf(“please enter an integer: “);
    scanf(“%d”,&x);
    printf(“\n”);
    if(x>0&&x<8){
    for(i=0;i<x;i++){
    sum=x-i;
    total=total*sum;
    }
    printf("the factorial of %d is %d\n",x,total);
    }
    else{printf("please enter a number between 1 and 7.\n");}
    return 0;
    }

  8. #include
    #include
    void main()
    {
    int n,a,factorial=1;
    printf(“Enter the valoue of n:”);
    if(n==0)
    {
    printf(“The factorial of %d is=%d”,n,factorial);
    }
    else if(n>0)
    {
    for(a=1;a<=n;a++)
    factorial=factorial*a;
    printf("The factorial of %d is=%d",n,factorial);
    }
    else
    printf("Negative Number is not allowed");
    getch();
    }

  9. #include
    int factorial(int n);
    int main()
    {
    int n;
    ans=factorial(n);
    printf(“%d”,ans);

    return 0;
    }

    int factorial(int n)

    {
    int fact;
    if(n>0)
    {
    fact=n*factorial(n-1);
    return fact;
    }
    else
    return 1;

    }

  10. #include
    int main()
    {
    printf (“Enter a number: “);
    int n ,i, factorial = 1;
    scanf (“%d”, &n);
    for (i = 1; i <= n; i++ )
    factorial *= i;

    printf ("%d! = %d\n", n, factorial);

    return 0;
    }

  11. #include
    #include

    int main ()

    {
    int f,i,s=1;
    printf(“input the number : “);
    scanf(“%d”,&f);
    if(f<=7){ for(i=1;i7) {printf(“please input less than or equal 7”);}
    getch ();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s