CPP All : Series printing loops
Concept Learning Code Sheets

#### Square terms series #2353

Printing 10 terms of square terms of natural numbers beginning from 1.

#include<iostream.h>
using namespace std;
int main( )
{
for(int i=1;i<=10;++i)
{
cout<<i*i<<" ";
}
return 0 ;
}

#### Series building concept with different term expression builder and term counter #2364

It is possible to build many series expression easily when we take different variables for term counting and term expression building.

#include<iostream.h>
using namespace std;
int main( )
{
int no_of_terms=10;
int tbase=5;
int texp=tbase;
for(int tc=1;tc<=no_of_terms;++tc)
{
int term=texp*texp;
cout<<term<<" ";
texp+=2;
}
return 0 ;
}

#### Series based on x^2+x+1 #2367

Writing 10 terms of a series based on x^2+x+1 where x is taken from user.

#include<iostream.h>
using namespace std;
int main( )
{
int no_of_terms=10;
int x;
cout<<"Give x for x^+x+1 series :";
cin>>x;
int tbase=x;
int texp=tbase;
for(int tc=1;tc<=no_of_terms;++tc)
{
int term=texp*texp+texp+1;
cout<<term<<" ";
texp+=1;
}
return 0 ;
}

#### Fibonacci series from 1 (All terms using loop) #2392

Printing 10 terms of Fibonacci series  starting from 1 like 1 1 2 3 5 8 13 21 34 55 . All terms are printed using the loop.

#include<iostream>
using namespace std;
int main( )
{
int no_of_terms=10;
int term=1,pterm=0, ppterm;
for(int tc=1;tc<=no_of_terms;++tc)
{
cout<<term<<" ";
ppterm=pterm;
pterm=term;
term=pterm+ppterm;
}
return 0 ;
}

#### Fibonacci series from 0 (All terms using loop) #2404

Printing 10 terms of Fibonacci series  starting from 0 like 0 1 1 2 3 5 8 13 21 34 . All terms are printed using the loop.

#include<iostream>
using namespace std;
int main( )
{
int no_of_terms=10;
int term=0,pterm=1, ppterm;
for(int tc=1;tc<=no_of_terms;++tc)
{
cout<<term<<" ";
ppterm=pterm;
pterm=term;
term=pterm+ppterm;
}
return 0 ;
}

#### Fibonacci series from 0 (Fixed first two terms) #2409

Printing 10 terms of Fibonacci series  starting from 0 like 0 1 1 2 3 5 8 13 21 34 . First two terms are printed as fixed terms and rest all terms are printed using the loop.

#include <iostream>
using namespace std;
int main( )
{
int no_of_terms=10;
int ppterm=0,pterm=1,term;
cout<<ppterm<<" "<<pterm<<" ";
for(int tc=3;tc<=no_of_terms;++tc)
{
term=pterm+ppterm;
cout<<term<<" ";
ppterm=pterm;
pterm=term;
}
return 0 ;
}

#### Prime number series #2429

Print a series of prime numbers up to predefined number of terms..

#include<iostream>
using namespace std;
int main( )
{
int no_of_terms=50;
int term,n=2;
for(int tc=1;tc<=no_of_terms;++tc)
{
while(1)
{
int dc=0;
for(int i=2;i<=n/2;i++)
if(n%i==0) dc++;
if(dc>0) n++;
else break;
}
term=n++;
cout<<term<<" ";
}
return 0 ;
}

#### Simple Geometric series 1+x+x^2+..+x^n #2434

Printing the simple geometric series and finding its sum

#include<iostream.h>
#include<math.h>
using namespace std;
int main( )
{
int x,n,y=0,sum=0;
cout<<"Give x and n for (x^0..x^n) series :";
cin>>x>>n;
for(int tc=1;tc<=n+1;++tc)
{
int term=pow(x,y);
cout<<term<<" ";
sum+=term;
y++;
}
cout<<endl<<"Sum of above series = "<<sum<<endl;
return 0 ;
}

#### Multiple return paths in isPrime() function for prime number series #2449

Using a bool output function isPrime() which has multiple return paths for prime number check and then using this function to print a prime series for given number of terms

#include<iostream>
using namespace std;
bool isPrime(int n)
{
if(n<2) return false;
for(int i=2;i<=n/2;i++)
if(n%i==0) return false;
return true;
}
int main( )
{
int no_of_terms=100;
int term=2;
for(int tc=1;tc<=no_of_terms;++tc)
{
while(!isPrime(term)) term++;
cout<<term++<<" ";
}
return 0 ;
}

#### Simple descending order series #2459

Printing a descending order series – Numbers divisible by 3 from 30 to 1.

#include<iostream>
using namespace std;
int main( )
{
int start=30,upto=1;
for(int i=start;i>=upto;--i)
{
if(i%3==0) cout<<i<<" ";
}
return 0 ;
}

#### Series of triangular numbers #2864

Program to print the triangular series like – 1 3 5 10 15 ……….

#include <iostream>
using namespace std;
int main()
{
int j=1,k=1;
int terms;
cout<<"Give no. of terms :";
cin>>terms;
for(int tc=1;tc<=terms;++tc)
{
cout<<k<<" ";
j+=1;
k+=j;
}
return 0 ;
}
Solved Problems

#### Two previous terms addition series (like Fibonacci) from another number. #2413

Assume that first two terms of a series are 10 and 11. Then use the Fibonacc

#### Odd number series beginning a given number and given term count #2418

Write a program to print odd numbers series up to 20 terms beginning from 13.

#### Series observation, building expression and then printing given terms #2420

Write a program to print the following series up to 15 terms ..
1 8 27

#### Series with continuously modified gaps #2422

Write a program to print the following series up to 15 terms ..
1 4 8

#### Sum of geometric series x+x^2/2+..+x^n/n #2438

Write a program to find the sum of the following series if x is 3 and n is 5

#### Printing and summing series 1/n! form. #2443

Write a program to find the sum of the following series upto 5 terms. Also pr

Code Sheets:11  Solved Problems:6
×