अगर आप Find Greatest of Three Numbers in C Program को सीखना चाहते हैं, तो आप बिल्कुल सही जगह पर आए हैं। यह C programming के सबसे basic और important concepts में से एक है, जो हर beginner को जरूर सीखना चाहिए। इस program के जरिए आप if-else logic, comparison operators, और user input को handle करना सीखते हैं।
इस blog post में हम एक simple लेकिन practical C program देखेंगे जो user से तीन numbers input लेता है और उनमें से सबसे बड़ा number find करके display करता है। Program के साथ-साथ हम हर line का step-by-step explanation भी करेंगे ताकि आपको पूरी logic clearly समझ आए।
चाहे आप अपनी exam की तैयारी कर रहे हों, college assignment complete करनी हो, या simply C programming सीखना चाहते हों — यह guide आपके लिए complete reference की तरह काम करेगी। Program code, output, explanation, और FAQs — सब कुछ एक ही जगह मिलेगा।
Table of Contents
Find Greatest of Three Numbers in C — पूरी जानकारी
C language में numbers को compare करना एक foundational skill है। जब हम find greatest of three numbers in C program की बात करते हैं, तो हम basically सीखते हैं कि कैसे if-else conditions का use करके logic build किया जाता है। यह concept आगे चलकर sorting algorithms, data processing, और decision-making programs में काम आता है।
Program Statement
C में एक program लिखिए जो user से तीन integer numbers input ले और उनमें से greatest (सबसे बड़ा) number display करे।
Conditions:
- अगर तीनों numbers equal हों → “All numbers are equal” display करो
- अगर पहला number सबसे बड़ा हो → पहला number display करो
- अगर दूसरा number सबसे बड़ा हो → दूसरा number display करो
- अगर तीसरा number सबसे बड़ा हो → तीसरा number display करो
Program Code
#include <stdio.h>
int main() {
int a, b, c;
// Take three numbers as input
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Check whether all three numbers are equal
if (a == b && b == c) {
printf("\nAll three numbers are equal.\n");
}
// Check whether 'a' is the greatest
else if (a >= b && a >= c) {
printf("\nGreatest number is: %d\n", a);
}
// Check whether 'b' is the greatest
else if (b >= a && b >= c) {
printf("\nGreatest number is: %d\n", b);
}
// Otherwise, 'c' is the greatest
else {
printf("\nGreatest number is: %d\n", c);
}
return 0;
}
Program Output
Step-by-Step Program Explanation
अब हम इस program को line-by-line समझते हैं ताकि find greatest of three numbers in C की पूरी logic आपको crystal clear हो जाए।
1. Header File
#include <stdio.h>
- यह Standard Input-Output library है।
- printf() और scanf() functions इसी library से आते हैं।
- इसके बिना input/output operations possible नहीं होते।
2. Variable Declaration
int a, b, c;
- तीन integer variables declare किए गए हैं — a, b, और c।
- यह तीनों variables user द्वारा दिए गए तीन numbers store करेंगे।
- int data type इसलिए use किया क्योंकि हम whole numbers (integers) के साथ काम कर रहे हैं।
3. User Input लेना
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);- printf() → screen पर message display करता है, जो user को बताता है कि input क्या देनी है।
- scanf() → user द्वारा type किए गए तीनों numbers को एक साथ read करता है।
- %d → integer format specifier है।
- &a, &b, &c → इन variables के memory addresses हैं जहाँ values store होती हैं।
4. Equal Numbers Check
if (a == b && b == c)
{
printf("\nAll three numbers are equal.");
}- सबसे पहले यह check किया जाता है कि क्या तीनों numbers equal हैं।
- == operator से values compare होती हैं।
- && (Logical AND) का मतलब है — दोनों conditions एक साथ true होनी चाहिए।
- अगर a == b और b == c दोनों true हैं, तो automatically a == c भी true होगा।
5. First Number Greatest Check
else if (a >= b && a >= c)
{
printf("\nGreatest number is: %d", a);
}अगर a दोनों b और c से greater than या equal है, तो a सबसे बड़ा है।
>= operator greater than or equal to check करता है।
यहाँ && ensure करता है कि a का दोनों से comparison एक साथ true हो।
6. Second Number Greatest Check
else if (b >= a && b >= c)
{
printf("\nGreatest number is: %d", b);
}- अगर b दोनों a और c से बड़ा या equal है, तो b greatest number है।
- यह condition तभी check होती है जब ऊपर वाली conditions false हों।
7. Third Number Greatest (Default Case)
else
{
printf("\nGreatest number is: %d", c);
}- अगर ऊपर की कोई भी condition true नहीं हुई, तो c automatically greatest होगा।
- यह else block default case की तरह काम करता है।
Alternative Method — Nested If-Else से Find Greatest of Three Numbers in C
#include <stdio.h>
int main() {
int a, b, c, greatest;
// Take three numbers as input
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Check whether all three numbers are equal
if (a == b && b == c) {
printf("All three numbers are equal.\n");
}
else {
// Compare a with b and c
if (a > b) {
if (a > c)
greatest = a;
else
greatest = c;
}
// Compare b with c
else {
if (b > c)
greatest = b;
else
greatest = c;
}
printf("Greatest number is: %d\n", greatest);
}
return 0;
}
यह nested if-else approach भी उतना ही effective है। इसमें पहले a और b को compare करते हैं, फिर winner को c से compare करते हैं।
Ternary Operator से Greatest Number Find करना
C में एक और elegant तरीका है — ternary operator का use:
#include <stdio.h>
int main() {
int a, b, c, greatest;
// Take three numbers as input
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Find the greatest number using nested ternary operators
greatest = (a > b)
? ((a > c) ? a : c)
: ((b > c) ? b : c);
// Display the greatest number
printf("\nGreatest number is: %d\n", greatest);
return 0;
}
- ? : ternary operator एक short-form if-else की तरह काम करता है।
- यह method code को compact बनाती है लेकिन beginners के लिए थोड़ी complex लग सकती है।
Common Mistakes जो Beginners करते हैं
जब students find greatest of three numbers in C program लिखते हैं, तो कुछ common errors होती हैं:
- = और == का confusion — = assignment है, == comparison। गलत use करने पर logic fail हो जाती है।
- & को scanf में भूलना — scanf(“%d”, a) की जगह scanf(“%d”, &a) लिखना जरूरी है, नहीं तो program crash हो सकता है।
- Float numbers handle न करना — अगर decimal numbers compare करने हों तो int की जगह float use करें और %f format specifier।
- सिर्फ > use करना >= की जगह — Equal numbers के case में wrong output आ सकता है।
Real-Life Applications
- Find greatest of three numbers in C सिर्फ एक academic exercise नहीं है। इसके practical uses हैं:
- Student grade comparison — तीन subjects में highest marks find करना।
- Sales data analysis — तीन months में best sales figure identify करना।
- Game scoring — तीन players में winner determine करना।
- Temperature monitoring — दिन के तीन readings में peak temperature find करना।
Conclusion ( निष्कर्ष )
इस blog में हमने Find Greatest of Three Numbers in C को तीन अलग-अलग approaches — simple if-else, nested if-else, और ternary operator — के साथ detail में समझा। साथ ही हमने program की हर line का explanation, common mistakes, और real-life applications भी cover किए। यह concept C programming की नींव है और इसे अच्छी तरह समझना आपको आगे के complex programs के लिए तैयार करता है।
अब आपकी बारी है — program को अपने system पर run करें, अलग-अलग inputs से test करें, और खुद से modify करके देखें। अगर कोई doubt हो या आप किसी और C programming topic पर guide चाहते हैं, तो नीचे comment करें। हमारे और C programming tutorials पढ़ने के लिए blog को bookmark करना न भूलें!
