Posted on 16-02-2011
Filed Under (C++, programming) by admin

A Prime number can only be divided by one and itself. For example; 13 can only be divided by 1 and 13. Here’s a C++ code that prints out the all prime numbers between 0 and 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isPrime=true;
int num = 100;
 
for ( int i = 0; i <= num; i++)
{ 
	for ( int j = 2; j <= num; j++)
	{ 
		if ( i!=j && i % j == 0 )
		{ 
			isPrime=false;
			break;
		}
	}
	if (isPrime)
	{
		cout <<"Prime:"<< i << endl;
	}
 
	isPrime=true;
}

Comments Off    Read More   
Posted on 25-04-2009
Filed Under (java, programming) by admin

A prime number is a positive integer that has exactly two factors, 1 and the number itself. We know 0 is neither a positive nor a negative number. Otherwise, 0 is a neutral number. So, it is not a prime number, and 1 is not a prime number, 1 has only one factor, that’s 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isPrime = true;
int num = 100;
 
for ( int i = 0; i <= num; i++)
{ 
	for ( int j = 2; j <= num; j++)
	{ 
		if ( i!=j && i % j == 0 )
		{ 
			isPrime=false;
			break;
		}
	}
	if (isPrime)
	{
		System.out.println("Prime: "+i);
	}
 
	isPrime=true;
}

Comments Off    Read More   
Posted on 23-04-2009
Filed Under (java, programming) by admin

A palindromic number reads the same both ways eg. 10201, 12321, 14641, 40804, 44944, 69696
, here is a Java code that displays the largest Palindrome numbers from the product of two 3 digits numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
        int palin = 0;
        ArrayList<Integer> allPalin = new ArrayList<Integer>();
        String strPalin1 = "", strPalin2 = "";
        for(int i = 100; i <= 999; i++)
        {
            palin = i * i ;
            strPalin1 = Integer.toString(palin);
            strPalin2 = new StringBuffer(strPalin1).reverse().toString();
            if(strPalin1.equals(strPalin2))//finding palindromes
            {
                int aPalin = Integer.parseInt(strPalin1);//converting back to int to find biggest
                allPalin.add(aPalin);
            }
        }
        int largestYet = allPalin.get(0);//finding the largest palindrome in array
        for(int i = 0; i < allPalin.size(); i++)
        {
            int a = allPalin.get(i);
            if( a > largestYet )
                largestYet = a;
        }
        System.out.println(largestYet+" is the largest Palindrome!");
    }
}

Comments Off    Read More   
Posted on 21-04-2009
Filed Under (java, programming) by admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class Tictactoe {
    private static final int ROWS = 3;
    private static final int COLUMNS = 3;
    private String[][] board;
    //construct an empty board
    public Tictactoe()
    {
        board = new String[ROWS][COLUMNS];
        //Fill with spaces
        for(int i = 0; i < ROWS; i++)
        {
            for(int j = 0; j < COLUMNS; j++)
            {
                board[i][j] = " ";
            }
        }
    }
    //set the fields in the board
    public void set(int i, int j, String player)
    {
        if(board[i][j].equals(" "))
        {
            board[i][j] = player;
        }
    }
    /**creates a string representation of the board
     * @return the string representation
     */
    public String toString()
    {
        String r = "";
        for(int i = 0; i < ROWS; i++)
        {
          r += "|";
            for(int j = 0; j < COLUMNS; j++)
            {
                r += board[i][j];
            }
          r += "|\n";
        }
      return r;
    }
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String player = "x";
        Tictactoe game = new Tictactoe();
        boolean done = false;
        while(!done)
        {
            System.out.println(game.toString());
            System.out.println("Row for "+player+" (-1 to exit): ");
            int row = in.nextInt();
            if(row < 0) done = true;
            else
            {
                System.out.println("Column for "+player+": ");
                int column = in.nextInt();
                game.set(row, column, player);
                if(player.equals("x")) player = "o";
                else player = "x";
            }
        }
    }
}

Comments Off    Read More   
Posted on 17-04-2009
Filed Under (java, programming) by admin

In Mathematics the Fibonacci numbers are the following sequence of numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…..
the first two numbers are 0 and 1 the rest
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
Implementing Fibonacci in Java is easier done with recursion…. simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. A recursive method consist of a “base case” and the “recursive call”.. here’s how to implement Fibonacci’s number with recursion

public static long fib(int n)
{
   if(n &lt;= 2) return 1;
   else return fib(n - 1) + fib(n - 2);
}

but let’s take a look at how is done through linear programming.. that’s without recursion Read the rest of this entry »

Comments Off    Read More   
Posted on 07-04-2009
Filed Under (java, programming) by admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try
    {
        String host = "server-box";
        InetAddress addr = InetAddress.getByName(host);
        System.out.println("Scanning for open ports on "+"\""+host+"\""+"...");
        for(int i = 0; i < 65536; i++)
        {
            try
            {
                Socket s = new Socket(host, i);//trying to open connection
                System.out.println("port "+i+" open!");//we found open port
                s.close();//closing port
            }
            catch(IOException e){  }
        }
    }
    catch(UnknownHostException a)
    {
        System.out.println(a);
    }

Comments Off    Read More   
Posted on 21-03-2009
Filed Under (java, programming) by admin

In this article, I’m covering the common task of reading and writing files that contain text that are created with a simple text editor like Notepad or Wordpad. To read input from a disk file first construct a FileReader object with the name of the input file, then use the FileReader to construct a BufferedReader.

1
2
FileReader reader = new FileReader("input.txt");
BufferedReader br = new BufferedReader(reader);

This BufferedReader object can be use to read the file “input.txt”. You can use methods readLine() to read input from the file.
To write output to a file you construct a PrintWriter object with the given file name:

it is important that when you’re done writing data to the file you close the file with some clean up code

out.close();

If the output file already exist, it is emptied before the new data are written into it. If the file doesn’t exist, an empty file is created. Use print, println methods to send number, objects, and String to a PrintWriter:

1
2
3
out.println(20.40);
out.println(new Rectangle(5, 10, 15, 25));
out.println("Hello World!");

The print and println methods convert numbers to their decimal string representation and use the toString method to convert objects to strings. Read the rest of this entry »

Comments Off    Read More