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
How Much Car You Can ACTUALLY Afford (By Salary)
What’s up, Graham? It’s guys here. So have you ever wondered how much money you need to make to afford a car like this or this or even this? Well, wonder no longer because today we’ll cover exactly how much money you need to make to afford the typical car…
30 Years After Chernobyl, Nature Is Thriving | National Geographic
The large reason why these animals seem to be persisting in high densities or a high abundance within the exclusion zone is because of the absence of humans. It’s absolutely normal. As you drive around the exclusion zone, you’re overcome by all the lush n…
15 Ways to Buy Back Your Freedom
Freedom is more than money, but money definitely contributes to you buying your freedom. The truth is, most people get it twisted: money doesn’t buy happiness, but neither does being broke. If you know how to use it, money buys freedom, and freedom gives …
Negative frequency
I want to talk a little bit about one of the quirkier ideas in signal processing, and that’s the idea of negative frequency. This is a phrase that may not initially make any sense at all. What does it mean to be a negative frequency? Could there be a sine…
2004 Berkshire Hathaway Annual Meeting (Full Version)
Thank you. Good morning. Some of you may have noticed a stuntman was used in that. Arnold just couldn’t handle some of those scenes. Before we get started, I’d especially like to thank Andy Hayward, who’s here today, and if we can, no way we can find him …
The BENEFITS of IGNORING People | STOICISM
…this chaos. It provides us with tools to navigate through the noise and distractions, allowing us to reclaim our focus and purpose. By concentrating on our values and the aspects of life that resonate with our true selves, we cultivate a clarity that shi…