Write a C program to print the given even number pattern using loop. How to print the given even number pattern using for loop in C programming. Logic to print the given number pattern in C program.
Example:
Input N: 5
Output:
![Number pattern in C]()
Once you get the logic of above pattern you can easily print the second pattern. You just need to add extra space before printing the numbers.
![Number pattern in C]()
Happy coding ;)
Example:
Input N: 5
Output:
Required knowledge
Basic C programming , LoopLogic to print the given even number pattern
To get the logic of the pattern have a close look on to the pattern. You can observe following things about the pattern.- The pattern consists of total N rows (where N is the total number of rows to be printed).
- To make things little easier let's divide the pattern in two internal parts.
- Loop formation to print the first part of the pattern will be for(j=2; j<=i*2; i+=2).
- Loop formation to print the second part of the pattern will be for(j=(i-1)*2; j>=2; j-=2).
Program to print the given even number pattern
/**
* C program to print the given number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter rows: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Prints first part of the pattern
for(j=2; j<=i*2; j+=2)
{
printf("%d", j);
}
// Prints second part of the pattern
for(j=(i-1)*2; j>=2; j-=2)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
Enter rows: 5
2
242
24642
2468642
2468108642
2
242
24642
2468642
2468108642
Screenshot
Once you get the logic of above pattern you can easily print the second pattern. You just need to add extra space before printing the numbers.
Program to print the above pattern
/**
* C program to print the given number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter rows: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Prints space
for(j=i; j<N; j++)
{
printf("");
}
// Prints first part of the pattern
for(j=2; j<=i*2; j+=2)
{
printf("%d", j);
}
// Prints second part of the pattern
for(j=(i-1)*2; j>=2; j-=2)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Screenshot
Happy coding ;)