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 I Boarded a US NAVY NUCLEAR SUBMARINE in the Arctic (ICEX 2020) - Smarter Every Day 237
This is awesome! Hey, it’s me Destin. Welcome back to Smarter Every Day. This video is unlike anything I’ve ever had the opportunity to do. It’s incredible. What you just saw was filmed in March of 2020. That was me on a sled on my way to a US nuclear sub…
RECESSION WARNING: My YouTube Income Is Crashing
What’s up, Graham? It’s guys here. So, things are getting serious, and we have a lot of indicators that would point to a potential recession. For example, it’s shown that a lack of conferences tends to coincide with a slowing economy. Exotic dancers say t…
HOW TO PROFIT CHURNING BANK ACCOUNTS IN 2019
What’s up guys? It’s Graham here. So really quick, I want you to ask yourself these three very important questions. Number one: Do you want to make an easy 500 bucks? Number two: Do you have some free time on your hands that you can kill? And number th…
why you understand English but CAN'T speak fluently
As you can probably tell from my accent and from my grammar mistakes, I’m not a native English speaker. I became fluent in English by watching YouTube videos and practicing by myself, and I always get comments like, “I can understand you, but I cannot tal…
Hunted in the Arctic | Edge of the Unknown on Disney+
I was 8 and my brother was about 10. We really wanted to go camping without any adults. My parents agreed as long as we trained. We were living in the Arctic, so it was cold temperatures and storms and blizzards and bears. But we wanted them to succeed in…
Multiplying 1-digit numbers by 10, 100, and 1000 | Math | 4th grade | Khan Academy
Let’s talk about multiplying by 10, 100, and 1,000. There’s some cool number patterns that happen with each of these, so let’s start here with something like 4 * 10—one that maybe we’re comfortable with or already know. 4 * 10 would be the same as saying…