// All_tasks.cpp
C++ program: gather 4 tasks programmers
task(1) shows the sizes of all variables in C++ in bytes
task(2) ordering numbers: even, odd & prime (descending-ascending)
task(3) factorial
task(4) shows the day of the week by date given.
programming C++.
using functions
1. 1 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
Task 5:
//All_tasks.cpp
C++ program: gather all tasks
programmers
Prepared By:
Khalid Waleed
CANAL HIGHER INSTITUTE OF ENGINEERING & TECHNOLOGY
2. 2 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
1: // All_tasks.cpp
2: // gather all tasks programmers
3: #include <iostream>
4: #include <iomanip>
5: using namespace std;
6: void error(int &);
7: void line();
8: void sizes();
9: void numbers();
10: void factorial();
11: void daycalc();
12: int main()
13: {
14: int q; char Y;
15: do
16: {
17: line();
18: cout << setw(30)<<"khalid's projects"<< endl;
19: line();
20: cout <<"1- sizes.cpp"<< setw(25)<<"2- numbers.cpp"<<endl;
21: cout <<"3- factorial.cpp"<< setw(28)<<"4- daycalculation.cpp"<< endl;
22: line();
23: cout <<"Enter number of programme to open: ";
24: cin >> q;
25: error(q);
26: switch(q)
27: {
28: case 4:
29: cout << endl;
30: daycalc();
31: break;
32: case 1:
33: cout << endl;
34: sizes();
35: break;
36: case 2:
37: cout << endl;
38: numbers();
3. 3 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
39: break;
40: case 3:
41: cout << endl;
42: factorial();
43: break;
44: default:
45: cout<<"Unknown number!"<< 'n';
46: }
47: cout <<"openkhalid's projects (y/n)? :";
48: cin >> Y;
49: cout << endl;
50: }while(Y == 'y');
51: return 0;
52: }
53: void sizes()
54: {
55: int x;
56: cout <<"Size of bool"<< setw(11)<<":"<< setw(6)<< sizeof(bool)<<" Byte"<< endl;
57: cout <<"Size of char"<< setw(11)<<":"<< setw(6)<< sizeof(char)<<" Byte"<< endl;
58: cout <<"Size of unsigned char :"<< setw(6)<< sizeof(unsigned char)<<" Byte"<<
endl;
59: cout <<"Size of short"<< setw(10)<<":"<< setw(6)<< sizeof(short)<<" Byte"<< endl;
60: cout <<"Size of unsigned short:"<< setw(6)<< sizeof(unsigned short)<<" Byte"<<
endl;
61: cout <<"Size of long"<< setw(11)<<":"<< setw(6)<< sizeof(long)<<" Byte"<< endl;
62: cout <<"Size of unsigned long : "<< setw(5)<< sizeof(unsigned long)<<" Byte"<<
endl;
63: cout <<"Size of int"<< setw(12)<<":"<< setw(6)<< sizeof(int)<<" Byte"<< endl;
64: cout <<"Size of unsigned int"<< setw(3)<<":"<< setw(6)<< sizeof(unsigned int)<<"
Byte"<< endl;
65: cout <<"Size of short int"<< setw(6)<<":"<< setw(6)<< sizeof(short int)<<" Byte"<<
endl;
66: cout <<"Size of long int"<< setw(7)<<":"<< setw(6)<< sizeof(long int)<<" Byte"<<
endl;
67: cout <<"Size of float"<< setw(10)<<":"<< setw(6)<< sizeof(float)<<" Byte"<< endl;
68: cout <<"Size of double"<< setw(9)<<":"<< setw(6)<< sizeof(double)<<" Byte"<<
endl;
4. 4 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
69: cout <<"Size of long double"<< setw(4)<<":"<< setw(6)<< sizeof(long double)<<"
Byte"<< endl;
70: cout << endl <<"write any number to exit programme: ";
71: cin >> x;
72: error(x);
73: }
74: void numbers()
75: {
76: int process , numb , f=0;
77: char ask;
78: do{
79: cout <<" 1- ordering even numbers n 2- ordering odd numbers
80: n 3- ordering prime numbers";
81: cout <<"ninsert number of process : ";
82: cin >> process;
83: error(process);
84: switch(process)
85: {
86: case 1:
87: cout <<"n 4- desending order n 5- ascending order ";
88: cout <<"ninsert number of process :";
89: cin >> process;
90: error(process);
91: switch(process)
92: {
93: case 4:
94: cout <<"Enter your number : ";
95: cin >> numb;
96: error(numb);
97: for (numb ; numb>0;--numb)
98: {
99: if (numb%2==0)
100: {
101: cout << numb <<", ";
102: }
103: }
104: break;
105: case 5:
5. 5 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
106: cout <<"Enter your number : ";
107: cin >> numb;
108: error(numb);
109: for (int c=1; numb>=c ; c++)
110: {
111: if (c%2==0)
112: {
113: cout << c <<", ";
114: }
115: }
116: break;
117: default:
118: cout <<"number of process unknown !";
119: }
120: break;
121: case 2:
122: cout <<"n 6- desending order n 7- ascending order ";
123: cout <<"ninsert number of process : ";
124: cin >> process;
125: error(process);
126: switch(process)
127: {
128: case 6:
129: cout <<"Enter your number : ";
130: cin >> numb;
131: error(numb);
132: for (numb ; numb>0;--numb)
133: {
134: if (numb%2!=0)
135: {
136: cout << numb <<", ";
137: }
138: }
139: break;
140: case 7:
141: cout <<"Enter your number : ";
142: cin >> numb;
143: error(numb);
6. 6 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
144: for (int c=1; numb>=c ; c++)
145: {
146: if (c%2!=0)
147: {
148: cout << c <<", ";
149: }
150: }
151: break;
152: default:
153: cout <<"number of process unknown !";
154: }
155: break;
156: case 3:
157: cout <<"n 8- desending order n 9- ascending order ";
158: cout <<"ninsert number of process : ";
159: cin >> process;
160: error(process);
161: switch(process)
162: {
163: case 8:
164: cout <<"Enter your number : ";
165: cin >> numb;
166: error(numb);
167: for (numb ; numb>=2;--numb)
168: {
169: f=0;
170: for (int j=2; j<=numb/2; j++)
171: {
172: if (numb%j ==0)
173: {
174: f=1;
175: break;
176: }
177: }
178: if (f==0)
179: cout << numb <<", ";
180: }
181: break;
7. 7 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
182: case 9:
183: cout <<"Enter your number : ";
184: cin >> numb;
185: error(numb);
186: for ( int c=2; numb>=c ; c++)
187: {
188: f=0;
189: for (int j=2; j<=c/2; j++)
190: {
191: if (c % j ==0)
192: {
193: f=1;
194: break;
195: }
196: }
197: if (f==0)
198: cout << c <<", ";
199: }
200: break;
201: default:
202: cout <<"number of process unknown !";
203:
204: }
205: break;
206: default:
207: cout <<"number of process unknown !";
208: }
209: cout <<"nDo you want to exit the programme (y/n) ? ";
210: cin >> ask;
211: }while(ask == 'n');
212: }
213: void daycalc()
214: {
215: int day, month, m, year, century, day_of_week;
216: char ask;
217: cout << program to show the day of the week by date n";
218: line( );
219: cout <<"centuries availiable : 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23";
8. 8 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
220: cout <<"n ***** Note: we are now in the 21st century ***** n";
221: do
222: {
223: cout <<"please Enter the century number: ";
224: cin >> century;
225: error(century);
226: switch (century)
227: {
228: case20:
229: century;
230: break;
231: case21:
232: --century;
233: break;
234: case19:
235: century+=2;
236: break;
237: case18:
238: century++;
239: break;
240: case22:
241: century+=2;
242: break;
243: case23:
244: century+=2;
245: break;
246: case17:
247: century+=2;
248: break;
249: case16:
250: century+=3;
251: break;
252: case15:
253: century+=3;
254: break;
255: case14:
256: century-=2;
257: break;
9. 9 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
258: case13:
259: century--;
260: break;
261: default:
262: cout <<"century number not in the list n";
263: cout <<"Day calculation may not be accurate n";
264: cout <<"do you want to continue (y/n) ?";
265: cin >> ask;
266: if(ask == 'y')
267: break;
268: else
269: goto again;
270: }
271: cout <<"Enter the date (day/month/year) n";
272: cout <<"daynumber = "; cin >> day;
273: error(day);
274: if (day >31)
275: {
276: cout <<"Unknown day! n";
277: goto again;
278: }
279: cout <<"month number = "; cin >> month;
280: error(month);
281: switch(month)
282: {
283: case1:
284: m =6;
285: break;
286: case2:
287: m =2;
288: break;
289: case 3:
290: m =2;
291: break;
292: case4:
293: m =5;
294: break;
295: case5:
10. 10 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
296: m =0;
297: break;
298: case6:
299: m =3;
300: break;
301: case7:
302: m =5;
303: break;
304: case8:
305: m =1;
306: break;
307: case9:
308: m =4;
309: break;
310: case10:
311: m =6;
312: break;
313: case11:
314: m =2;
315: break;
316: case12:
317: m =4;
318: break;
319: default:
320: cout <<"Unknown month! n";
321: goto again;
322: }
323: cout <<"year number = "; cin >> year;
324: error(year);
325: day_of_week =( day + m + year +(year/4)+ century )%7;
326: switch(day_of_week)
327: {
328: case 6:
329: cout <<"the day was a Saturday n";
330: break;
331: case0:
332: cout <<"the day was a Sunday n";
333: break;
11. 11 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
334: case1:
335: cout <<"the day was a Monday n";
336: break;
337: case2:
338: cout <<"the day was a Tuesday n";
339: break;
340: case3:
341: cout <<"the day was a Wednesday n";
342: break;
343: case4:
344: cout <<"the day was a Thursday n";
345: break;
346: case5:
347: cout <<"the day was a Friday n";
348: break;
349: default:
350: cout <<"ERROR!! n";
351: }
352: again:
353: cout <<"Do you want to exit the programme (y/n)? : ";
354: cin >> ask;
355: }while(ask == 'n');
356: }
357: void error(int &x)
358: {
359: while (cin.fail())
360: {
361: cin.clear();// clear input buffer to restore cin to a usable state
362: cin.ignore(INT_MAX, 'n');// ignore last input
363: cout <<"You can only enter numbers.n";
364: cout <<"Enter a number : ";
365: cin >> x;
366: }
367: }
368: void line()
369: {
370: for(int j=0; j<45; j++)
371: cout << '=';
12. 12 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
372: cout << endl;
373: }
374: void factorial()
375: {
376: int c;
377: char ask;
378: do
379: {
380: long double j=1;
381: cout <<"Enter factorial number: ";
382: cin >> c;
383: error(c);
384: for( c ; c>0; c--)
385: {
386: j=j*c;
387: }
388: cout <<"!number = "<< j << endl;
389: cout <<"Do you want to exit the programme (y/n)? : ";
390: cin >> ask;
391: }while(ask == 'n');
392: }
13. 13 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y
14. 14 | P a g e C A N A L H I G H E R I N S T I T U T E O F E N G I N E E R I N G & T E C H N O L O G Y