yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 7 | Making Prime Number Program


4m read
·Nov 3, 2024

Hello, this is a special guest Jake with Mack Heads 101. It's going to be another Java tutorial, and right now I'm just going to be building a program with you guys. There's not really going to be that much new stuff, maybe one or two new things, but they won't really require that much explanation.

The main point of this is just to sort of sit back and build a program with you. So what this good program is going to do is you enter a number, and it tells you whether or not it's prime. In case you know what prime means, it means it's only evenly divisible by 1 and itself, which means it is not evenly divisible by any other number, of course, besides 1 and itself.

So the first thing we're going to do is already got here Java.util, so we can use the Scanner. We're going to just get in some input from them. Equals, and of course, I've gone over this before, so if you don't know what I'm doing, just watch the past tutorials.

Vanish and equals, oh yeah, equals new Scanner, and then I'm just going to put out a prompt: "Enter a number." Alright? And then I'm going to make an int input equal scan.nextInt.

Alright, so it's going to ask you to enter a number. Whatever they enter is going to be stored in that variable input.

So now, how we're going to determine whether or not it's prime is we're going to use a loop and something called the modulus operator. So I'm going to build the whole thing first, and then I'm going to explain how it works because I think that's the best way to show.

So for, I'll just write int I equals 2, and it's important that it equals 2. I is less than input, I plus plus, and then it's going to have an if statement in here.

So what this does is, um, I will at some point equal every positive number between, um, 2 and the number right before it. So it'll basically equal everything, um, but besides 1 and itself.

And so if, let's say the number is 5, it'll be 2, 3, 4. Alright? Let's say you don't, at some point before the loop breaks, I will once be 2, once be 3, and once before.

So here's the important part on the modulus operator. What the modulus operator does is, like adding, you know, the plus sign will add two. Well, if you do something modulus something, what it returns is the remainder of them when divided.

So like, let's say you have 17 modulus 2, it would equal 1 because 17 divided by 2 is 8 with a remainder of 1. So if input modulus I equals equals zero, we're going to do something.

And so if anything modulus anything equals zero, there's no remainder, which means it's evenly divisible. So it's going to try to divide it by anything besides one and itself, and if at any point it's evenly divisible by something, we're going to do this.

I'm going to make something called a boolean, which is the only new thing besides the modulus that I'm going to mention here, which is a variable that can be set to either true or false. It can only be set to two things: boolean prime.

And so if input modulus prime equals false, which means if at any point it's divisible by something like three, our prime equals false; it's not prime. And otherwise, we're going to just say prime equals true, which means if it's not proven false, it's if it's never, um, what's it called.

If it never gets set to false, it also will be true. It's kind of like a guilty till proven innocent kind of thing. Um, and then I'm just going to make this if statement: if prime equals equals false, then system.out.println(input + " is not prime"); I'm going to put a space so it’s not smushed together like that.

Otherwise, if prime equals equals true, system.out.println(input + " is prime."); Alright?

So I'm just going to run over what this did one more time. We got the input from the user and prompted them to assign. We got the input that became input. We have this boolean prime, and it starts off as true.

Then we're going to make this loop to see if we should set it equal to false. It starts at 2, which means it doesn't include 1 and stops right before it gets to the number because if I is less than input.

So let's say it's 17. Um, when it loops, I will be 2, then 3, 4, 5, 6, 7, 8, 9, 10, all the way up to 16. So if at any point input modulus that equals 0, which means it's evenly divisible, prime equals false because if it's evenly divisible by one of those numbers, it is not prime.

If prime is false, it prints out whatever the answer is: "not prime." If prime is true, it prints out whatever they entered: "is prime."

So I'm just going to run this and prove it to you. Enter number: I'll enter, um, 19, which is prime. I know it's prime. And I'll enter 20, which is not prime. 20 is not prime.

And of course, I know if you're going to be entering something like 1 or 2, it'll probably mess things up. Um, that's why you could just make some if statements like if input equals 1, then you're just going to print out: "1 is not prime." And if input equals 2, just print out: "2 is prime."

You don't have to do it for those two numbers because it would just be funky with this. So, um, yeah, that's just a prime number program. Just, you know, sit back and build the program with you guys. So I'll see ya.

More Articles

View All
Khan Academy for Texas Administrators Webinar 7.18.2024
Hello everyone! Welcome! Thank you for joining. We are going to get started in about 10 seconds. There are a lot of people pouring into the room, so you are here to see what Khan Academy has done to support Texas teachers. We’re so excited to be addressin…
Great Schism part 1
In previous videos, we talked about the dramatic turnaround in the 4th century in terms of how Christianity was treated in the Roman Empire. As you enter into the 4th century, it’s persecuted by Diocletian, but then Constantine takes power. He’s sympathet…
Mr. Freeman, part 08 — "Оскорблять меня весело и безопасно!"
white noise Hello there! My name is Mr. Freeman and I have a small mental dick. I’m black & white, like bird shit, because it’s cheaper! I’m screaming like a tantrum. I’m Captain Obvious with di… …Napoleon complex! I am scum and noobs cheating! I’m mi…
Watch: Camera Put on Giant Manta Ray for First Time Ever | National Geographic
Although manta rays are a really iconic species that have a really high value in tourism, they’re also targeted all around the world in fisheries. But they’re also incredibly susceptible to bycatch. So, Critter cam is going to play a huge role in being a…
Negative definite integrals | Integration and accumulation of change | AP Calculus AB | Khan Academy
We’ve already thought about what a definite integral means. If I’m taking the definite integral from ( a ) to ( b ) of ( f(x) \, dx ), I can just view that as the area below my function ( f ). So, if this is my y-axis, this is my x-axis, and ( y ) is equ…
Confessions of a Tomb Robber | Lost Tombs of the Pyramids
Dr. Colleen Darnell hunts for clues to solve the mystery of why dozens of pharaohs were removed from their original tombs and reburied in an unmarked grave. [Music] Could this ancient papyrus hold the answer? One of the more remarkable documents to surv…