pTutorial

New way to learn Programming

Java Program Example

In this section we will discuss about some important java program example with output and explanation like java program to check prime number, java program to print Fibonacci series, java program to print array etc.

Java program to check prime number


class PrimeNumber{
public static void main(String...s){
int n=8960, temp=0,m=n/2;
for(int i=2;i<m;i++){
if(n%i==0){
temp=1;
break;
}
else{
temp=0;
}
}
if(temp==1){
System.out.print("Not prime");
}
else{
System.out.print("prime no");
}
}
}

Output

Not prime

java array example

//user define java program to print array
package arrayexample;
import java.util.Scanner;
public class ArrayUser 
{
    static int n;
    public static void main(String[] args) {
    Scanner uk = new Scanner(System.in);
    System.out.println("Enter the number of element");
    n = uk.nextInt();
    int x[] = new int[n];	

    
    System.out.println("Enter "+ n +" numbers");
    for(int l=0;l<x.length;l++){
        x[l]= uk.nextInt();
    }
    
    System.out.println("OuTput:");
    System.out.println("Entered numbers are :");
    for(int l=0;l<x.length;l++){
        System.out.println(x[l]);
    }
}
}

Output

Enter the number of element
4
Enter 4 numbers
12
38
07
89
OuTput:
Entered numbers are :
12
38
7
89

Java Reverse Number Example

class ReverseNumber{
public static void main(String...s){
int n=534, rev=0;
while(n!=0)
{
rev=rev*10;
rev=rev+n%10;
n=n/10;
}
System.out.print("Reversed number is: "+rev);
}

}
Reversed number is: 435

QUOTES OF THE DAY

Wicked men obey from fear, good men from love

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER