- Creating a standard 31-day calendar
- A program that allows the user to input a number, which will then be the dimension for two squares - one empty and one filled (with asterisks)
- Example: if user types "3," the program spits out:
- *** ***
- *** * *
- *** ***
- A program where the user inputs a number, and the program will print all prime numbers within given number
- Example: if user types "10," the program prints:
- 2
- 3
- 5
- 7
Put simply: I misused the if-then statement ^_^
Oops!
My excuse is that it was late at night, and I wasn't thinking clearly. :)
Anyway, here is the code I came up with:
//These are possible prime numbers up until the number inputted
for (int prime=2; prime <= input; prime++)
{
//These are divisors to check if the number is indeed prime
int divisor;
for (divisor = 2; divisor <= prime; divisor++)
{
//if prime/divisor has remainder of 0
if (prime % divisor == 0)
{
//AND if prime and divisor are the same number, then it's prime
if (prime == divisor)
{
System.out.println(prime);
}
//otherwise, it's not. Break out of if-then loop.
else
{
break;
}
}
}
}
No comments:
Post a Comment