Some Fascinating Java Programs


 

Finding Prime Number

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

 

import java.io.*;

public class PrimeNo {

 

/**

* @param args

*/

public static void main(String[] args) throws Exception {

 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter a  number”);

String s=br.readLine();

 

int i=Integer.parseInt(s);

int j=i/2;

int count=0;

for(int a=2;a<=j;a++)

{

if(i%a==0)

count++;

}

 

if(count==0)

{

System.out.print(“Prime number”);

}

else

{

System.out.print(“Not Prime number”);

}

}

 

}

 

 
Printing Words In Different Order

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PrintingWordsInDiffProb
{
static String str=” “;
static char a ;
//static String s=”Welcome to 4i apps and we welcome you”;
static int cc=0;
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a  number”);
String s=br.readLine();
String c=s;
String k;
String y;
for(int i=0; i <= s.length()-1; i++)
{

if(c.charAt(i) == ‘ ‘)
cc=cc+1;
}
//System.out.println(cc);
for(int i=0;i<=cc;i++)
{

int j=c.indexOf(‘ ‘);

k=c.substring(j);
y=c.substring(0, j);
c=k+” “+y;
c=c.trim();
//System.out.println(k);
System.out.println(c);

}

}
}

Printing Diamond Star Program

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DiamondStar {
//import java.io.*;

static int e;
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a  number”);
String q=br.readLine();
int z=Integer.parseInt(q);

String j=” “;
String k=”                                “;
int g=z/2;
int p=g-1;
int r=0;
String s=”**”;
String m=”*”;
for(int i=1;i<z;i++)
{

if(i<=g)
{
j= k.substring(0, (z-g)-i)+j.trim()+s;
//j=j+s;
System.out.println(j.substring(0, j.length()-1));
e=j.length();
}

else{

String w = k.substring(0,r+1)+j.substring(0, z-i)+j.substring(0, z-i-1);
System.out.println(w);
e–; r++;
}
}
}
}

Converting number to char in String

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RepNumWithWords
{
static String y;
static String y1=” “;
public static void main(String[] args)  throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a String with number”);
String str=br.readLine();
char c=’a’;
String k[]=new String[27];
int j[]=new int[27];
String e[]=new String[27];
String a=”a”;
for(int i=1;i<=26;i++)
{

e[i]=””+i;
j[i]=c;
k[i]=Character.toString ((char) j[i]);
c++;
}
for( int r=str.length()-1;r>=0;r–)
{
y=str.substring(r, r+1);
for(int v=1;v<=26;v++)
{
if(y.equals(e[v]))
{
y=k[v];
}
}
y1=y+y1;
}
System.out.println(y1);
}
}

About pacesettergraam

A good and realistic person

Posted on April 24, 2013, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: