2 programs of constructor in java !!!!!
Answers
Answered by
1
1.
import java.util.*;
class Calculate{
int num,f,rev;
Calculate(int n){
num = n
f = 0;
rev = 0;
}
void prime(){
int ctr = 0;
for(int i = 1; i <= num; i++){
if(num % i == 0)
ctr++;
}
if(ctr == 2)
System.out.println("1");
}
}
2.
import java.util.*;
class Prime{
int n;
public Prime(){
n = 0;
}
void input(int x){
n=x;
}
void display(){
int ctr = 0;
for(int i = 1; i <= n; i++){
if(n%i == 0)
ctr++;
}
if(ctr == 2)
System.out.println("The number is prime");
}
public static void main(String args[]){
Prime ob = new Prime();
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
ob.input(num);
ob.display();
}
}
Hope this helps,
ira.
Similar questions
Social Sciences,
6 months ago
English,
6 months ago
English,
6 months ago
Biology,
11 months ago
Chemistry,
11 months ago