This blog is under construction

Saturday, 22 June 2013

C program to find factorial of a given number

Write a C program to find factorial of a given number without using recursion.


  #include <stdio.h>
  int main() {
        int n, res = 1;

        /* get the input from user */
        printf("Enter your input:");
        scanf("%d", &n);

        /* calculate factorial for the given input */
        while (n > 0) {
                res = res * n;
                n--;
        }

        /* print the result */
        printf("Result: %d\n", res);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:5
  Result: 120



No comments:

Post a Comment