Computer Science, asked by lmbadi03, 1 year ago

WAP in java to calculate the sum of all prime numbers between 1 to 100 .

Answers

Answered by Anonymous
22

CODE:


class prime_sum

{

public void main()

{

int s=0;

int c=0;

for(int i=1;i<=100;i++)

{

c=0;

for(int j=1;j<=i;j++)

{

if(j%i==0)

{

c++;

}

}

if(c==2)

{

s=s+i;

}

}//end of for-loop

}//end of main

}//end of class


Note :

The prime logic is given so that primes have 2 and only 2 factors.


Hope it helps :-)

________________________________________________________

Answered by AngshumanRoy
5
import java.util.*;
public class cls
{
public static void main (String args [])
{
Scanner sc=new Scanner (System.in);
System .out.println("enter a number");
int n=sc.nextInt();
int sum=0,count=0;
for (int i=1;i<=100;i++)
{
if (n%i==0)
{
count++;
sum+=i;
}
System.out.println(sum);
}
}
if(count==2)
{
System.out.println("prime");
}
else
{
System.out.println("composite");
}

}
}

AngshumanRoy: please mark me brainlist
AngshumanRoy: i hope my answer helps you a lot
Similar questions