Hello readers!!!
Today we are going learn about doing some intelligent task with C. Excited??? Okay….. Let’s solve a problem.
Problem:
You will be given three variables assigning three different numbers. Now you will have to pick the largest variable by the value among them.
Example:
You will be given A, B and C as three integer variables. Now you will have to pick A or B or C which will contain the largest integer number among them.
Sample input:
A = 12
B = 20
C = 10
OUTPUT:
Largest number is B
So, Now how did we get the largest number among A, B and C? Let’s think about the process. It’s so simple just we will have to think it with our mind and brain.
First of all forget about the number of variables. Just pick a variable. Let’s pick A. Then we will have to pick another variable. We can pick anyone from rest of them. Here B and C are the remaining variable. But you may have to get the largest number among 5 to 6 variables. So, we are picking B as the second variable. Now compare with A. If the value of A is greater than B then pick A for the next step and erase the B else if B is the greater then pick B and erase A from the list. Now which variable is picked it will be compared with the rest of the remaining variables. By this method you will end up with only two variables one will be the greater between them and this greater variable is the greatest variable in the given list. So, now we will see a flow chart of three variables that will compare and find the largest number among them.
So, we have the flow chart and we know the algorithm now we will write the code. Always remember one thing, If you know the algorithm and can draw the flow chart then the coding is too much easy. Here the code is given below;
#include<stdio.h> int main(){ int A, B, C; printf("Enter value for A:"); scanf("%d",&A); printf("Enter value for B: "); scanf("%d", &B); printf("Enter value for C: "); scanf("%d", &C); if(A>B){ if(A>C){ printf("A is the largest number"); }else{ printf("C is the largest number"); } }else{ if(B>C){ printf("B is the largest number"); }else{ printf("C is the largest number"); } } }
Okay Cool…..!!!!
Now dear friends, copy the code and paste it into your compiler then run and enjoy………
One thing for you friends, if you face any difficulty in my blogs please comment immediately and if you find these posts are important then share with your friend zone. Remember “Sharing means Caring”.
I have posted another blog to understand if-else more clearly. Have a look.
Thank you……. Happy coding.
1 thought on “Let us dive into C Part-6 Decision Making (if else)”