You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"> **_Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8\n",
89
+
"> Then, the output should be:40320_**\n",
90
+
"\n",
91
+
"---\n",
92
+
"\n",
93
+
"### Hints:\n",
94
+
"\n",
95
+
"> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
96
+
"\n",
97
+
"---\n",
98
+
"\n",
99
+
"**Main author's Solution: Python 2**"
100
+
]
101
+
},
102
+
{
103
+
"cell_type": "code",
104
+
"execution_count": null,
105
+
"metadata": {},
106
+
"outputs": [],
107
+
"source": [
108
+
"def fact(x):\n",
109
+
" if x == 0:\n",
110
+
" return 1\n",
111
+
" return x * fact(x - 1)\n",
112
+
"\n",
113
+
"x = int(raw_input())\n",
114
+
"print fact(x)"
115
+
]
116
+
},
117
+
{
118
+
"cell_type": "markdown",
119
+
"metadata": {},
120
+
"source": [
121
+
"---\n",
122
+
"\n",
123
+
"**My Solution: Python 3**\n",
124
+
"\n",
125
+
"- **Using While Loop**\n"
126
+
]
127
+
},
128
+
{
129
+
"cell_type": "code",
130
+
"execution_count": null,
131
+
"metadata": {},
132
+
"outputs": [],
133
+
"source": [
134
+
"n = int(input()) #input() function takes input as string type\n",
135
+
" #int() converts it to integer type\n",
136
+
"fact = 1\n",
137
+
"i = 1\n",
138
+
"while i <= n:\n",
139
+
" fact = fact * i;\n",
140
+
" i = i + 1\n",
141
+
"print(fact)"
142
+
]
143
+
},
144
+
{
145
+
"cell_type": "markdown",
146
+
"metadata": {},
147
+
"source": [
148
+
"- **Using For Loop**\n"
149
+
]
150
+
},
151
+
{
152
+
"cell_type": "code",
153
+
"execution_count": null,
154
+
"metadata": {},
155
+
"outputs": [],
156
+
"source": [
157
+
"n = int(input()) #input() function takes input as string type\n",
158
+
" #int() converts it to integer type\n",
159
+
"fact = 1\n",
160
+
"for i in range(1,n+1):\n",
161
+
" fact = fact * i\n",
162
+
"print(fact)"
163
+
]
164
+
},
165
+
{
166
+
"cell_type": "markdown",
167
+
"metadata": {},
168
+
"source": [
169
+
"- **Using Lambda Function**\n"
170
+
]
171
+
},
172
+
{
173
+
"cell_type": "code",
174
+
"execution_count": null,
175
+
"metadata": {},
176
+
"outputs": [],
177
+
"source": [
178
+
"# Solution by: harshraj22\n",
179
+
"\n",
180
+
"n = int(input())\n",
181
+
"def short_fact(x): return 1 if x <= 1 else x*short_fact(x-1)\n",
182
+
"print(short_fact(n))\n"
183
+
]
184
+
},
185
+
{
186
+
"cell_type": "markdown",
187
+
"metadata": {},
188
+
"source": [
189
+
"---\n",
190
+
"\n",
191
+
"# Question 3\n",
192
+
"\n",
193
+
"### **Question:**\n",
194
+
"\n",
195
+
"> **_With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8_**\n",
"> **_In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()_**\n",
216
+
"\n",
217
+
"---\n",
218
+
"\n",
219
+
"**Main author's Solution: Python 2**"
220
+
]
221
+
},
222
+
{
223
+
"cell_type": "code",
224
+
"execution_count": null,
225
+
"metadata": {},
226
+
"outputs": [],
227
+
"source": [
228
+
"n = int(raw_input())\n",
229
+
"d = dict()\n",
230
+
"for i in range(1,n+1):\n",
231
+
" d[i] = i * i\n",
232
+
"print d"
233
+
]
234
+
},
235
+
{
236
+
"cell_type": "markdown",
237
+
"metadata": {},
238
+
"source": [
239
+
"**My Solution: Python 3:**\n",
240
+
"\n",
241
+
"- **Using for loop**"
242
+
]
243
+
},
244
+
{
245
+
"cell_type": "code",
246
+
"execution_count": null,
247
+
"metadata": {},
248
+
"outputs": [],
249
+
"source": [
250
+
"n = int(input())\n",
251
+
"ans = {}\n",
252
+
"for i in range (1,n+1):\n",
253
+
" ans[i] = i * i\n",
254
+
"print(ans)"
255
+
]
256
+
},
257
+
{
258
+
"cell_type": "markdown",
259
+
"metadata": {},
260
+
"source": [
261
+
"- **Using dictionary comprehension**"
262
+
]
263
+
},
264
+
{
265
+
"cell_type": "code",
266
+
"execution_count": null,
267
+
"metadata": {},
268
+
"outputs": [],
269
+
"source": [
270
+
"n = int(input())\n",
271
+
"ans={i : i*i for i in range(1,n+1)}\n",
272
+
"print(ans)"
273
+
]
274
+
},
275
+
{
276
+
"cell_type": "markdown",
277
+
"metadata": {},
278
+
"source": [
279
+
"---\n",
280
+
"\n",
281
+
"## Conclusion\n",
282
+
"\n",
283
+
"**_These was the solved problems of day 1. The above problems are very easy for the basic syntex learners.I have shown some easy ways of coding in my solutions. Lets see how to face and attack new problems in the next day._**\n",
284
+
"\n",
285
+
"[**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md \"Next Day\")\n",
0 commit comments