-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoop statements (29-42)
290 lines (236 loc) · 6.62 KB
/
Loop statements (29-42)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
using namespace std;
// wap to print hello world 5 times
void hello_World_5_Times()
{
for (int i = 1; i <= 5; i++)
cout << "Hello World " << endl;
}
// wap to print natural numbers (1 to 10)
void natural_Numbers_1_to_10()
{
for (int i = 1; i <= 10; i++)
cout << i << endl;
}
// wap to print natural numbers in reverse order (10 to 1)
void rev_Natural_Numbers_10_to_1()
{
for (int i = 10; i >= 1; i--)
{
cout << i << endl;
}
}
// wap to find factorial of a number using while loop
void fact_While()
{
int i = 1, num, fact = 1;
cout << "enter number to find factorial : " << endl;
cin >> num;
while (i <= num)
{
fact = fact * i;
i++;
}
cout << "Factorial of " << num << " is " << fact;
}
// wap to print fibonacci series until given terms
void fibonacci()
{
int x = 0, y = 1, z = 0, num;
cout << "Enter number to find fibanacci series upto that term : " << endl;
cin >> num;
while (z <= num)
{
x = y;
y = z;
z = x + y;
cout << z << endl;
}
}
// WAP to display the sum of n natural numebrs where n is entered by the user
void sum_Of_n_Natural_Num()
{
int i = 1, num, sum = 0;
cout << "Enter number to find sum of n natural numbers entered by the user : " << endl;
cin >> num;
while (i <= num)
{
sum = sum + i;
i++;
cout << sum << endl;
}
}
// WAP to display reverse of any number using loop
void reverse_Of_Num()
{
int rev = 0, num;
cout << "Enter number to find its reverse : " << endl;
cin >> num;
while (num > 0)
{
rev = (rev * 10) + (num % 10);
num = num / 10;
}
cout << "Reverse is : " << rev;
}
// WAP to sum all the numbers entered by the user until user enters 0
void sum_Of_All_Until_0()
{
float sum = 0, num;
do
{
cout << "Enter number : " << endl;
cin >> num;
sum = sum + num;
} while (num != 0);
{
cout << " sum is : " << endl
<< sum;
}
}
// WAP to display reverse of a five digit number {
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
//}
// WAP to print numbers within the range given by the user
void num_Within_the_Range()
{
int num1, num2;
cout << "Enter first number : " << endl;
cin >> num1;
cout << "Enter last number : " << endl;
cin >> num2;
for (int i = num1; i <= num2; i++)
{
cout << i << endl;
}
}
// WAP to print number table using multiplication operation
void table_Using_Multiplication_operation()
{
int num;
cout << "Enter number to find it's multiplication table" << endl;
cin >> num;
for (int i = 1; i <= 10; i++)
{
cout << num << " * " << i << " = " << num * i << endl;
}
}
// WAP to print number table using Addition operation
void table_Using_Addition_operation()
{
int num;
cout << "Enter number to find it's multiplication table" << endl;
cin >> num;
for (int i = 1; i <= 10; i++)
{
cout << num << " + " << i << " = " << num + i << endl;
}
}
// WAP to generate prime numbers series upto n numbers using for loop
void prime_Number_Upto_n()
{
int num, count = 0;
cout << "Enter number till which you want to find prime number : " << endl;
cin >> num;
for (int i = 2; i <= num; i++)
{
if (num % i == 0)
{
count = count + 1;
}
if (count > 1)
{
cout << num;
}
}
}
// WAP to find sum of all digits of a number using loop
void Sum_Of_All_Digits()
{
int num, sum = 0;
cout << "Enter number to find sum of all digits : " << endl;
cin >> num;
while (num > 0)
{
sum = sum + num % 10;
num = num / 10;
}
cout << "sum is : " << sum << endl;
}
int main()
{
cout << "<_____________Enter number to run Program accordingly_____________> : " << endl;
cout << endl;
cout << "Enter 1 to print Hello World 5 times : " << endl;
cout << "Enter 2 to print natural numbers (1 to 10) : " << endl;
cout << "Enter 3 to print natural numbers in reverse order (10 to 1) : " << endl;
cout << "Enter 4 to find factorial of a number using while loop : " << endl;
cout << "Enter 5 to print Fabonacci series until given terms : " << endl;
cout << "Enter 6 to display sum of n natural numbrers entered by the user : " << endl;
cout << "Enter 7 to display reverse of any number using loop : " << endl;
cout << "Enter 8 to sum all the numbers entered by the user until the user enters 0 : " << endl;
cout << "Enter 9 to print reverse of 5 digit number using loop : " << endl;
cout << "Enter 10 to print numbers within the range given by the user : " << endl;
cout << "Enter 11 to print number table using Multiplication operation : " << endl;
cout << "Enter 12 to print number table using Addition operation : " << endl;
cout << "Enter 13 to generate Prime numbers series up to n numbers using for loop : " << endl;
cout << "Enter 14 to find the sum of all digits of a digit using a loop : " << endl;
cout << endl;
int count;
cout << "Enter here " << endl;
cin >> count;
switch (count)
{
case 1:
hello_World_5_Times();
break;
case 2:
natural_Numbers_1_to_10();
break;
case 3:
rev_Natural_Numbers_10_to_1();
break;
case 4:
fact_While();
break;
case 5:
fibonacci();
break;
case 6:
sum_Of_n_Natural_Num();
break;
case 7:
reverse_Of_Num();
break;
case 8:
sum_Of_All_Until_0();
break;
// case 9:
// ();
// break;
case 10:
num_Within_the_Range();
break;
case 11:
table_Using_Multiplication_operation();
break;
case 12:
table_Using_Addition_operation();
break;
case 13:
prime_Number_Upto_n();
break;
case 14:
Sum_Of_All_Digits();
break;
default:
break;
}
return 0;
}