CPP All : Nested Loops
Concept Learning Code Sheets

#### Factorial table printing with number and its factorial #2382

Printing a table of numbers followed by its factorial.

#include <iostream>
using namespace std;
int main()
{
int n,i=1;
int f=1;
cout<<"Enter an positive integer :";
cin>>n;
for(int j=1;j<=n;++j)
{
f=1;i=1;
cout<<j<<"t";
while(i<=j)
{
f *=i;
++i;
}
cout<<f<<"n";
}
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 ;
}

#### 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 ;
}
Solved Problems

#### 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:3  Solved Problems:1
×