Write a program to print factorial of given number without using recursion?

#include
#include
void main(){
int i,fact=1,number;
clrscr();
printf(“Enter a number: “);
scanf(“%d”,&number);

for(i=1;i<=number;i++){
fact=fact*i;
}
printf(“Factorial of %d is: %d”,number,fact);
getch();
}

Certainly! Here’s a C program to calculate the factorial of a given number without using recursion:

c
#include <stdio.h>

unsigned long long factorial(int num) {
unsigned long long fact = 1;

// Multiply 'fact' by each integer from 1 to num
for (int i = 1; i <= num; ++i) {
fact *= i;
}

return fact;
}

int main() {
int number;
unsigned long long fact;

printf("Enter a number: ");
scanf("%d", &number);

if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
fact = factorial(number);
printf("Factorial of %d is: %llu\n", number, fact);
}

return 0;
}

This program defines a function factorial that calculates the factorial of a given number using a loop, and then in the main function, it takes user input and calls this function to compute and print the factorial. It handles the case of negative numbers gracefully by printing a message that factorial is not defined for negative numbers.