Question

These are the only instructions I'm given. This is intended to be one assignment. We are...

These are the only instructions I'm given. This is intended to be one assignment. We are only allowed to use 'for' loops. Thanks for your help.

Using Java:

  • Create an array with 10 integer elements.
  • Ask user to input 10 integer values.
  • going through all the array elements and find/print out all the prime numbers.

Instructions:

Only use for loops for this lab (no need to create methods or classes for this assignment). Put comment on each line of your codes, explaining what each line of code does.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Below is your required java program using only for loops*/

import java.util.Scanner;/*import for taking input*/
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);/* Object for input */
int a[]=new int[10];
System.out.println("Enter 10 integers:");
for(int i=0;i<10;i++)
a[i]=sc.nextInt();/* Enteer integers*/
System.out.println("Prime Numbers are ");
for(int i=0;i<10;i++){
boolean b=true;
if(a[i]<=1)
b=false;
else{
label: for(int j=2;j<a[i];j++){/* Check prime number condition for each array element*/
  
if(a[i]%j==0){/* If true set boolean to false*/
b=false;
break label;
}
}
if(b==true)/* If true then print it*/
System.out.print(a[i]+" ");

}
}
}
}

Add a comment
Know the answer?
Add Answer to:
These are the only instructions I'm given. This is intended to be one assignment. We are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT