Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me with java programs for computing and printing prime numbers/factorial of a no

Status
Not open for further replies.

nyadimo

Junior Member level 2
Joined
Mar 1, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,427
1.write a program which computes and prints prime numbers

2. write a program which computes and prints the factorial of a number
 

Re: java programming

to calculate factorial ;

public class P1
{
public static void main(String[] args)
{
int num=5;//the number required to calc its factorial .
int res=1;
for(int i=1;i<=num;i++)
res*=i;<
System.out.println(res);
}
}
to get prime numbers :

public class P2
{
public static void main(String[] args)
{
for(int i=1;i<=50;i++) //50 can be replaced by max num to calc to .
{
int x=2;
while((i%x)!=0 && x<50)
x++;

if (x==i)
System.out.println(i);
}
}
}


note : I haven't tried these codes so they may contain some bugs .
 

    nyadimo

    Points: 2
    Helpful Answer Positive Rating
java programming

Are we doing people's homework here ?
 

Re: java programming

For prime, odd, and even numbers.. I hope I am not doing people's homework.

class prima{
public static void main(String args[])
throws java.io.IOException{
utama();
}

public static void utama()
throws java.io.IOException{
int pil;
System.out.println("Pilihlah salah satu dari pilihan di bawah ini :D");
System.out.println("1. Tampilin bilangan prima");
System.out.println("2. Tampilin bilangan ganjil");
System.out.println("3. Tampilin bilangan genap");
System.out.print("Masukkan pilihan anda: ");
do{
pil = (int) System.in.read();
}while(pil < '1' || pil > '5');
switch(pil){
case '1': prima1(); break;
case '2': ganjil(); break;
case '3': genap(); break;
default:
}
}

private static void prima1()
throws java.io.IOException{
System.out.println("\n \nbilangan prima diantara 1 sampai 100 yaitu:");
for(int i = 1;i<=100;i++){
int k=0;
for(int j=1;j<=i;j++){
if(i%j==0) k++;
}
if(k==2) System.out.print(i+ " ");
}
}
private static void ganjil(){
System.out.println("\n \nbilangan ganjil diantara 1 sampai 100 yaitu: ");
for(int i = 2;i<=100;i+=2)
System.out.print(i-1+" ");
System.out.println();
}
private static void genap(){
System.out.println("\n\nbilangan genap diantara 1 sampai 100 yaitu: ");
for(int i = 2;i<=100;i+=2)
System.out.print(i + " ");
System.out.println();
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top