Computer Science, asked by maneeshalopchhan, 4 months ago

write a c program to print the sum of series as 1+3+9+27+81+243...up to the nth term

Answers

Answered by vvsgs0508
0

Answer:

please mark me as brainliest

Explanation:

This is really a question of iteration. We want to do something over and over for an undetermined amount of time. For this we need to sort what information we have:

1) We need to print a value to the screen so we will need at least 1 printf statement.

2) the terms follow a precise pattern (term = previousTerm * multiplier) so we will need at least 2 variables that will contain our data.

3) we need to repeat the process an undetermined number of times so we will need one of our control statements and a stopping point but the question is which one.

There are two choices that are likely to work for what we need: the while loop and the do ... while loop.

The while loop is a top driven structure which means that it tests the condition first and then if true runs the inside statements. On the other hand the do...while loop is a bottom driven structure which means that it runs the inside statements first then checks the condition to see if it needs to be done again.

Using this information we can start to form our code.

#include

main()

{

//variables

int term = 1, multiplier = 3, terminus = 1000; // terminus value was picked at random

// Statements

do

{

term = term * multiplier;

printf("%d\n", term);

}

while(term < terminus);

}

Provided the syntax is correct the output should be as follows:

3

9

27

81

243

729

2187

Take note that term started at 1 but the first number printed is 3 and that the terminus is 1000 but the output stops at 2187. The line "term = term * multiplier;" takes whatever value is stored in the variable term and multiplies it by the value that is stored in multiplier. In this case 1 is stored in term and 3 is stored in multiplier therefore 1*3 = 3 so 3 is now stored in the variable term.

As for why our output stops at 2187 that is because we used a bottom driven loop. the inside block of code is run BEFORE the check is made so when term contained 729 it was multiplied by the multiplier (containing the value 3) and changing the value of term to 2187. the do...while statement then compared the value in term to the value in terminus and since 2187 is NOT smaller than 1000 the result came back false and the loop did not repeat and our code exited.

try accomplishing the same task using a top driven while loop and see if the output is the same. If not then try to figure out what is different and why.

Good luck in your coding and remember coding is fun so try to keep it fun. If you start to feel stressed step back relax and try again in a few minutes.

Similar questions