I have to work on a question for college using arrays this is it:
Write a Java program that will create an array of size 10 and into it put the first 10 even numbers greater than the given user input.
This is what I'm trying but I can't get the right output
import java.util.Scanner;
public class EvenNumbers
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
int array[]=new int[10];
System.out.println("Please enter a number:");
int num = scan.nextInt();
num+=1;
int i=0;
while(i<10)
{
if(num/2==0)
{
array[i]=num;
num++;
i++;
}
else
num++;
}
for(int j=0; j<array.length; j++)
{
System.out.print(array[j]);
}
}
}
You should check num%2==0 instead of num/2==0
And also you can simplify your program by iterating only on even numbers:
num++;
if (num % 2 == 1) // make sure that num is even
num++;
for(int i = 0; i < 10; i++)
{
array[i] = num;
num += 2; // jump to the next even number
}
you can use if (n % 2 == 0) because if (n % 2 == 0) would run if n was even (n can be divided evenly by 2). if (n % 2 == 1) would run if n was odd.the % sign is called mod. Its like dividing and getting the remainder.
Try this :)
import java.util.Scanner;
public class EvenNumbers
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
int array[]=new int[10];
System.out.println("Please enter a number:");
int num = scan.nextInt();
num+=1;
int i=0;
while(i<10)
{
if(num%2==0)
{
array[i]=num;
num++;
i++;
}
else
num++;
}
for(int j=0; j<array.length; j++)
{
System.out.print(array[j]);
}
}
}
Just for fun, you can do it using java-8 IntStream class
IntStream.iterate(++num + num % 2, i -> i + 2).limit(10).forEach(System.out::println);
// ++num; generated numbers should be greater than the given user input
// ++num + num % 2; num variable should be even itself.
Related
import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
while (a < b) {
for (int i = a; i <= b; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} //count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println(i);
a++;
}
}
}
}
}
This is what my output looks like. I dont understand why last prime numbers are being repeatedly printed.
What you should probably do is breaking task into steps.
Get the input
satrt finding the prime numbers from 1 to the max
create a list of already found prime numbers
start a loop from 1 to max
each iteration loop trough already found primes and perform number % prime == 0
if none of numbers in prime numbers can fulfils the condition add number to the list
if found number is greater or equal to min, save the index of it (startIndex).
print the input by iterating trough list from startIndex and printing the numbers
this workflow guarantees you will not consider multiplicant of two big prime numbers as prime number with better performance
You missplaced your first statement, your while-loop is a little bit scary, but here your fix:
import java.util.Scanner;
public class PrimeN{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
for(int i = a; i <= b; i++){
int count = 0;
for(int j = 1; j <= i; j++){
if(i%j == 0){
count++;
}
}//count if equals 2 the prime numbers displayed
if(count == 2){
System.out.println(i);a++;
}}}}
Found a way to solve the ques.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
for(int n = a; n <= b; n++){
boolean prime = true;
int i = 2; //1 will be a factor always
while(i <= n/2){
if(n%i == 0){
//is not prime
prime = false;
break;
}i++;
}
if(prime){
System.out.print(n+ " ");
}
}}}
Just remove your while(a<b) around the two for loops and then your code works fine
like this
import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
// System.out.println("a<b");
int i = a;
while(i<=b)
{
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} // count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println("a" + a + " i:" + i);
a++;
}
i++;
}
}
}
To find out what the actual error is, print out the values of a,b,i and j in each iteration.
This piece of code is supposed to take 2 numbers and find the factorial of every number between and incuding said numbers. I'm not getting the right output however and cant figure out what im doing wrong.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0) //want factorial greater than zero
for(int j = n; j <= m; j++)
{
for(int i = 1; i <= j; i++)
{
result = result * i; //find factorial
}
System.out.println(result);
}
if(n <= 0 || m <= 0) //if value is les than zero
{
System.out.println("Not Valid!");
}
Something like should work:
public class RangeFactorial {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int max = scan.nextInt();
int min = scan.nextInt();
if (max < 0 || min < 0) {
System.out.println("Invalid Params");
}
for (int i = min; i <= max; i++) {
System.out.println("Factorial for " + i + " is: " + factorial(i));
}
scan.close();
}
private static int factorial(int i) {
if (i <= 1) {
return 1;
}
return i * factorial(i-1);
}
}
Note that the code assumes that max/min fall in place, I've omitted the logic to decide the max/min integer from the given inputs. You'll need to add this.
You forgot to reset 'result' to 1.
Also, there is no need to have another if statement if it is only checking for the negation of the first one, just use an else.
I also fixed the code style guidelines to follow the standard Java ones as:
The curly bracket style you used is the one usually used in C/C++.
Even if if-statements or loops contain only one line after them, it is good etiquette to use curly brackets anyway, in Java.
Take a look at the Google Java Style Guide if you want to know more.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0){
for(int j = n; j <= m; j++){
result = 1; //You forgot to reset 'result'
for(int i = 1; i <= j; i++){
result *= i;
}
System.out.println(result);
} else { // No need for another if statement
System.out.println("Not Valid!");
}
Im trying to create a descending triangle of numbers like this in Java:
4
3 3
2 2 2
1 1 1 1
The user inputs the first number and then it's supposed to create the descending triangle and honestly I don't know how to figure it out.
I haven't really tried that much I'm just lost :)
Here's my code so far:
public static void numberlines(){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int counter = 0;
int count = 0;
char tab = 9;
int i;
usernum = getInt();
while (counter != usernum){
if (usernum > counter) {
usernum--;
System.out.println(+ usernum);
}else if (usernum < counter){
usernum++;
System.out.println(+usernum);
}
}//while
}//numberlines
Right now it just prints the descending line of numbers but I'm pretty sure there's a lot more to it. If anyone has any suggestions or ideas that would be awesome. Thanks
You can do this by nested while loops:
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int i = 1;
usernum = in.nextInt();
while(i <= usernum){
int j = 1;
while(j<=i){
System.out.print(usernum-i+1);
j++;
}
System.out.println();
i++;
}//while
}//numberlines
You can use this:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int usernum = s.nextInt();
for(int i = 1; i <= usernum; i++){
for(int j = 1; j <= i; j++){
System.out.print(usernum-i+1);
}
System.out.println();
}
}
package Testing;
import java.util.Scanner;
public class test {
public static void main (String[] args){
Scanner scan1 = new Scanner(System.in);
System.out.print("Input positive integer: ");
int n = scan1.nextInt();
for (int i = 2; i < Math.sqrt(n); i++){
if (n % i == 0){
System.out.print(n/i);
}
}
}
}
When I run this code it ends up printing the numbers 2510 whenever I input 50. What did I do wrong?
As was mentioned in the comments, you're very close, but you need to stop iterating after you find an answer. This can be done with the break keyword, which exits the innermost loop.
for (int i = 2; i < Math.sqrt(n); i++){
if (n % i == 0){
System.out.print(n/i);
break;
}
}
You have to stop when you find the first prime factor. And do not forget, that in your example Math.sqrt(n) is calculated every loop.
public static int getLargestFactor(int num) {
for (int i = 2, max = (int)Math.sqrt(num); i <= max; i++)
if (num % i == 0)
return num / i;
return num;
}
Here is my code :-
package javaapplication;
import java.util.Scanner;
public class perfect02 {
public static void main(String args[]) {
int i = 1, sum = 0;
System.out.println("Enter maximum range : ");
Scanner kb = new Scanner(System.in);
int a = kb.nextInt();
System.out.println("Enter minimum range : ");
Scanner kb2 = new Scanner(System.in);
int b = kb2.nextInt();
System.out.println("perfect number in the given range are :");
for (int n = b; n <= a; n++) {
while (i < n) {
if (n % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == n)
System.out.println(+n + " ");
}
}
}
Why program is not printing perfect numbers ?
I have checked code many times but i am unable to find the solution .Please tell me what is going wrong in my code .Thanks in advance
Any help would be appreciated ....
Here, I looked into the perfect number generation and fixed it for you!
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter minimum range : ");
int b = kb.nextInt();
System.out.println("Enter maximum range : ");
int a = kb.nextInt();
kb.close();
System.out.println("Perfect number in the given range are :");
for (int n = b; n <= a; n++) {
int sum = 0;
int i = 1;
while (i < n) {
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
System.out.println(n + " is perfect");
}
}
You should've had the sum and i variables declared inside the for loop, so they would be reset for each number!