answersLogoWhite

0

To calculate the factorial of a given number in C on a Unix system, you can use a simple recursive or iterative function. Here's an example of an iterative approach:

#include <stdio.h>

unsigned long long factorial(int n) {
    unsigned long long result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int number;
    printf("Enter a positive integer: ");
    scanf("%d", &number);
    printf("Factorial of %d is %llu\n", number, factorial(number));
    return 0;
}

Compile the code using gcc filename.c -o factorial and run it with ./factorial to calculate the factorial of a number.

User Avatar

AnswerBot

9mo ago

What else can I help you with?

Related Questions

What is UNIX SEQ?

I don't know about a SEQ command, but the 'seq' command in Unix will print a sequence of numbers from first to last, with a given increment. Use the 'man seq' command to find out how to use it.


What is the program to calculate factorial value of given number using BC command in UNIX?

From the manpage of the bc(1) command: The following is the definition of the recursive factorial function. define f (x) { if (x &lt;= 1) return (1); return (f(x-1) * x); } So you could enter that definition of f(), and then call it, for example f(10)


Shell script that accepts a file name starting and ending line numbers as arguments and display all the lines between the given line numbers?

i=1 while [ $i -le $# ] do grep -v Unix $i &gt; $i done


Unix script to calculate average of n numbers given by user?

You really don't want to do this in a shell script - scripting languages in Unix typically do not handle or work with floating values, only integers. A better way would be to write a program to do this that works under Unix, such as a 'C" program. See the related link for an example


How can you debug a C program in Unix?

You can debug C programs using gdb on Unix.


Are there still people using unix?

Yes, quite a bit of companies and users use unix.


How do you find a factorial using Unix?

perl -e 'sub f { my $fu = shift; return 1 if $fu == 1; return f($fu - 1) * $fu; } print f(5), "\n";' just paste that in to a command prompt, change the print f(5) to print f(6) or whatever you want.


What is SEQ?

I don't know about a SEQ command, but the 'seq' command in Unix will print a sequence of numbers from first to last, with a given increment. Use the 'man seq' command to find out how to use it.


In unix how will you find extension of file using commands?

Unix files do not rely on extensions, therefore there is no command to find them.


How do you create dummy file in unix?

using touch command of UNIX. syntax touch &lt;filename&gt; will create dummy regular file.


How do you transfer files from unix to windows?

Unix files can be easily transferred to windows via a network connection either by using FTP or by using Samba. Samba allows a Unix file system to be mounted/shared on a Windows system to look like a windows directory.


How does unix access a file from a directory?

Unix accesses a file from a directory using a hierarchical file system structure. When a command is executed to access a file, the Unix kernel navigates through the directory tree, starting from the root directory, to locate the specified path. Each directory contains entries that map file names to their corresponding inode numbers, which store metadata and point to the actual data blocks on disk. By reading the inode, Unix can access the file's content efficiently.