Greatest Number Finder in C++ _ Example Program
44K views
Feb 7, 2024
This video explores the concept of finding the greatest number in a set of values using the C++ programming language. The tutorial provides a step-by-step demonstration of writing an example program that efficiently identifies and outputs the largest number among a given set. Viewers will gain insights into key programming techniques and logic involved in creating a robust "Greatest Number Finder" algorithm in C++. Whether you're a beginner looking to enhance your programming skills or an intermediate developer seeking a practical example, this video serves as a valuable resource for mastering the process of finding the maximum value in a collection of numbers using C++.
View Video Transcript
0:00
Hi guys, welcome back to the new video on our channel
0:15
In this one, I am going to create a program in C++ language which is going to find out
0:21
the greatest one among 4 different numbers. So with that said, let's get started
0:29
So first of all, I am simply going to include some libraries, so hash include iostream
0:38
the one we should be including every time, right? And then I am going to use namespace, std right up here, and now I am just going to
0:49
simply create our main function here, integer man, and then we are going to create body
0:55
for it. So now, here is the deal. First of all, I simply have to initialize 4 different variables with different values
1:05
That shouldn't be same because we will be having different numbers and then we will
1:10
be finding out the greatest one among them. So let's get started
1:16
I want to name the first variable as num1. Integer num1 equals to suppose 5
1:25
Integer num2 equals to 10. And the third one is integer num3 equals to suppose 100
1:34
And we have the fourth one, integer num4 equals to suppose 0, because that's also an integer
1:42
Now here, I am going to use FL statements here, right? And through those conditional statements, I can simply determine the greatest number
1:54
You always can change these values. It will never matter. So let's get started
2:01
I am going to use if statement here. So if num1 is greater than num2, if the first number is greater than the second one, n
2:13
then I will be using n logical operator here. If num1 is also greater than num2 and num1 is also greater than num4, then that means
2:33
prompt the user that the greatest one is the greatest integer
2:47
And then just simply I have to pass that variable right up here, which should be num1
2:53
So what does it mean? I have simply declared or initialized four variables
2:59
They're having different values. Then I simply use if statement here, that if the first number is greater than the second
3:06
one and the first number is also greater than the third one, here I have to put num3, alright
3:14
If the first number is also greater than the third one and num1, I mean the first number
3:19
is also greater than the fourth one, then that means num1 is the greatest among all
3:25
of them. That is it. Now we're going to do the same process with the num2 here
3:33
Now I'm checking that if num2 is the greatest one. So I can simply copy this block of code and just paste it right up here
3:43
I have to change them into else if. So else if num2 is greater than num1, and num2 is greater than num3, and num2 is greater
3:59
than num4. So now we're checking the second number, if that's the greatest
4:04
If second number is greater than the first one and the second is also greater than the
4:09
third one, and also the second is if that is greater than the fourth one, then that
4:14
means num2 is the greatest number among all of them. So then I can simply copy this block of code again, and I'm going to do that for the third
4:24
number right now. So if num3 is greater than the first number, and if the third number is also greater than
4:33
the second one, and if the third number is also greater than the last one, I mean the
4:39
fourth one, then that means num3, which represents the third number, is the greatest here
4:46
And then I can simply use else statement here. Else, if none of these conditions evolve as to true, then that means fourth one is the greatest
5:00
So simply tell the user, let me copy this line here, and now I'll simply pass here num4
5:13
So that's it. So return zero. Let me put C over here
5:22
So that is it guys. I hope that this is clear. That is the code for determining the greatest number among four, among four different values
5:36
So now let's compile this program and see if we are getting any errors
5:40
If I have done any mistakes, the compiler is going to tell me
5:44
So let's compile the program. You can also use F9 for this purpose here
5:49
I'm using div C++ as of now because I get a lot of comments, people asking me that what
5:55
IDE am I really using. So I'm using div C++ and you can easily download it right from Google
6:03
So now I've simply compiled the program and as you can see down here, I've got zero errors
6:09
and zero warnings. So that means everything is going well. So let me run the program and see if that's really working
6:17
So as you can see right up on my screen that the greatest integer is 100, which is absolutely true
6:25
So as you can see, these four variables, we have different values and you can see with
6:31
your own eyes that the greatest one is the hundred, which is number three, which is stored
6:37
in the third variable, number three variable. So now let's change these values
6:43
Suppose I put 500 over here and now let me recompile the program and let's see if that's
6:51
really working. So now let me run it once more. As you can see on the screen that the greatest integer is now 500
7:01
That's it. You can also put negative numbers over here that shouldn't be considered as the greatest one
7:11
There should be no problem. So let me recompile it. We've got zero errors
7:14
Let me run it. And now as you can see right up on my screen that the greatest number is still 500
7:23
So that's how you can determine or you can find out the greatest one among four different
7:30
values, four different numbers. Now the question arises, can I also increase these values
7:36
Of course you can, but keep in mind that you will have to also increase the number of times
7:43
that you are using else if statements here. You will have to increase these lines again
7:48
You will have to increase else if statement each time you increase these values here
7:55
So that is it. I hope that this is clear and if you have got any questions concerning this program
8:02
you can always ask in comments and I'm going to reply you
8:07
And one more thing that if you have any problem anywhere in C++, you can always comment down below
8:15
And if you have any requests concerning some small programs, you can always ask for and
8:22
I'm going to create videos right for you. So that was all for now
8:26
Hopefully you guys enjoyed the video and thank you for taking your time to watching this
8:31
video and I will see you guys next time
#C & C++
#Programming