Originally Posted by
arko93
class Cons
{
int a,c,k=0; // CMH: You Can't initialize members in a class declaration,
// Do in the constructor. Maybe these should be local to your methods and not class
// members
int number()
{ //fun
for(a=1;a<=10;a++)
{//for a
for(c=1;c<=a;c++)
{//for c
if(a%c==0)
k++;
} //for c
}//for a
if(k==2)
{
System.out.println("prime numbers are"+a);
}
} // fun
// You just ended the declaration of the above member. You have the value K, what are you doing with it here? Should you return it?
int number(int x,int y)
{
a=x;
c=y;
for(a=1;a<=10;a++)
{
for(c=1;c<=a;c++)
{
if(a%c==0)
k++;
} } }
// You just ended the declaration of the above member. You have the value K, what are you doing with it here? Should you return it?
}
public class Prime
{
public static void main(String[]args)
{
Cons t=new Cons(); // You only need to allocate this one.
System.out.println("result = "+t.number());
Cons t1=new Cons(20,30); // The numbers here
System.out.println("result = "+t1.number());// should be the parameter list for this.
}
}
Why I am getting an error in this program
error- Prime.java:22: error: missing return statement
}
^
Prime.java:35: error: missing return statement
} } }
^
Prime.java:46: error: constructor Cons in class Cons cannot be applied to given types;
Cons t1=new Cons(20,30);
^
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length