Sunday, March 22, 2015

Sum and Count of Prime Number With Method and Parameter

Write a Java program to calculate the SUM and COUNT of Primes from a given range of numbers. Formatting required for the Input and Output statements. Use Method with Parameter and return value to the Main function.

Reference:
(1) Course Book(The Complete Reference Java j2se) ; Page 116
(2) http://c-programming-sourcecode.blogspot.com/2012/08/prime-number.html?updated-min=2012-01-01T00:00:00-08:00&updated-max=2013-01-01T00:00:00-08:00&max-results=14


Source Code:

package Prime;
import java.util.Scanner;
class PrimeCheeck
{
int sum=0;
int get_Prime_Sum(int range)
{
for(int i=1;i<=range;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag=1;
}
if(flag==0)
{
System.out.print(i+" ");
sum=sum+i;
}

}
System.out.println();
return sum;
}
}
public class Prime {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

PrimeCheeck pr =new PrimeCheeck();
int range,result;
System.out.print("Enter your last Range : ");
range = in.nextInt();
System.out.println("Sum of all prime number giving in range = " +pr.get_Prime_Sum(range));
}
}

12 comments:

  1. class Rectangle
    {
    double width;
    double height;

    double area()
    {
    return width*height;
    }
    void SetDem (double w, double h )
    {
    width = w;
    height = h;

    }
    }
    class Rectangledemo5{
    public static void main(String[] args) {
    Rectangle n1=new Rectangle();
    Rectangle n2=new Rectangle();

    double vol;
    n1.SetDem(10, 15);
    n2.SetDem(8, 12);
    vol=n1.area();
    System.out.println("Volume is=" + vol);

    vol=n2.area();
    System.out.println("Volume is=" + vol);

    }
    }

    ReplyDelete
  2. ID-201420406
    batch-46th

    class Box
    {
    double width;
    double height;
    double volume()
    {
    return width * height ;
    }
    void setDim(double w, double h)
    {
    width = w;
    height = h;
    }
    }
    class BoxDemo5 {
    public static void main(String args[]) {
    Box mybox1 = new Box();
    Box mybox2 = new Box();
    double vol;
    mybox1.setDim(2, 3);
    mybox2.setDim(3, 6);
    vol = mybox1.volume();
    System.out.println("Volume is " + vol);
    vol = mybox2.volume();
    System.out.println("Volume is " + vol);
    }
    }

    ReplyDelete
  3. ID-201421066
    Batch:46th

    package exx4;
    class Bx{
    int width,height;
    int muti()
    {
    return width*height;
    }

    void getvalue( int w, int h)
    {
    width = w;
    height =h;
    }
    }
    public class Exx4 {
    public static void main(String[] args) {
    Bx ob1 = new Bx();
    Bx ob2 = new Bx();
    double value;
    ob1.getvalue(10,5);
    ob2.getvalue(20,4);
    value =ob1.muti();
    System.out.println("Result is secound " +value);

    value =ob2.muti();
    System.out.println("Result is secound " +value);
    }

    }

    ReplyDelete
  4. package primenum;
    class check
    {
    int beg,end,inp,sum,count;
    int sumValue(int b,int e)
    {
    beg=b;
    end=e;
    for(inp=beg;inp<=end;inp++)
    {
    int t=1;
    for(int i=2;i<inp;i++)
    {
    if(inp%i==0)
    {
    t=0;
    break;
    }
    }
    if(t==1)
    {
    sum+=inp;
    count++;
    }
    }
    System.out.println("The number of prime is " + count);
    return sum;

    }

    }
    public class Primenum
    {
    public static void main(String[] args)
    {

    check c=new check();
    int b=1,e=10;
    double result=c.sumValue(b,e);
    System.out.println("THE SUM IS:"+result);
    }

    }

    ReplyDelete
  5. ID-201420674
    batch-46th

    class var
    {
    double width;
    double height;
    double volume()
    {
    return width * height ;
    }
    void setDim(double w, double h)
    {
    width = w;
    height = h;
    }
    }
    class varDemo5 {
    public static void main(String args[]) {
    var myvar1 = new var();
    var myvar2 = new var();
    double vol;
    myvar1.setDim(2, 3);
    myvar2.setDim(3, 6);
    vol = myvar1.volume();
    System.out.println("Volume is " + vol);
    vol = myvar2.volume();
    System.out.println("Volume is " + vol);
    }
    }

    ReplyDelete
  6. Q:Use Method with Parameter and return value to the Main function

    package principalnum;
    class barrier
    {
    int ask,last,inp,sum,count;
    int sumValue(int A,int L)
    {
    ask=A;
    last=L;
    for(inp=ask;inp<=last;inp++)
    {
    int P=1;
    for(int i=2;i<inp;i++)
    {
    if(inp%i==0)
    {
    P=0;
    break;
    }
    }
    if(P==1)
    {
    sum+=inp;
    count++;
    }
    }
    System.out.println("Principal is " + count);
    return sum;
    }
    }
    public class principalnum
    {
    public static void main(String[] args)
    {
    barrier bb=new barrier();
    int A=11,L=12;
    double result=bb.sumValue(A,L);
    System.out.println("the sum is:"+result);
    }
    }

    ReplyDelete
  7. package Prime;
    import java.util.Scanner;
    class PrimeCheeck
    {
    int sum=0;
    int get_Prime_Sum(int range)
    {
    for(int i=1;i<=range;i++)
    {
    int flag=0;
    for(int j=2;j<i;j++)
    {
    if(i%j==0)
    flag=1;
    }
    if(flag==0)
    {
    System.out.print(i+" ");
    sum=sum+i;
    }

    }
    System.out.println();
    return sum;
    }
    }
    public class Prime {
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);

    PrimeCheeck pr =new PrimeCheeck();
    int range,result;
    System.out.print("Enter your last Range : ");
    range = in.nextInt();
    System.out.println("Sum of all prime number giving in range = " +pr.get_Prime_Sum(range));
    }
    }

    ReplyDelete
  8. package primenum;
    class check
    {
    int beg,end,inp,sum,count;
    int sumValue(int b,int e)
    {
    beg=b;
    end=e;
    for(inp=beg;inp<=end;inp++)
    {
    int t=1;
    for(int i=2;i<inp;i++)
    {
    if(inp%i==0)
    {
    t=0;
    break;
    }
    }
    if(t==1)
    {
    sum+=inp;
    count++;
    }
    }
    System.out.println("The number of prime is " + count);
    return sum;

    }

    }
    public class Primenum
    {
    public static void main(String[] args)
    {

    check c=new check();
    int b=1,e=10;
    double result=c.sumValue(b,e);
    System.out.println("THE SUM IS:"+result);
    }

    }

    ReplyDelete
  9. package principalnum;
    class barrier
    {
    int ask,last,inp,sum,count;
    int sumValue(int A,int L)
    {
    ask=A;
    last=L;
    for(inp=ask;inp<=last;inp++)
    {
    int P=1;
    for(int i=2;i<inp;i++)
    {
    if(inp%i==0)
    {
    P=0;
    break;
    }
    }
    if(P==1)
    {
    sum+=inp;
    count++;
    }
    }
    System.out.println("Principal is " + count);
    return sum;
    }
    }
    public class principalnum
    {
    public static void main(String[] args)
    {
    barrier bb=new barrier();
    int A=11,L=12;
    double result=bb.sumValue(A,L);
    System.out.println("the sum is:"+result);
    }
    }

    ReplyDelete
  10. ID-201421068
    batch-46th

    package old;
    class Box{
    int width,height;
    int ad()
    {
    return width*height;
    }

    void getvalue( int w, int h)
    {
    width = w;
    height =h;
    }
    }
    public class Old {
    public static void main(String[] args) {
    Box ob1 = new Box();
    Box ob2 = new Box();
    double value;
    ob1.getvalue(10,5);
    ob2.getvalue(20,4);
    value =ob1.ad();
    System.out.println("Result is secound " +value);

    value =ob2.ad();
    System.out.println("Result is secound " +value);
    }

    }

    ReplyDelete
  11. Name-Main Uddin
    ID-201421090
    Batch-46th
    package exercise.pkg04;
    import java.util.Scanner;
    class PrimeNumberChecking
    {
    int Summation=0;
    int PrimeMain(int Count)
    {
    for(int P=1;P<=Count;P++)
    {
    int IsTrue=0;
    for(int T=2;T<P;T++)
    {
    if(P%T==0)
    IsTrue=1;
    }
    if(IsTrue==0)
    {
    System.out.print(P+" ");
    Summation=Summation+P;
    }
    }
    System.out.println();
    return Summation;
    }
    }
    public class Exercise04 {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    PrimeNumberChecking pr =new PrimeNumberChecking();
    int Number;
    System.out.print("Enter your Range : ");
    Number = in.nextInt();
    System.out.println("Sum of all prime number giving in range = " +pr.PrimeMain(Number) );

    }
    }

    ReplyDelete
  12. Name : Habibur Rahaman
    ID-201421091
    Batch-46

    package exercise.pkg04;
    import java.util.Scanner;
    class SAJIBXM
    {
    int Summation=0;
    int CTG(int Count)
    {
    for(int m=1;m<=Count;m++)
    {
    int IsTrue=0;
    for(int q=2;q<m;q++)
    {
    if(m%q==0)
    IsTrue=1;
    }
    if(IsTrue==0)
    {
    System.out.print(m+" ");
    Summation=Summation+m;
    }
    }
    System.out.println();
    return Summation;
    }
    }
    public class Exercise04 {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    SAJIBXM pr =new SAJIBXM();
    int Number;
    System.out.print("Enter your Range : ");
    Number = in.nextInt();
    System.out.println("Sum of all prime number giving in range = " +pr.CTG(Number) );

    }
    }

    ReplyDelete