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 have congressional elections changed over time? | US government and civics | Khan Academy
How have congressional elections changed over time? Congressional elections used to be separate from the presidential elections. One of the great examples is in 1938. FDR, who we all look back and think of as a president who had such extraordinary power a…
My Coffee Company Is Going Broke
What’s up guys, it’s Graham here. So this is a video I’m certainly not happy about making, but since I started this channel with the sole purpose of being as open and transparent as possible, I think I owe it to you to explain what’s going on and bring yo…
House Hack: How to live FOR FREE by investing in multifamily real estate
What’s up you guys, it’s Graham here. So, as your real estate agent and real estate investor, I’m going to be sharing with you guys exactly how you can cover all of your housing expenses and essentially live for free without ever having to pay rent or com…
Crazy experiences while selling private jets!
When you’re selling a jet for a company, that company is either moving up to a bigger, newer jet, or the company’s having problems and they’re selling the jet and they’re getting out of the business of operating their own corporate jet. If it’s the latte…
Cara Delevingne Pulls Herself Across a Canyon | Running Wild With Bear Grylls
[music playing] OK, you’re good, Cara. You know the bit I said about gravity doing the first bit? Yeah. That’s wrong. You’re just going to have to muscle it out most of the way. Oh, no. Hopefully, I’ll get across before I get scared. That’s what I’m hop…
How I built a private jet in my office!
14 years ago, I had to come up with the idea of how to build the best showroom in the world. But the biggest issue was, what the hell do I put inside the window of this showroom? I had to make sure that people looking in from the window outside didn’t thi…