0% found this document useful (0 votes)
34 views10 pages

C Pograms On Series and Pattern

The document discusses various types of patterns that can be generated using C programs, including number patterns, star patterns, and character patterns. It provides the logic and code to generate common patterns like half and full pyramids of numbers and stars. Character patterns are also briefly covered, explaining how to typecast integers to ASCII characters to generate letter patterns. Examples of patterns discussed include half and inverted pyramids of numbers from 1 to N, Floyd's triangle, and letter patterns from A to Z. The document emphasizes that pattern programs help improve looping concepts in C.

Uploaded by

Sakshi Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

C Pograms On Series and Pattern

The document discusses various types of patterns that can be generated using C programs, including number patterns, star patterns, and character patterns. It provides the logic and code to generate common patterns like half and full pyramids of numbers and stars. Character patterns are also briefly covered, explaining how to typecast integers to ASCII characters to generate letter patterns. Examples of patterns discussed include half and inverted pyramids of numbers from 1 to N, Floyd's triangle, and letter patterns from A to Z. The document emphasizes that pattern programs help improve looping concepts in C.

Uploaded by

Sakshi Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Practice problem on series:

C program to find the sum of series 1^2 + 2^2 + …. + n^2 –


c program to print series 0,3,8,15,24,35,48,63,80,99.
c program to print series 0, 1, 3, 6, 10, 15… N.

C program to find sum of all natural numbers.


Series: 1+2+3+4+..N
#include<stdio.h>

int main()
{
int i,N,sum;

/*read value of N*/


printf("Enter the value of N: ");
scanf("%d",&N);

/*set sum by 0*/


sum=0;

/*calculate sum of the series*/


for(i=1;i<=N;i++)
sum= sum+ i;

/*print the sum*/

printf("Sum of the series is: %d\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;

/*read value of N*/


printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0.0f;

/*calculate sum of the series*/


for(i=1;i<=N;i++)
{
fact=fact*i; //factorial of i
sum = sum +( i / (float)fact);
{

/*print the sum*/

printf("Sum of the series is: %f\n",sum);

return 0;
}
Output:
Enter the value of N: 10
Sum of the series is: 2.718282

C program for Exponential Series


Exponential Series:
Exponential Series is a series which is used to find the value of ex.
The formula used to express the ex as Exponential Series is

Expanding the above notation, the formula of Exponential Series is

void main()
{
int i, n;
float x, sum=0, t=1;
clrscr();

printf("Enter the value for x : ");


scanf("%f", &x);

printf("\nEnter the value for n : ");


scanf("%d", &n);

/* Loop to calculate the value of Exponential */


for(i=1;i<n;i++)
{
t=t*x/i;
sum=sum+t;
}

printf("\nThe Exponential Value of %f = %.4f", x,1+sum);


getch();
}

C program to find sum of series


// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
#include <math.h>
#include <stdio.h>
int main()
{
int x, n, i;
float total=0.0;
printf("enter the value of x ");
scanf("%d",&x);
printf("enter the value of n ");
scanf("%d",&n);
for (i = 1; i < n; i++)
{
total = total + (pow(x, i) / (float)i);
}
printf("%.2f", 1.0+total);
return 0;
}

Output :

Practice problem on patterns:

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. Half pyramid of numbers 0 and 1

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;
}

3. Inverted half pyramid of numbers


Pattern:
12345
1234
123
12
1
This pattern deals with an inverted half-pyramid. The concept remains the same; just the outer loop
runs in the reverse order since the size of each row decrease from n to 1.
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("%d ", j);
}
printf("\n");
}
return 0;
}

4. Full pyramid of numbers


Pattern:
1
232
34543
4567654
567898765
This pattern helps us deal with a new structure, i.e., a full pyramid. There are many ways to formulate
a strategy to solve such a pattern however, let us see a relatively simple strategy: This is the first
pattern where we need to use spaces(represented as hyphens in the image below). Let us divide the
pattern into two different patterns:
Now we can start writing the 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(" ");
}
//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);

for (int i = n; i >= 1; --i) {

for (int j = 0; j < n - i; ++j)


printf(" ");

for (int j = i; j <= 2 * i - 1; ++j)


printf("* ");

for (int j = 0; j < i - 1; ++j)


printf("* ");

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;
}

You might also like