0% found this document useful (0 votes)
10 views4 pages

Practical 1-1

The document contains two C programs to calculate factorials using iterative and recursive methods. Both programs calculate the factorial of a user-input number, time the calculation, and output the result and elapsed time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Practical 1-1

The document contains two C programs to calculate factorials using iterative and recursive methods. Both programs calculate the factorial of a user-input number, time the calculation, and output the result and elapsed time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1)

i)
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
void factorial(long double n)
{
long double fact=1,i;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nfactorial of %Lf is %Lf",n,fact);
}
void main()
{
long double n;
struct time t1,t2;
clrscr();
printf("ET22BTIT084\n");
printf("Factorial using iterative!\n");
printf("Enter the number:");
scanf("%Lf",&n);
gettime(&t1);
printf("starting time:%d:%d:%d",t1.ti_hour,t1.ti_min,t1.ti_sec);
factorial(n);
gettime(&t2);
printf("\nending time:%d:%d:%d",t2.ti_hour,t2.ti_min,t2.ti_sec);
printf("\ndifference:%d:%d:%d",t2.ti_hour-t1.ti_hour,t2.ti_min-t1.ti_min,t2.ti_sec-t1.ti_sec
);
getch();
}

Output:
ii)
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
int factorial(int n)
{ int fact=1;
if(n==0)
{
return 1;
}
else
{
fact=n*factorial(n-1);
return fact;
}
}
void main()
{ struct time t1,t2;
int n;
clrscr();
printf("ET22BTIT084\n");
printf("Factorial using recursive!\n");
printf("Enter the number for Perform factorial:");
scanf("%d",&n);
gettime(&t1);
printf("The current time is: %d %d %d
%d\n",t1.ti_hour,t1.ti_min,t1.ti_sec,t1.ti_hund);
factorial(n);

printf("Factorial is:%d",factorial(n));
gettime(&t2);

printf("\nThe end time is: %d %d %d


%d\n",t2.ti_hour,t2.ti_min,t2.ti_sec,t2.ti_hund);

printf("\nDifference of time is: %d %d %d


%d",t2.ti_hour-t1.ti_hour,t2.ti_min-t1.ti_min,t2.ti_sec-t1.ti_sec,t2.ti_hund-t1.ti_hund);
getch();
}

Output:

You might also like