इस अनुभाग में, हम देखेंगे कि तीन दिए गए मानों की तुलना करके उनमें से मध्य मान कैसे खोजा जाए। तो यदि तीन संख्याएं दी गई हैं जैसे (10, 30, 20), तो यह 20 ढूंढेगा क्योंकि यह मध्य तत्व है। आइए पहले एल्गोरिदम देखें, फिर हम उस एल्गोरिदम को C++ कोड में लागू करेंगे।
एल्गोरिदम
middle_of_three(a, b, c):
Input: Three numbers a, b and c
Output: The middle of these three
Begin
if a > b, then
if b > c, then
return b
else if a > c, then
return c
else
return a
else
if a > c, then
return a
else if b > c, then
return c
else
return b
End
उदाहरण
#include <iostream>
using namespace std;
int mid_three(int a, int b, int c) {
if (a > b) {
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
} else {
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
main() {
int a = 10, b = 30, c = 20;
cout << "Middle Out of Three "<< mid_three(a, b, c);
}
आउटपुट
Middle Out of Three 20