Computer Science, asked by divyadivi1206, 14 hours ago

Write a program to generate the first n terms in series 3, 9, 27, 81,...

Answers

Answered by PurpleHeartz
3

{\boxed{\pink{\huge{\tt{\color{hotpink}{Answer}}}}}}

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 cöde.

#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 cöde 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 cöde exited.

Answered by shagun0505
1

Answer:

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

2) The terms follow a precise pattern ( term- previous term 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 need a stopping point but the question is which one.

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

Similar questions