Pseudocode and flowchart to find the series of S= 1 - 3 + 5 - 7 + 9

[286 views]


Flowchart to find the series of number:

Algorithm and flowchart to find the series of S= 1 - 3 + 5 - 7 + 9
Remove WaterMark from Above Flowchart

Algorithm to find the series of number:

Declare i, sum, n and t; Read n; for i = 1 to i <= 2 * n - 1 incrementing i by 2 { sum = sum + i * t; If(i * t < 0) print i * t; else print + i * t; t = (-1) * t; } print sum;

Here in this algorithm we declare four variables as integer, n for storing the number, i for running the for loop, sum for storing the result and t for deciding if its positive(+1) or negative(-1).

Then we read the variable n and run the for loop from i=1 to n*2-1 representing the odd number like if we choose 5 then at the 5th number 9 should be there. And incrementing i by 2 as we need odd numbers we will start form 1 and add 2 to get odd number. Then using the if statement we will check if the number n is less than 0 or not. If yes then print as it is and if no print +i*t .We are doing this only to print the series as positive numbers do not come with + sign. For getting the sum we add sum of previous loop and odd number of this loop. For Eg. Let's take N =3 then loop will run from 1 to 5

  • In the first iteration i=1 sum =1 and we print +1 and t becomes -1
  • In the second iteration i=3 sum=1-3 as t is negative from the previous loop print -3 and t becomes 1
  • In the third iteration i=5 sum =1-3+5 print 5 and t now becomes -1
Now we exit the loop as we have reached i = 5 and print the sum i.e. 3

Implementation of the Program in C:

#include<stdio.h> int main() { int i, s = 0, n, t = 1; printf("N:"); scanf("%d", & n); for (i = 1; i <= 2 * n - 1; i += 2) { s = s + i * t; i * t < 0 ? printf(" %d ", i * t) : printf(" + %d ", i * t); t = (-1) * t; } printf(" \nSum is %d ", s); }

Output of the program

Algorithm and flowchart to find the series
        

Struggling to Understand Algorithm and Flowchart? Try our Notes



Want to Test Your Knowledge on Algorithm and Flowchart?



Comments



Recommended Deals End in



Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Algorithm to find the series of S= 1 - 3 + 5 - 7 + 9

    Flowchart to find the series of S= 1 - 3 + 5 - 7 + 9

    C Program for finding the series of S=1-3+5-7+9