Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

find the last element using the formula n*n-(n(n-1)/2)

// then find the distacne at current printingn element and lst tern

if col != 0

//then element at current posotion cur_err = total sum of elements in previous col + distance in
//present col

final_element = lastterm - cur_err

int n = 5;

int n2= n;

double f = (n * n) - (n * (n - 1) / 2);

for (int i = 0; i < n; i++) {

for (int j = 0; j <= i; j++) {

// calc no of previious column

currenrt row = j;

// calc no of elements in column

int sum_of_colelements = 0; //which

int y = n;

//calc sum of elements in prev col

for (int k = 1; k <= j; ++k) {

sum_of_colelements = sum_of_diagonal + y;

--y;

// find the distance and order in current col

let dir = 0;

if(dir%2==0)
{

//then decrese elements in upward

//calc the distance from bottom bot = n;

distance = n-j;

final_element = last_term-(sum_of_prevcolelements + distance );

System.out.print(final_element);

else

//then decrease elements in downwarsd

top_of_current_row= i; // since col is shifting along with row;

current_col_diatance= n-i;

total_dis = sum_of_prevcolelements + current_col_diatance;

final_term= lastterm- total_dis;

System.out.print(final_term)

If(col==0)

System.out.print(n2);

--n;

System.out.println();

You might also like