-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_factorial.c
76 lines (64 loc) · 1.57 KB
/
19_factorial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*****************************************************************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :24.06.2021
DESCRIPTION :19. WAP to find factorial for given number using recursive method and without
using any other function than main function
OUTPUT :./a.out
ENTER THE NUMBER FOR FACTORIAL:5
THE FACTORIAL IS: 120
DO YOU WANT TO CONTINUE:y
ENTER THE NUMBER FOR FACTORIAL: 0
THE FACTORIAL IS: 1
DO YOU WANT TO CONTINUE:y
ENTER THE NUMBER FOR FACTORIAL:6
THE FACTORIAL IS: 720
DO YOU WANT TO CONTINUE:n
*****************************************************************************************************/
#include<stdio.h>
static int num = -1, fact;
int main()
{
char choice;
if(num >= 0)
{
if(num > 1)
{
fact = fact * num;
main(--num);
}
else
{
printf("THE FACTORIAL IS: %d\n",fact);
printf("\nDO YOU WANT TO CONTINUE:");
scanf(" %c",&choice);
if( choice == 'Y' || choice == 'y' ) //to execute as new num=-1
main(num = -1);
else
return 1;
}
}
else if(num < 0) //num is initialised to -1
{
printf("\nENTER THE NUMBER FOR FACTORIAL: ");
scanf("%d", &num);
fact = 1;
int limit = 1;
for(int i = 0; i < 20; i++)
{
limit = limit * 2;
}
if(num < 0 || num > limit)
{
printf("NUMBER IS INVALID\n"); //error msg on invalid input
printf("\nDO YOU WANT TO CONTINUE:");
scanf(" %c",&choice);
if( choice == 'Y' || choice == 'y' )
main(num = -1); //to execute as new num=-1
else
return 1;
}
else
main(num);
}
}