This blog is under construction

Saturday 22 June 2013

C program to calculate factorial of first N numbers

Write a C program to find the factorial of first N numbers.


  #include <stdio.h>
  int main() {
        int i, n, res, data;
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* calculate factorial of n numbers */
        for (i = 1; i <= n; i++) {
                res = 1;
                data = i;

                /* factorial manipulation - data! */
                while (data > 0) {
                        res = res * data;
                        data--;
                }

                printf("%d\n", res);
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:10
  1
  2
  6
  24
  120
  720
  5040
  40320
  362880
  3628800



No comments:

Post a Comment