Access over 20 million homework & study documents

search

write a program to print prime numbers from 1 to 100 using loops in java script

Type

Homework

Rating

Showing Page:
1/2
Write a program that will print all the prime numbers between 1 and 100.
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String

Sign up to view the full document!

lock_open Sign Up
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}
Output:
Prime numbers from 1 to 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

Write a program that will print allthe prime numbers between 1 and 100.class PrimeNumbers{ public static void main (String[] args) { int i =0; int num =0; //Empty String String primeNumbers = ""; for (i = 1; i <= 100; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { //Appended the Prime number to the String primeNumbers = primeNumbers + i + " "; } } System.out.println ...
Purchase document to see full attachment

Anonymous
Very useful material for studying!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4