chevron_left
240 points
4 2 2

Write a Program to Print First x Terms of the Series 3n+2 which are not Multiples of 4
Photo by geralt

In this article, you're going to learn on how to fix errors that come when you want to write a program to print the first x terms of the series 3n+2 which are not multiples of 4. Understanding the algorithm and pseudocode, is core toward solving the errors that arise.

Reason for the Error

The terms set for printing is derived from multiplying the equation 3n+2. The n in the formula can be any unique number unique from the initial.

In simple words, the program executes the equation 3n+2 with the changing variable n. With each change of n, the results of term is different. More importantly, the terms of the series that are printing should not be divisible by 4.

The main issues arise here. Majorly, the while loop is critical to enable recurrent program execution until certain condition is met. More so, the values should not be multiples of 4 hence the printed value modulus four should not equate to 0.

The x terms on the other hand is a variable set by the user and when the user sets such that the x terms are 5 then the program prints 5 terms in the 3n+2 series. All the terms should not be multiples of 4. In the next sections, and as stated, we take a look at the exact algorithm, pseudo code and program that accomplishes the task.

The Algorithm to print the first x terms of Series 3n+2 that are Not Multiples of 4

The algorithm used for the program is especially simple:

START

Step 1: Take a variable lim.

Step 2: Assign the variable lim to an integer derived from user input.

Step 3: Create variable counter and assign to 0.

Step 4: Create a variable n and assign it to value 0.

Step 5: Perform while counter is not equal to 0

Step 6: If so, increment value n

Step 7: Create variable eq and assign to (3*n) +2

Step 8: If eq is greater than 4 and eq is not a multiple of 4 print eq

Step 9: Increment the counter

Now that we have the algorithm, let's have a look at the pseudo code.

Pseudo code to Writer the first x Terms of the Series 3n+2 that are not Multiples of 4

While counter is not equal to limit

Increment n term of the series

And execute the eq value

If the eq value is not a multiple of 4 and the eq is greater than 4

Print eq

Increment the counter

The Python Program that write first x Terms of a Series that are Not Multiples of 4

We use the while counter to perform the operation. With while, the program checks if the value of counter is not equal to the eq value set by the user. If so, it first increment the present variable n by 1, and then uses n to execute the equation 3n+2. Here is the program that makes it happen:

while counter != lim:

n +=1 eq=(3*n)+2

The python programming language has got an operator called modulus that returns the remainder after a given division. As in our case, after executing the equation 3n+2, and checking whether the result is a multiple of 4, the result of modulus can either be a 0 or any other value. If the result is a 0 then then the value of eq is a multiple of 4 and hence not desired for our program. Otherwise, the output is correct and hence the print statement executes. Also, a program that is a multiple of 4 has to be greater than 4 and hence the program checks if that is accomplished too.

Now let us have a look to check whether the value is a multiple of 4 or not:

if (eq % 4 != 0) and (eq>4):

Moving ahead here is the complete code that implement the program:

lim=int(input())
counter=0
n=0
while counter != lim:
        n +=1
eq=(3*n)+2
if (eq % 4 != 0) and (eq>4):
print(eq)
counter += 1

Executing the program at first prompts the user to enter the number of terms x in the series. In this case, let us enter the value 6. The output of the program is as follows:

5

11

14

17

23

26

 

In this program, we took the value which the user enters as the number of terms of the series 3n+2 which the computer output. More so, the computer checks to ensure that the value displayed is greater than 4 and not a divisible of 0.

 

Conclusion.

Now that you have gone through this post, you now understand how to write a program to print the first x terms of the series 3n+2 which are not multiples of 4 with deep details. Hopefully, this post adds value adds value in your quest toward building the program.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Given an array of ints, return true if the array contains a 2 next to a 2 somewhere. NoirHusky - Sep 8
Write a 'C' program to find the day of the given date. Bushra Rubab - Sep 9
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). zhteja - Aug 1
Python program to print even numbers from the list Ankur Ranpariya - Jul 16
Program to calculate number of vowels in a string in Python Ferdy - Aug 6
Print the contents of file in reverse order in Python Ferdy - Jul 28
Given a string, return a string where for every char in the original, there are two chars. Ankur Ranpariya - Jul 19
Which method is used to delete a directory in Python Ferdy - Aug 24
Failed to load resource: the server responded with a status of 404 (not found) Ankur Ranpariya - Sep 18
The truth value of an array with more than one element is ambiguous. use a.any() or a.all() AAbouelenien - Sep 22
Given a List of Non Negative Integers, Arrange them such that they Form the Largest Number Ankur Ranpariya - Jul 27
Write a program that squares an integer and prints the result Aida Perez - Jul 14
Format a number of seconds to date using python amr12345a - Jun 6
Write a program to reverse an integer value AAbouelenien - Sep 5
Add to the list in a loop using python amr12345a - Jun 2
Which of the following platform(s) provides a modular architecture? Hussain Zafar - Jun 2
Write a function max that has two java string parameters and returns the larger of the two NoirHusky - Sep 2
Write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end. sadmin - Apr 29
Write Recursive Implementation of Binary Search in Python Ankur Ranpariya - Jul 22
distributed 1.21.8 requires msgpack, which is not installed - Fixed sadmin - May 20