answersLogoWhite

0

Factorial in c program using for loop?

Updated: 8/11/2023
User Avatar

Rajeshkumarg

Lvl 1
14y ago

Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int i,n,fact=1;

clrscr();

printf("enter the number");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("%d",n);

fact=fact*i;

{

printf("the factorial is=%d",fact);

}

getch();

}

By:-Abhishek Goyal(goyal.abhi40@Yahoo.com)

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

The problem with factorials is that small numbers have huge factorials. Even a 64-bit unsigned int is only capable of storing 20 factorial (denoted 20!). Therefore any factorial function that returns a 64-bit unsigned int must assert for values greater than 20.

The for loop version would be:

UINT64 factorial (UINT64 num)

{

assert (num<21);

UINT64 result = 1;

for(UINT64 n=2; n<=num; result *= n++);

return result;

}

However, a while loop is more efficient as a counter is not required:

UINT64 factorial (UINT64 num)

{

assert (num<21);

UINT64 result = 1;

while (1 < num) result *= num--;

return result;

}

To accommodate factorials greater than 20, you have to create a user-defined class to accommodate the result as there are no built-in types available.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include int main() { int fact,Factorial; printf("Please Enter Factorial Number\n"); scanf("%d",&fact); Factorial=func_fact(fact); printf("factorial is %d\n",Factorial); } int func_fact(int number) { int i; int factorial=1; for(i=number;i>=1;i--) { factorial=factorial*i; } return factorial; }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Factorial in c program using for loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

7 Write a C program to compute the factorial of a number using for loop?

int factorial(int n) { int i; int f=1; for(i=2;i&lt;=n;++i) f*=i; return f; }


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


How do you write a c program to design calculator using loop?

Wr


C program algorithm for simple interest using while loop?

Not used


Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }


Sum three real numbers using for loop in c program?

take input n chodo


Write 8085 assembly language program for BCD up counter and display using 8279?

loop: mvi c,59 dcr c mov a,c daa movc,a jnz loop end


How do you find factorial 5 in C programming?

to find the factorial we declare a variable n and initialize its value to 1 initiate a loop for example a for loop and multiply the numbers upto 5 code:- for(i=1,n=1;i&lt;=5;i++) { n=n*i; }


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


What is odd loop in 'C' program?

odd loop means at least the loop execute once.


C program to find factorial of a no?

this is a code for calculating it recursivelly: float Factorial (float n) { if (n&lt;=1) return 1.0; else return n* Factorial(n-1); }


How do you write a c program to print n no's required using while loop?

int i=0; while (i++&lt;n) { /*...*/ }