Match Schedule Generator Algorithm <p>I'm working on a schedule generator where every team plays a match with each other teams. My Database table and the output I need like below, <img src="https://i.stack.imgur.com/pf2tZ.png" alt="enter image description here"></p> <p>What I have tried so far is</p> <pre><code> &lt;% ResultSet rsteams = clmmodel_database.selectQuery("select count(ct.teamid) as teamcount, teamid,teamname from clm_team ct"); while(rsteams.next()){ int teamcount = rsteams.getInt("teamcount"); int n = teamcount - 1; int numofmatches = n*(n+1)/2; %&gt; &lt;h1&gt;Team Count = &lt;%out.print(teamcount);%&gt;&lt;/h1&gt; &lt;h1&gt;Number of Matches = &lt;%out.print(numofmatches);%&gt;&lt;/h1&gt; &lt;table&gt; &lt;%for(int i =0;i&lt;n;i++){%&gt; &lt;tr&gt; //Here I need to display the matches row by row &lt;/tr&gt; &lt;%}%&gt; &lt;/table&gt; &lt;%}%&gt; </code></pre> <p>Which retrieves the team count and the number of matches to be played. Please help me on this.</p>