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.
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 .
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 ");
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();
}
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.