Saturday, November 1, 2014

Filled Under: , , , , ,

Program to find all prime numbers in between a range and also sum all it's digits.

Share

Code Box
----------------------------
/*program to find a number is prime or not and sum its all digits between a range */

class rprimesum
{
public static void main(String a[])
{
int i,j,c,l,k,sum,n,b,m,z;
j=0;
z=0;
n=Integer.parseInt(a[0]);
m=Integer.parseInt(a[1]);

for(l=n;l<=m;l++)
{ //----------outer loop for number
c=0;
b=l;
sum=0;

for(i=2;i<l;i++)
{
if(l%i==0)
{c++;
}
}
//------------------------if it is prime
if(c==0)
{
for(j=0;j<=b;j++)
{
k=b%10;
b=b/10;
sum=sum+k;
}
System.out.println("Number "+l+" is prime and sum of digits are : "+sum);

z=1;//---------------------for checking any prime number found or not
}
//-------------------------------if it is prime end
} //----------------------------------------outer loop for number

if(z==0)   //-----------to check prime number exist or not
{
System.out.println("Not prime in between range");
}
}
}

0 comments:

Post a Comment