![]() |
Find the Greatest Number among Five Numbers [C Program] |
Hello Guys. Welcome to my Blog. This is my very first post here about C programming. Today I am gonna share a C programming Problem where we are asked to find the greatest integer among five numbers. I know we can do this in few lines and easily with Loops and arrays but here we are asked to do this by only using nested if else condition. I know we can use else if instead but here we can't use them, neither the && operator to combine the test cases also.As you can see the code will be too much Big as here is no looping. The fun of this program is this the test cases you need to implement here. I didn't mean that they are tough, but still it needs strong concentration while doing (at least took for me :P). So lets start the coding.!! Here I have made the code as simple as possible. If you have any problem with the code you can ask me in the comments.
What we are doing at first??
here first we create 5 variables to contain 5 numbers and one extra variable to contain the biggest number. we have named them a,b,c,d,e,big. First we ask the user to input 5 numbers and thereafter we scanf them regarding their corresponding variables.
Test Conditions !!
Here we first check whether a is greater than b. if yes then we again check whether a is grater than other present variables such as c,d,e. if yes then we assign the vale of a to big, otherwise we assign the value of e to it.
after that, in corresponding else case, we check whether c is greater then d as we already know a is less than c(opposite to the case in first part, a>c). Then we check whether c is greater than d and e. if yes then we assign c to big and if not then e to big. In the else part of c>d, in other word c<d, we check whether the d is greater than e or not. if yes assign the value of d to big, or of e in No condition.In the else part of the first if (a>b), we check if b is greater than rest. if yes then assign the value of b to big, otherwise of e to big.In the else part of b>d, in other word in c>d, we check the greater value among them and assign the greatest to big(same logic).
At last we are left with only one test case,probably the smallest and easiest one. if d>e. if Yes then big=d otherwise big=e.
That's it. We are done. You can run the following code with your own 5 numbers and can check the accuracy of the code. Hope I have made the logic much simple. if you have any doubt about it then don't forget to ask me. Thank you !!
/*Find the Greatest Number among 5 Numbers C Programming Code by ©Anuran Barman.2015.All Rights Reserved.*/ #include<stdio.h> #include<conio.h> int main(){ int a,b,c,d,e,big; printf("Enter 5 Numbers: "); scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); if(a>b){ if(a>c){ if(a>d){ if(a>e){ big=a; } else{ big=e; } } } else{ // a<c if(c>d){ if(c>e){ big=c; } else{ big=e; } } else{ if(d>e){ big=d; } else{ big=e; } } } } else { if(b>c){ if(b>d){ if(b>e){ big=b; } else{ big=e; } } else{ if(c>d){ if(c>e){ big=c; } else{ big=e; } } else{ if(d>e){ big=d; } else{ big=e; } } } } } printf("The greatest number is: %d",big); printf("\n"); getch(); return 0; }
0 comments