Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
C++ program: Numbers .cpp
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 3:
//numbers.cpp
C++ program: ordering numbers:
even, odd & prime
(desending-ascending) order
Prepared By:
Khalid Waleed
CANAL HIGH 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: // numbers.cpp
2: // program for ordering numbers: even,odd & prime
3: #include <iostream>
4: usingnamespace std;
5: void error(int&);
6: int main()
7: {
8: int process , numb , f=0;
9: char ask;
10: do{
11: cout <<" 1- ordering even numbers n 2- ordering odd numbers
12: n 3- ordering prime numbers";
13: cout <<"ninsert number of process : ";
14: cin >> process;
15: error(process);
16: switch(process)
17: {
18: case1:
19: cout <<"n 4- desending order n 5- ascending order ";
20: cout <<"ninsert number of process : ";
21: cin >> process;
22: error(process);
23: switch(process)
24: {
25: case4:
26: cout <<"Enter your number : ";
27: cin >> numb;
28: error(numb);
29: for(numb ; numb>0;--numb)
30: {
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
31: if(numb%2==0)
32: {
33: cout << numb <<", ";
34: }
35: }
36: break;
37: case5:
38: cout <<"Enter your number : ";
39: cin >> numb;
40: error(numb);
41: for(int c=1; numb>=c ; c++)
42: {
43: if(c%2==0)
44: {
45: cout << c <<", ";
46: }
47: }
48: break;
49: default:
50: cout <<"number of process unknown !";
51: }
52: break;
53: case2:
54: cout <<"n 6- desending order n 7- ascending order ";
55: cout <<"ninsert number of process : ";
56: cin >> process;
57: error(process);
58: switch(process)
59: {
60: case6:
61: cout <<"Enter your number : ";
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
62: cin >> numb;
63: error(numb);
64: for(numb ; numb>0;--numb)
65: {
66: if(numb%2!=0)
67: {
68: cout << numb <<", ";
69: }
70: }
71: break;
72: case7:
73: cout <<"Enter your number : ";
74: cin >> numb;
75: error(numb);
76: for(int c=1; numb>=c ; c++)
77: {
78: if(c%2!=0)
79: {
80: cout << c <<", ";
81: }
82: }
83: break;
84: default:
85: cout <<"number of process unknown !";
86: }
87: break;
88: case3:
89: cout <<"n 8- desending order n 9- ascending order ";
90: cout <<"ninsert number of process : ";
91: cin >> process;
92: error(process);
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
93: switch(process)
94: {
95: case8:
96: cout <<"Enter your number : ";
97: cin >> numb;
98: error(numb);
99: for(numb ; numb>=2;--numb)
100: {
101: f=0;
102: for(int j=2; j<=numb/2; j++)
103: {
104: if(numb%j ==0)
105: {
106: f=1;
107: break;
108: }
109: }
110: if(f==0)
111: cout << numb <<", ";
112: }
113: break;
114: case9:
115: cout <<"Enter your number : ";
116: cin >> numb;
117: error(numb);
118: for(int c=2; numb>=c ; c++)
119: {
120: f=0;
121: for(int j=2; j<=c/2; j++)
122: {
123: if(c % j ==0)
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
124: {
125: f=1;
126: break;
127: }
128: }
129: if(f==0)
130: cout << c <<", ";
131: }
132: break;
133: default:
134: cout <<"number of process unknown !"; 135:
136: }
137: break;
138: default:
139: cout <<"number of process unknown !";
140: }
141: cout <<"ndo another (y/n) ? ";
142: cin >> ask;
143: }while(ask == 'y');
144: return0;
145: }
146: void error(int&x)
147: {
148: while(cin.fail())
149: {
150: cin.clear();// clear input buffer to restore cin to a usable state
151: cin.ignore(INT_MAX, 'n');// ignore last input
152: cout <<"You can only enter numbers.n";
153: cout <<"Enter a number : ";
154: cin >> x;
155: }
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
156: }