Hello and welcome back to this practical module….. In this module we will be seeing different problem-solving questions one by one and we are making respective C programs for them.
So exactly of the same type, we have a new question over here which is prime number check.
Now we have all studied mathematics and we know that in maths there are different types of numbers and we can characterise those numbers in different categories, that can be in odd and even category or the category of prime and composite number So, the concept of prime and composite number, we all know about them.
Once we will quickly discuss what exactly are these prime numbers and what exactly are the composite numbers.
So, Prime numbers are those numbers which basically only divided by one or by themselves.
Suppose we have 5, 5 is one number and we want to see that with which numbers it can be divided.
So, five is one prime number.
And how do we know that because 5 can only be divided by 1 completely, as well as 5 can be divided by itself completely.
Apart from these, 5 cannot be divided by any other numbers completely, that's why we call five as a prime number.
So, the prime number concept is that the number should be divisible by only 1 and by the number itself.
So, the prime number can be divided by 1 and by itself.
All the numbers can also be divided but it will not completely divide that number and that number we call as the prime number.
So, the opposite of the prime, which means those numbers, apart from these two numbers, there are other numbers that can divide them, we call those as composite numbers.
So, this was about the concept that what is the concept of the prime number and what is the concept of the composite number.
Now we will talk about our today's problem question.
So, this question is prime number check.
What will happen in this is our user, who is going to use our program, he will give us one number and we have to tell with the help of our C program that the number that the user has entered that it is prime or composite.
Now to categorise prime or composite.
We also have to develop its logic that how our program will get to know that that number is prime or the number is composite.
For this we will go and see one logic, if we quickly talk about this logic, you all know that 5 is one prime number, 7 is a prime number, but as and when we go further in the counting, so we start calculating a little whether it is a prime number or a composite number.
So, we'll take one example let's say we have taken an example of number 7, how we will check whether seven is a prime number.
Let's see that once over here.
So, to check number 7, we will, one by one start from 1 and by starting from 1 we will check that, number 7 is being divided by which all numbers.
1 and itself, which are the numbers it can be divided by, if it's not getting divided by any other number, then definitely it is prime.
But if it is getting divided completely then basically, we will say here that it is a composite number.
So, we will go and start with 1.
If we do 7/1, we get a chance to completely divide and 7/1 comes as 7 itself.
In the same way if we increase the number instead of 1, if we use 2.
If we do 7/2, 2 will not completely divide 7.
2X3 is 6, so the answer will be three point something but it will not completely divide, 7 with 2.
Okay, no problem now we will go ahead.
3 comes ahead, if 7/3 if we try then here, we can see that it will not completely divide.
It will be 3X2=6.
And here two point something will be the answer.
But it will not be completely divided 3 to 7.
So, there is no problem even here we will go ahead and see our next number.
We have seen 2, 3 and now we will come on to 4, as soon as you will see 4, what is 4X2, 4X1 is 4, which is very smaller than 7.
If I try to do 4X2, it becomes greater than 7.
So, here 4X2 is 8, which is greater than 7.
So here definitely it is not going to completely divide number 7.
In the same way if we go to 5, even 5, single 5, that is if we do 5X1=5 if we do, even that will not divide 7.
In the same way, if we do 5X2 is 10, which is quite greater than 7, so even 5 is not dividing 7 over here, even 6 becomes greater in the same way and the answer will start coming in zero points.
So, in this way, the answers that are coming.
If you see that the possibility of getting the answer that can completely divide the number, it has been ended at four which means that how it got over, we will see that, we know that one and the number itself will divide itself.
We will remove these possibilities and we will consider 2, with 2 we have divided and see, the answer was coming in three point, we tried dividing it by 3, two point something was the answer.
We try dividing it by 4, with 4 you will see that the possibility had reduced a lot.
Why? Because if we do seven 7/4, what comes 4/1.
So, 4X1=4 and after that a lot of numbers will start coming.
So, this basically would be completely incorrect.
And definitely the numbers ahead of four will not be able to divide 7.
That’s why to check the prime number, we have to make this logic.
But how we will start is we will start taking the numbers from two because we know that one will divide all the numbers, so we don't have to check it every now and then.
So, we will start with two.
And suppose whatever number we have taken, suppose we have taken n number, we will only go till n/2, we will try to divide.
Like we had taken 7, so 7/2 is 3.5.
So, we will round of and take 4 and then we will check 2, 3 and 4, which means for these 3 numbers we will divide and check.
Because the numbers which are there after 4 will never divide of 7 number.
In the same way.
You can take any nth number; it's division will be completed till when? Till the time we don't reach n/2.
Which means if we take 10 or 15, what will happen in case of 15, we will do 15/2, which will come as 7.5, we will round off 7.5 till 8 and from 2 till 8, we will divide and see 15 because a number greater than 8, will not be able to divide 15.
So, in this way we have to make the logic for the prime number.
How it will work on how its logic will work.
That we will see in our text editor.
So, for now we have understood that how we have to write the logic of prime number, how we have to check it whether this is a prime number or not.
So, to see this we will quickly go on to our text editor and we will write one program to check the prime number.
The first thing that is there is of the pre-processing command, that we definitely have to use and after that we will come on to our main function.
Now we have come into our main function and here the most important part is of making the variables, only then we will take the values in the variables and then only we'll be able to check whether those are prime or not.
So, that’s why what we will do here is we will be making the variables here.
Now these variables are n, i and flag.
We have made three different variables.
In this the n named variable which is of integer data type, in that we will be storing the number which we have to check whether it is prime or composite.
After that, if we talk about the i variable, i variable is used for, for loop.
So here for loop will also be used.
why? Because one by one we have to keep dividing.
First, we will be dividing it by two, then three, then four, then five.
In this way we have to go on increasing the numbers.
That's why there will also be the use of for loop definitely and to keep on increasing the for loop we will require i variable.
So, this was about n and i variable.
Now we will talk about the flag variable.
So, this flag variable story is somewhat in this way.
What we will do is we will define “flag=0”.
That in our flag variable, the value is 0.
And here we will test one condition which will be a completely divisible condition, whether any number can completely divide our n number and if it can devide we will change the flag’s value to 1 and at the end when the entire loop will get over.
At the end we will check the condition.
What is our flag’s value? Is it 0 or 1 because if it remains 0 and in any condition, if it doesn't happen that it completely divides and the flag’s value remains 0 till the end.
At that time, we can say that the number that you have given is a prime number.
But if the flag’s value becomes one because some number was completely divided.
At that time, we can say that it’s a composite number.
So, this is the story of our flag variable.
So, this is about our printf and scanf function.
So, here in n variable we have taken that positive integer, which we have to check whether it is prime or composite.
Now we will go ahead and see our for loop.
Now in for loop there are a few conditions over here, there are few initializations here, that we will be seeing.
We are starting the for loop where we are making use up i variable and like we had discussed before that we will be not divide our number by 1 and see.
Because we know that all the numbers are automatically divided by one, so there is no need to test it over here.
That's why we are initialising i with two and i here will be that number, which we will keep on increasing to divide, whether the i’s value is two before.
So, n number we will divide by two and see, then the i’s value will be 3, then n number will divide by 3.
In this way the eye will keep on increasing till when? The value will increase till n divided by 2 because we have discussed now.
Our number is suppose 7, so, 7/2 we do and 3.5 comes.
If it is rounded off it comes out to be 4.
Now this number 4, we know that, the further numbers cannot be completely divided our n number, that's why we will not be running those conditions over here.
We can definitely run it and see but there is no benefit over here.
Why? Because the number of iterations, number of repetitions of for loop will keep on increasing and the outputs will remain the same.
So, all these possibilities we will eliminate for now, in which we know for sure that one is already dividing and the number itself divides as well.
Even the number’s half will not completely divide it further.
So, we will eliminate all these conditions, we are making one for loop, in which the i’s value starts with 2 and it goes on till n/2 only.
So, with this the number of repetitions reduces and there is nothing much that happens over here.
Now we will go ahead and see one condition like we talked about that we are using a condition for the composite number.
And this condition is of this way that you can see here we have made use of % sign, we have made use of if condition and we have written here that n mod of i equal to equal to zero, this means that our n number, we are dividing it by with whatever is the i’s value, with that.
And since we have made use of mod operator over here, it will divide and not give us quotient in the output.
It will give us the remainder in the output, which is our mod function.
Here if the remainder comes as zero, it means that a number can be divided by some other third number.
So, at that time we can see that the flag’s value is made 1 as soon as this condition is satisfied.
Which means some third number is completely dividing it.
So, we will make flag’s value as 1 and we will break with the help of break statement, with our for loop we will break.
And after that suppose if the flag’s value has become 1.
Now after the flag’s value is 1.
We will check with the condition, if flag is 1, the number is composite, if the flag is 0, then the number is prime.
For that what we will do here is we will make use of if and else and in if else we are checking, if “n==1”, which means our n the number which we had passed, which user had passed, if the user has passed 1.
So, we all know that one is neither prime nor it is a composite.
So, in this way the n’s condition which will come, if the user writes 1 in n.
At that time, we have to print that that print, “1 is neither prime nor composite.” If n ==1, then we have to print this printf statement.
But if it is not there, if there is some other value then what will happen? Then we will come into the else part.
But even in that we will have to put one if statement.
Now why we have to put that, we will see over here.
Like we said here if the flag is zero that means that the number is prime.
But if the flag’s value is changed to 1 then definitely our number is composite.
That's why in the else statement also we have put if and else statement again, and we have checked that if flag ==0, then we have to put that number is a prime number.
Otherwise, it will print the else part which is the number is not a prime number.
So, this is the entire concept how we can check our prime number, whether it is prime or composite.
So, if it is composite, we can straight away say that this is not a prime number.
So, this was the entire logic of checking the prime number.
I hope you have all understood it, now we will run it once and see and see how its output comes.
So, you can see that there is some error that we have been told.
So, we will quickly see once what is the error exactly.
So, here basically there was some issue with the compiler because of which we were seeing the error over here.
As soon as we will restart our program.
Then there will be no problem over here.
So that's why we will run it again over here.
Now we will run it and see our program, how it is running.
Here we will write “gcc checkingprime.c”, as soon as we compile it over here, it will get successfully compiled and when we will try to run it, you can see that here there is some problem that is arising.
Once we will see what is the problem.
Now we will run it, as soon as we will run it you can see that here it is prompting us, enter a positive integer.
Now we have to enter a positive integer here, it can be 5,10, 20, 50, 100.
So, for now our know prime numbers, which we know, those we will test and see whether those are considered prime or not.
So, first of all, we will consider 7 number and as soon as we will press enter, you can see that we are getting printed here “7 is a prime number.” So, we will try it again and see some other number.
So now we will try 17.
As soon as we will enter 17, “17 is a prime number” is what we are getting printed over here.
Now, we will test a non-prime and to test the non-prime number we will enter 9, here as soon as we enter 9.
You can see here that we have got typed over here that “9 is not a prime number.” In the same way we'll test one more and see, that will be 25, we will test 25, so you can see that it is telling us 25 is not a prime number.
So, in this way, we can check the different numbers whether they are prime or not the prime number with the help of this logic and this was a very easy logic as it is even in your computer, you can run it and see how this functionality will work.
Apart from this you can design some other logic and see with the other logic also, we can check the number whether it is prime or not.
So, in the same way you can do this brainstorming part, definitely in your computer.
If there is some part related to this program or related to this topic, which you have not understood, you can definitely ask it on forums.learnvern.com and there you can post the question in any language.
We will reply you over there at the earliest in your own language, so that you can efficiently complete the practical of C programming.
Till that time, you keep on practising all these practical.
We will meet you in the next video.
Thank you so much.
Very happy 😄😁
Very happy and greatful, thank you , great course.
Super for poor students to learn software course languages TQ