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
Schelling Point: Cooperating Without Communicating
Let’s talk about the shelling point. Shelling point is a game theory concept made famous by Thomas Schelling in the book called “Strategy of Conflict,” which I do recommend reading. It’s about multiplayer games where other people are responding based on w…
From TV Repairman to Artist, One Man Makes Art Out of Parts | Short Film Showcase
[Music] I saw a video once, and it showed the house of the future: TV set in the refrigerator, TV set in the counter, TV set everywhere. You know, controls for this, for that. There I thought, oh, this is funny. You’d have to have another room in the hous…
Can Money Buy Happiness? Yes, According to Philosophy & Science
Some people claim that money is the root of all evil, pointing at the enormous amounts of violence humanity imposes on itself motivated by acquiring it. Others argue that not money but the lack of money is the root of evil, as people, out of fear of being…
Why Silence is Power | Priceless Benefits of Being Silent
“All profound things and emotion of things are proceeded and attended by silence.” Herman Melville. In Western cultures, silence is commonly used as a means to show respect and recollect. One example prominent in Dutch culture is the Silent March; a ritu…
15 Costliest Mistakes Billionaires (and YOU!) Make
Billionaires, they’re actually just like you. You’re one successful adventure away from claiming it, and they are one big mistake away from losing everything. We all make the same mistakes, but the bigger your bank account, the harder your fall. So, you s…
From the Ashes - Official Film Trailer | National Geographic
[Music] Ralph Waldo Emerson wrote, “Coal is civilization and power.” From the early 1900s to World War II, coal powered America. We’re still quite dependent on coal, but coal was a nineteenth-century source of fuel, and we’re in the 21st century. The tow…