196k views
2 votes
Write an algorithm and draw flowchart to print 30 terms in the following sequence

1,-2,3,-4,5,-6,7,-8,...........................up to 30 terms.

User Shutefan
by
5.4k points

1 Answer

4 votes

Answer:

/*

I don't know what language you're using, so I'll write it in javascript which is usually legible enough.

*/

console.log(buildSequence(30));

function buildSequence(maxVal){

maxVal = Math.abs(maxVal);

var n, list = [];

for(n = 1; n < maxVal; n++){

/*

to check for odd numbers, we only need to know if the last bit

is a 1 or 0:

*/

if(n & 1){ // <-- note the binary &, as opposed to the logical &&

list[list.length] = n;

}else{

list[list.length] = -n;

}

}

return list.implode(',');

}

User Radko Dinev
by
4.6k points