C Programming Tutorial - 42: Finding Prime Numbers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to my channel this is the 40 second tutorial in this course and in this tutorial we are going to make a program that's going to generate prime numbers between T 100 we're going to use nested loops in this program so if you guys all know what nested loops are if you want to know more about nest looks and you can watch the tutorial on nested loops in this course I guess it's the 39 tutorial so you can watch that and you would have your idea of how to use nested loops and even if you haven't seen that you told you can still watch this tutorial and I'm sure you'll be able to understand because nasa loops are really simple they're just loops within loops and you know the syntax is pretty simple so you know you can continue watching this tutorial and maybe watch that one later on so as you can see using code blogs I've saved a file I'd give me the name prime it has the extension dot C on line 1 in this file I have my header file and I do have main function the lines three and four I have my curly braces between the braces I'm going to put in the code for the program now what is a prime number a prime number is a number that is divisible by itself and 1 and it does not have any factor between itself and one and what I mean by that is seven is a prime number right because 7 is divisible by itself and 1 and it's not divisible by any number between one in itself like two three four five six none of these numbers would divide seven right and 11 is a prime number and 13 is a prime number so on and so forth you know all the numbers that are divisible by just themselves and one are prime numbers and we're going to make a program to find such numbers between 2 100 and we're going to use two loops both are going to be for loops and the outer loop is going to run through the numbers to 200 and the inner loop is only checked whether each number between 200 is a prime number or not right so 4 to 2 loops I'll need to control variables and I'm going to call my control variables inj and both are going to be integers so to declare them I'd have to type an int space I comma J and as to terminate this statement on the next line I'm going to start my outer for loop statement and I'll have to give I initial value to and I'll have to have a test condition I less than equal to hundred right because we're going to loop through 200 so that's why the initial value of I has to be two and the final value of Phi has to be hundred we're going to increment I in steps of one and in the body of this for loop we're going to have another loop and this one's going to use J as the control variable and what this loop is going to do is we're going to use J and check whether I is divisible by J or not and we're not going to use just one value of J we're going to use all values of J between 2 and I right so we're going to check whether I has any factors between the number 2 and itself so understandably the initial value of J is going to be 2 and we're going to loop through J from TD i right and of course we're going to increment J in steps of 1 as well and in the body of this loop we are going to use if you check whether I percentage J equals 0 or not and what we're doing through this statement is we are checking whether I when / J leaves a remainder of 0 or not and if that is the case then J perfectly the by its I if that's not the case then it doesn't right and if this is the case then we're going to break right because what this means is we found a factor for I and we don't have to check for any more values of J and we can simply break out of the inner for loop and remember this brain state is going to just get us out of the inner for loop it's not going to get us out of the outer for loop right and outside the body of the inner for loop we're going to have another if statement which is gonna check if the value of I is equal to J or not and the reason why we're doing this is because we're looping for the control variable J from to do I write so this if statement put to value even if j is equal to i right because the final value of j can be i and if that is the case then it means that we haven't been able to find a factor between the number two through the number i apart from the number i itself and what that means is that i is a prime number right so i hope this is not getting too complicated when you just slow it down and try to understand copy the code write it down on a piece of paper or you know you can pause the video after the program is complete just copy the code in your code blocks or other IDs if you using something else and you can execute the program see the output I'm sure you'll be able to understand so if is a prime number then we have to display it on screen so we're going to do that by typing percentage D the format specifier to print the value of I is a prime number and of course we're going to put in the newline escape sequence at the end and we don't have to give a space off the full stock and comma I right so this is going to print the value of I on screen for a semicolon to terminate the state in it with this our programs ready so when I click on build and run I see that this is the output I get to is a prime number three is a prime number 5 7 11 13 17 19 23 29 31 37 49 so all these numbers are prime numbers and you can check for yourself as I said 7 and 11 are prime numbers we spoke about them and all of these numbers are prime numbers in fact right so the logic of this program should not be too difficult to understand and the final prime number that you have between 2 100 is 97 and of course 98 9 9 are not fine numbers and 100 is divisible by 10 right so that's it for now in the next tutorial we are going to talk about something interesting again thank you so much for watching this tutorial and please subscribe to my channel in case you haven't already and I'll see you soon
Info
Channel: The Bad Tutorials
Views: 219,461
Rating: 4.6905804 out of 5
Keywords: C (programming Language), nested loops, Division, For Loop, Arithmetic Operations, prime numbers program, c programmming tutorial, And Operator, Computer, C Tutorial For Beginners, Case Statement, Addition, Else Statement, Arithmetic Operators, C Programming, computer programming tutorial, Nested If Else, Multiplication, Floats, prime numbers, c++ programming tutorial, Ifs, Associativity Of Operators
Id: l0vQS3gm8nQ
Channel Id: undefined
Length: 6min 32sec (392 seconds)
Published: Tue May 07 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.