C Pograms On Series and Pattern
C Pograms On Series and Pattern
int main()
{
int i,N,sum;
return 0;
}
Output:
Enter the value of N: 100
Sum of the series is: 5050
C program to find the sum of Natural Number/Factorial of Number of all natural numbers
from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
/*
This program will find the sum of Natural
Number/Factorial of Number of all natural numbers from 1 to N.
*/
#include<stdio.h>
int main()
{
int i,N;
float sum;
return 0;
}
Output:
Enter the value of N: 10
Sum of the series is: 2.718282
void main()
{
int i, n;
float x, sum=0, t=1;
clrscr();
Output :
A Pattern is defined as a design that repeats a specific number of times. That pattern can be decorative
in real life, like, for example, a rangoli.
A pattern program in C helps improve the concept of looping and algorithms. They are easily
displayed with the help of nested loops. Loops can be made using the while or for loop, but writing
programs using for loop is more straightforward than the while loop.
The outer loop is used for rows, and the inner loop is used to display columns (assume Matrix). That
is why the outer loop controls the number of rows, but the inner loop is used for displaying values in
those columns.
A general structure of printing a pattern is as follows:
for (row variable i…..) {
for (column variable j…..) {
//logic of what is to be printed;
}
}
patterns usually consist of a sequence of numbers or any special character that can be displayed in
different structures like a half pyramid, pyramid, square etc.
1
01
101
0101
Logic: On simply observing the pattern, we can see that we would need to use two loops:
Thus, it is easy to formulate the following logic: If we are at row number i and column number j, then
based on the parity of (i+j), we can say:
if (i+j) is even, then print 0
if (i+j) is odd, then print 1
Let us understand this with the help of code:
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if ((i + j) % 2 == 0) printf("1 ");
else printf("0 ");
}
printf("\n");
}
return 0;
}
2. Half pyramid of numbers
1
12
123
1234
12345
This pattern closely resembles the half pyramid we discussed earlier. The contents of each row can be
easily implemented with a single loop.
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = n; i >= 1; --i)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
int main() {
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
//P1 start here
//first we print spaces n-i times
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
//then we print elements starting from i to 2*i-1, which can be observed in each row of the pattern.
for (int j = i; j < 2 * i; j++) {
printf("%d ", j);
}
//P2 starts here
//each row starts with 2*(i-1) and then decrease i-1 times
int ele = 2 * (i - 1);
for (int j = 1; j <= i - 1; j++) {
printf("%d ", ele--);
}
//we finish each row and then print a new line
printf("\n");
}
return 0;
}
5. Floyd's triangle- very important
Pattern:
1
23
456
7 8 9 10
The only trick in this pattern is to keep an extra variable that can be incremented and displayed in
each iteration of the loops. Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int num = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", num);
++num;
}
printf("\n");
}
return 0;
}
Star Patterns
Now, let us move to star patterns. These very closely resemble the number patterns; the only
difference is that we display the asterisk in place of numbers or characters. Let us take a simple
example to understand these.
Examples of Star Patterns:
6. Half pyramid of *
Pattern:
*
**
***
****
*****
All we need to know is the half pyramid structure for displaying such a pattern. Simply two loops
while displaying the star should suffice.
Code:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
7. Inverted half pyramid of *
Pattern:
*****
****
***
**
*
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = n; i >= 1; --i)
{
for (int j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
8. Full pyramid of *
Pattern:
*
***
*****
*******
This is precisely the same as we solved with numbers. This is, in fact, much simpler as instead
of numbers, only the star needs to be displayed.
Code:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i = 1;i <= n; i++)
{
//P1 start here
//first we print spaces n-i times
for(int j = 1; j <= n-i; j++)
{
printf(" ");
}
for(int j = 1; j <2*i-1; j++)
{
printf("* ");
}
//we finish each row and then print a new line
printf("\n");
}
return 0;
}
9. inverted full pyramid of *
Pattern:
*********
*******
*****
***
*
If you understood the previous example, then this one should be quite easy.
Code:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
printf("\n");
}
return 0;
}
Character Patterns
The character patterns may resemble the structure of other patterns we have seen above; however, we
display characters rather than stars or numbers.
The critical thing to remember for such patterns is that we can operate the same way with numbers.
However, we can typecast the integers to character values using ASCII. Let us see two cases below:
For displaying lower case characters - (a to z) The ASCII values of lowercase characters range
from 97 (a) to 122 (z), thus in order to display them, we can typecast the integer values
to character ones using the method below:
for(int i = 0; i < 26; i++)
{
printf("%c ",(char)(i + 97));
}
Output:
abcdefghijklmnopqrstuvwxyz
For displaying upper case characters - (A to Z) The ASCII values of lowercase characters range
from 65 (A) to 90 (Z), thus in order to display them, we can typecast the integer values
to character ones using the method below:
for(int i = 0; i< 26; i++)
{
printf("%c ",(char)(i + 65));
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Now with this information, let us try to approach the following pattern:
A
bC
DeF
gHiJ
KlMnO
We can observe that this is the half-pyramid structure, where we need to keep an extra variable for
displaying the content of the rows similar to the pattern showing Floyd's Triangle. Here, the parity of
the row and column sum decides whether the character will be lowercase or uppercase.
Code:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int k = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if((i + j) % 2 == 1)
{
printf("%c ",(char)(k + 97));
}
else
{
printf("%c ",(char)(k + 65));
}
k++;
}
printf("\n");
}
return 0;
}
Example of Character Pattern
1. Half pyramid of alphabets
Pattern:
A
BB
CCC
DDDD
EEEEE
This is quite a standard pattern, where 2 loops would suffice.
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("%c ",(char)(i + 64));
}
printf("\n");
}
return 0;
}
A
bC
DeF
gHiJ
KlMnO
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int k = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if((i + j) % 2 == 1)
{
printf("%c ",(char)(k + 97));
}
else
{
printf("%c ",(char)(k + 65));
}
k++;
}
printf("\n");
}
return 0;
}