Quantcast
Channel: Codeforwin
Viewing all 132 articles
Browse latest View live

Number pattern 23 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern in C program using loop.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Logic to this pattern is pretty simple; to understand the logic, first have a careful eye on to the pattern for a minute and think the logic. You can observe that there are N number of rows (where N is the total number of rows to be printed). Each row exactly contains i number of columns (where i is the current row number). And for each row in each column j gets printed (where j is the current column number).
The step-by-step descriptive logic is:
  1. To iterate through rows, run an outer loop from 1 to N.
  2. To print the number, run an inner loop from 1 to i (where i is the current row number). Inside this loop print the value of j (where j is the current column number).
Lets implement this logic.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=1; j<=i; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
12
123
1234
12345


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

The above pattern is very much similar to the first pattern we just printed. We only need to add logic to print trailing spaces that should be printed before the number gets printed.
If you hover your mouse on to the pattern you can see or count total spaces per row and can also think of logic to print the spaces. If you can notice, there are exactly N - i spaces per row (where N is the total number of rows to be printed and i is the current row number.
The step-by-step descriptive logic to print spaces is:
  1. To print spaces, run an inner loop from 1 to N - i. Inside this loop print single blank space.
Lets now code this.

Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<=N-i; j++)
{
printf("");
}

//Logic to print numbers
for(j=1; j<=i; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)



Number pattern 24 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

To understand the logic of given pattern I recommend you to have a careful look of the pattern for a minute. Now talking about the pattern, there are N number of rows (where N is the total number of rows to be printed). Each row contains exactly N - i + 1 number of columns (where i is the current row number). And for each row in each column the value of j gets printed (where j is the current column number).
The step-by-step descriptive logic of the given pattern:
  1. To iterate through rows, start an outer loop from 1 to N.
  2. To print the numbers, start an inner loop from 1 to N - i + 1. Inside this loop print the value of j.
Lets now code the solution.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=1; j<=N-i+1; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
12345
1234
123
12
1


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

Once you got the logic of first number pattern that we just printed, you can easily get the logic of this pattern. As it is almost similar to the first number pattern expect that it contains trailing spaces. Hence we only need to add the logic of printing trailing spaces to the first number pattern program.
To get the logic of this pattern just hover your mouse on the above pattern to see or count total spaces per row. There are total i - 1 spaces per row (where i is the current row number).
The step-by-step descriptive logic to print space:
  1. To print space, run an inner loop from 1 to i - 1. Inside this loop print single blank space.
Only this much you need to do. Lets implement this on code.

Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=1; j<=N-i+1; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 25 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

To get the logic of the given pattern look to the pattern carefully for a minute. If you have noticed the pattern consists of N rows (where N is the total number of rows to be printed). Each row contains exactly i columns (where i is the current row number). Since the columns are printed in descending order hence, you must run the loop from i to 1 and print the value of j (where j is the current column number).

Step-by-step descriptive logic of the pattern:
  1. To iterate through rows, run an outer loop from 1 to N (where N is the total number of rows to be printed).
  2. To print the columns, run an inner loop from i to 1 in decreasing order (where i is the current row number). Since the values printed per row is in decreasing order hence we have used the loop from N-1 otherwise we can also use the loop from 1-N.
    Inside this loop print the value of j (where j is the current column number).
Lets now implement this logic.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=i; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
21
321
4321
54321


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

Logic of the above pattern is simple and similar to the first number pattern that we just printed. In this pattern we only need to add logic of printing extra trailing spaces before the number gets printed. You can hover on to the pattern to see or count total spaces printed per row. The total number of spaces printed per row is N - i (where N is the total number of rows to be printed and i is the current row number).
Step-by-step descriptive logic of the printing spaces:
  1. To print spaces, run an inner loop from 1 to N - i. Inside this loop print single blank space.
Finally you are done, lets now implement this on code.

Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<=N-i; j++)
{
printf("");
}

//Logic to print numbers
for(j=i; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 26 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

The above pattern consists of N rows (where N is the total rows to be printed). To since the pattern is in descending order hence, to make things easier we will iterate through rows from N-1 instead of 1-N so now the first row is row5, second row is row4 and last row is row1. Each row contains exactly i columns (where i is the current row number).
The step-by-step descriptive logic of the pattern is:
  1. To iterate through rows, run an outer loop from N to 1 in decreasing order.
  2. To print the columns, run an inner loop from i to 1 in decreasing order. Inside this loop print the value of j (where j is the current column number).
Lets, code it.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print numbers
for(j=i; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
54321
4321
321
21
1


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

If you look to the above pattern you will find that the logic to print the numbers are similar as the first pattern. Hence, we only need to write the logic to print the trailing spaces that gets printed before the number. You can hover on to the pattern to see or count the number of spaces per row. Total number of spaces per row is N - i (where N is the total number of rows and i is the current row number). Note that i loops in decreasing order i.e. at row1 i=5, at row2 i=4 and so on.
Step-by-step descriptive logic to print spaces:
  1. To print spaces, run an inner loop from i to N - 1. Inside this loop print the value of j (where j is the current column number).
Lets implement the logic.

Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print spaces
for(j=i; j<=N-1; j++)
{
printf("");
}

//Logic to print numbers
for(j=i; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 27 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Before I get into details about the number pattern I recommend you to have a close eye to the pattern carefully for a minute. The above pattern contains N rows (where N is the total number of rows to be printed). As you can see that the pattern printing is in descending order hence to make things easier we will iterate through the outer loop in descending order. Each row contains exactly i number of columns (where i is the current row number).
Step-by-step description to print the pattern:
  1. To iterate through rows, run an outer loop from N to 1. Note - Since the pattern is printed in descending order therefore we have initialized the loop from N and iterate till 1.
  2. To print the pattern, run an inner loop from N to i (where i is the current row number). Inside this loop print the value of j (where j is the current column number).
Lets implement this logic on code.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print numbers
for(j=N; j>=i; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
5
54
543
5432
54321


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

If you are done with first pattern, then logic to this wouldn't be much difficult to get. This pattern is almost similar to the first pattern we just printed, except trailing spaces before the number. Hence, logic to print the pattern will be same as the first pattern, we only need to add the logic of printing spaces. You can hover your mouse cursor to the pattern to see or count the number of space. The pattern consists of i - 1 spaces per row (where i is the current row number). Note that in the given pattern we have assumed that row numbers are ordered descending from N-1.
Step-by-step descriptive logic to print spaces:
  1. To print spaces, run an inner loop from 1 to i - 1. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=N; j>=i; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 28 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern program using for loop in C programming. Logic to print the given number pattern program using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

To get the logic of above pattern have a look to the pattern carefully for a couple of minutes. The pattern consists of N rows (where N is the total number of rows to be printed). Each row contains exactly N - i + 1 columns (where i is the current row number).
Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to N.
  2. To print columns, run an inner loop from N to i in decreasing order (where i is the current row number). Inside this loop print the value of j (where j is the current column number).


Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=N; j>=i; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
54321
5432
543
54
5


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

Once you get done with the first number pattern, then this wouldn't be much difficult as it is same as the pattern we just did except of trailing spaces. We only need to add the logic of printing spaces just before the number gets printed. If you hover mouse to the pattern above you can see or count total number of spaces per row and can easily think about the logic to print spaces. Here spaces are arranged in ascending order fashion i.e. first row contains 0 spaces, second row contains 1 space and so on last row contains 4 space. Total number of spaces per row is i - 1 (where i is the current row number).
Step-by-step descriptive logic to print spaces:
  1. To print space, run an inner loop from 1 to i - 1. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=N; j>=i; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 29 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

In the above pattern there are N rows (where N is the total number of rows to be printed). As you know that to iterate N times, you can either go from 1-N or from N-1. Here in our case we will iterate though rows from N-1, since the pattern printed is in descending order. Each row contains exactly N - i + 1 columns (where i is the current row number).
Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from N to 1 in decreasing order.
  2. To print columns, run an inner loop from i to N (where i is the current row number). Inside this loop print the value of j (where j is the current column number).


Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
5
45
345
2345
12345


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

If you look to the above pattern you will find that it is same as the pattern we just printed above except of trailing spaces. Hence, the whole logic of printing the pattern will be same as first pattern, we only need to add the logic to print spaces. If you hover mouse on to the pattern you can see or count total spaces per row. In the given pattern each row contains i - 1 spaces (where i is the current row number). Note that row are in descending order i.e. row1 is 5, row2 is 4 and so on.
Step-by-step descriptive logic:
  1. To print spaces, run an inner loop from 1 to i. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=N; i>=1; i--)
{
//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 30 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming , Loop

Logic to print the given number pattern 1

Before we get into this pattern I recommend you to look to the given pattern carefully for a minute. The pattern contains N rows (where N is the total rows to be printed). Each row contains exactly N - i + 1 columns (where i is the current row number).
Step-by-step descriptive logic:
  1. To iterate though rows, run an outer loop from 1 to N.
  2. To print columns, run an inner loop from i to N. Inside this loop print the value of j (where j is the current column number).


Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", j);
}

printf("\n");
}
return 0;
}


Output
Enter N: 5
12345
2345
345
45
5


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

The above pattern is similar to pattern we just printed except the trailing spaces. The logic of this pattern would be similar as first pattern we did, we only need to add logic to print trailing spaces before printing numbers. There are i - 1 spaces per row (where i is the current row number). To see or count total spaces per row you can hover mouse pointer to the pattern above.
Step-by-step descriptive logic:
  1. To print spaces, run an inner loop from 1 to i - 1 (where i is the current row number). Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)



Number pattern 31 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Before I discuss logic to print the given number pattern, I recommend you that have a close look to the pattern carefully for a couple of minutes. The above pattern consists of N rows (where N is the total number of rows to be printed). Each row contains i columns (where i is the current row number). To print the above pattern we will be using an extra variable say k that will keep the value which we need to print.
Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to N.
  2. Inside the outer loop initialize k = i (where k is an extra variable which will hold the number which we need to print next and i is the current row number).
  3. To print the numbers, run an inner loop from 1 to i. Inside this loop print the value of k. Also increment the value of k after printing.
And you are done, lets implement this.

Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=1; i<=N; i++)
{
k = i;

//Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
23
345
4567
56789


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

If you look to the pattern you will notice that the pattern is similar to the pattern we just did except the trailing spaces. The logic to print the given pattern would be similar as the first pattern we just did. We only need to add the logic of printing spaces before the number gets printed. If you can notice, there are N - i spaces per row (where i is the current row number).
Step-by-step description to print spaces:
  1. To print spaces, run an inner loop from i to N - 1. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=1; i<=N; i++)
{
k = i;

//Logic to print spaces
for(j=i; j<N; j++)
{
printf("");
}

//Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern programs in C

$
0
0
Number patterns are logical programs that basically are used to enhance your logical thinking abilities. These patterns are patterns created by numbers and are similar to star patterns. Enhance your logical thinking abilities by coding these patterns. Here is a list of basic, advanced, tricky and popular number patterns with logic and explanation.
Pascal triangle


11111
11111
11111
11111
11111
Number pattern 1


11111
00000
11111
00000
11111
Number pattern 2

01010
01010
01010
01010
01010
Number pattern 3



11111
10001
10001
10001
11111
Number pattern 4

11111
1 1
1 1
1 1
11111
Number pattern 5



11111
11111
11011
11111
11111
Number pattern 6

10101
01010
10101
01010
10101
Number pattern 7


11011
11011
00000
11011
11011
Number pattern 8

10001
01010
00100
01010
10001
Number pattern 9



11111
22222
33333
44444
55555
Number pattern 10

12345
12345
12345
12345
12345
Number pattern 11



12345
23456
34567
45678
56789
Number pattern 12

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Number pattern 13



12345
23455
34555
45555
55555
Number pattern 14

55555
45555
34555
23455
12345
Number pattern 15



12345
23451
34521
45321
54321
Number pattern 16



12345
21234
32123
43212
54321
Number pattern 17

54321
43212
32123
21234
12345
Number pattern 18



55555
54444
54333
54322
54321
Number pattern 19

55555
44445
33345
22345
12345
Number pattern 20


54321
54322
54333
54444
55555
Number pattern 21

12345
22345
33345
44445
55555
Number pattern 22



555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
Number pattern 23

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Number pattern 24



1
22
333
4444
55555
Number pattern 25

1
22
333
4444
55555
Number pattern 26


55555
4444
333
22
1
Number pattern 27

55555
4444
333
22
1
Number pattern 28



11111
2222
333
44
5
Number pattern 29

11111
2222
333
44
5
Number pattern 30


5
44
333
2222
11111
Number pattern 31

5
44
333
2222
11111
Number pattern 32



1
12
123
1234
12345
Number pattern 33

1
12
123
1234
12345
Number pattern 34


12345
1234
123
12
1
Number pattern 35

12345
1234
123
12
1
Number pattern 36



1
21
321
4321
54321
Number pattern 37

1
21
321
4321
54321
Number pattern 38


54321
4321
321
21
1
Number pattern 39

54321
4321
321
21
1
Number pattern 40



5
54
543
5432
54321
Number pattern 41

5
54
543
5432
54321
Number pattern 42


54321
5432
543
54
5
Number pattern 43

54321
5432
543
54
5
Number pattern 44



5
45
345
2345
12345
Number pattern 45

5
45
345
2345
12345
Number pattern 46


12345
2345
345
45
5
Number pattern 47

12345
2345
345
45
5
Number pattern 48



1
23
345
4567
56789
Number pattern 49

1
23
345
4567
56789
Number pattern 50


56789
4567
345
23
1
Number pattern 51

56789
4567
345
23
1
Number pattern 52



13579
3579
579
79
9
Number pattern 53

13579
3579
579
79
9
Number pattern 54



1
123
12345
1234567
123456789
Number pattern 55

1
123
12345
1234567
123456789
Number pattern 56



1
24
135
2468
13579
Number pattern 57



1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Number pattern 58


Happy coding ;)


Number pattern 32 in C

$
0
0
Write a C program to print the given number using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

The above pattern consists of N rows (where N is the total rows to be printed). Now to iterate N times you can either go from 1-N or from N-1. Here in our case we will go from N-1 as the pattern printed is in decreasing order. Each row contains i columns (where i is the current row number). Note that the first row is row 5, second is 4 and so on last row is 1.
Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from N to 1 in decreasing order.
  2. Inside the outer loop initialize another variable say k = i (where k is used to keep the value which is to be printed next).
  3. To print columns, run an outer loop from 1 to i. Inside this loop print the value of k after that increment the value of k to get next value.


Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=N; i>=1; i--)
{
k = i;

//Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
56789
4567
345
23
1


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

The logic to print the above pattern is almost similar to the pattern we just printed. In this pattern we only need to add logic of printing spaces just before number gets printed. There are N - i spaces per row (where i is the current row number). If you want to see or count spaces, you can hover on to the pattern.
Step-by-step descriptive logic:
  1. To print spaces, run an inner loop from i to N - 1. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=N; i>=1; i--)
{
k = i;

//Logic to print spaces
for(j=i; j<N; j++)
{
printf("");
}

//Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


C program to print odd number pattern

$
0
0
Write a C program to print odd number pattern using loop. How to print the given odd number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example:


Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

If you look to the pattern you will find that the pattern only consists of odd numbers. There are N rows (where N is the total number of rows to be printed). As you can see that each row contains exactly N - i + 1 columns (where i is the current row number).
For printing the numbers we will be using an extra variable lets say k that will keep track of next odd number. For each row odd number starts with the expression i * 2 - 1.

Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to N.
  2. Inside this outer loop, initialize variable k = i * 2 - 1 (where k is used to keep track of next odd number to be printed).
  3. To iterate though columns, run an inner loop from i to N (where i is the current row number).
    Inside this loop print the value of k and increment it to k = k + 2.
And finally you are done, lets code it down.

Program to print the odd number pattern


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=1; i<=N; i++)
{
k = (i * 2) - 1;

//Logic to print numbers
for(j=i; j<=N; j++, k+=2)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
13579
3579
579
79
9


Screenshot 1

C program to print odd number pattern


Logic to print the odd number pattern with spaces

The above pattern is similar to the first pattern we just printed above except the trailing spaces printed before the numbers. The logic for printing numbers will be similar as first pattern we printed, we only need to add logic of printing spaces. As you can see that each row contains exactly i - 1 spaces (where i is the current row number). You can also hover on to the pattern to see or count total spaces per row.

Step-by-step descriptive logic to print spaces:
  1. To print spaces, run an inner loop from 1 to i - 1. Inside this loop print single blank space.


Program to print the odd number pattern with spaces


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=1; i<=N; i++)
{
k = (i * 2) - 1;

//Logic to print spaces
for(j=1; j<i; j++)
{
printf("");
}

//Logic to print numbers
for(j=i; j<=N; j++, k+=2)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print odd number pattern


Happy coding ;)


Number pattern 34 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Before I discuss about the logic of the given pattern I would recommend you to look to the pattern carefully for a minute to make things easier.
Now, talking about the pattern it consists of N rows (where N is the total number of rows to be printed). Each row consists of exactly i * 2 - 1 columns (where i is the current row number).

Step-by-step descriptive logic:
  1. To iterate though rows, run an outer loop from 1 to N.
  2. To print columns, run an inner loop from 1 to i * 2 - 1 (where i is the current row number). Inside this loop print the value of j (where j is the current column number).


Program to print the given number pattern 1


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=1; j<=(i*2-1); j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
123
12345
1234567
123456789


Screenshot 1

C program to print number pattern


Logic to print the given number pattern 2

If you look to the pattern, you will find that above pattern is almost similar to the first pattern we just printed. The logic to print numbers will be same as first pattern we did, we only need to add the logic of printing spaces. To see or count total number of spaces per row you can hover on to the pattern. There are (N - i) * 2 spaces each row (where i is the current row number), as the first row contains (5 - 1) * 2 = 8 spaces, and second contains 6 spaces and so on last row contains 0 spaces.

Step-by-step descriptive logic:
  1. To print spaces, run an inner loop from 1 to (N - i) * 2. Inside this loop print single blank space.


Program to print the given number pattern 2


/**
* C program to print number pattern
*/

#include <stdio.h>

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

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

for(i=1; i<=N; i++)
{
//Logic to print spaces
for(j=1; j<= ((N-i)*2); j++)
{
printf("");
}

//Logic to print numbers
for(j=1; j<=(i*2-1); j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot 2

C program to print number pattern


Happy coding ;)


Number pattern 35 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, Loop

Logic to print the given number pattern

Logic to this pattern can be little tricky on first look. The pattern contains N rows (where N is the total number of rows to be printed). There are exactly i columns per row (where i is the current row number). Now, when you look to the pattern carefully you will find that the pattern contains odd and even columns.

If you don't know how to check even or odd, it is recommended that learn the logic of checking even or odd before continuing to this pattern.

When the row number is odd then the columns are odd, starting from first odd number and when it is even then the columns are even, starting from the first even number. For printing the numbers we will use an extra variable say k that will keep track of next number to be printed.

Step-by-step-descriptive logic:

  1. To iterate through rows, run an outer loop from 1 to N.
  2. Inside this loop initialize a variable k = 1 if the current row is odd otherwise k = 2.
  3. To iterate through columns, run an inner loop from 1 to i (where i is current row number).
    Inside this loop print the value of k and increment k = k + 2 to get the next even or odd number.


Program to print the given number pattern


/**
* C program to print number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

for(i=1; i<=N; i++)
{
//Checking even or odd
if(i&1)
k = 1;
else
k = 2;

//Logic to print numbers
for(j=1; j<=i; j++, k+=2)
{
printf("%d", k);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
24
135
2468
13579


Screenshot

C program to print number pattern


Happy coding ;)


C program to print Floyd's triangle number pattern

$
0
0
Write a C program to print the Floyd's triangle number pattern using loop. How to print the Floyd's triangle number pattern using for loop in C programming. Logic to print the Floyd's triangle number pattern using loop in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the Floyd's triangle number pattern

Printing the above pattern is easy if you are acquainted with basics of number pattern printing. The given pattern contains N rows (where N is the total number of rows to be printed). Each row contains exactly i columns (where i is the current row number).

Now, you have understood the looping constructs of the given pattern. As you can see that printed numbers are in increasing order, hence to print numbers we will use an extra variable k which will keep track of the next number to be printed.

Step-by-step descriptive logic:

  1. Initialize a variable k = 1 (where k is used to keep track of next number to be printed).
  2. To iterate through rows, run an outer loop from 1 to N.
  3. To print columns, run an inner loop from 1 to i (where i is the current row number).
    Inside this loop print the value of k also increment the value of k to k = k + 1.


Program to print the Floyd's triangle number pattern


/**
* C program to print floyd number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

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

k = 1;

for(i=1; i<=N; i++)
{
//Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%3d", k);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15


Screenshot

C program to print number pattern


Happy coding ;)



C program to print heart star pattern with name in center

$
0
0
Write a C program to print heart star pattern with name in the center. How to print heart star pattern with the name in center using for loop in C programming. Program to print the below heart star pattern.


***** *****
******* *******
********* *********
*****Codeforwin****
*****************
***************
*************
***********
*********
*******
*****
***
*

Required knowledge

Basic C programming, If else, Loop

Program to print heart star pattern with name in center


/**
* C program to print heart star pattern with center name
*/

#include <stdio.h>
#include <string.h>

int main()
{
int i, j, n;
char name[50];
int len;

printf("Enter your name: ");
gets(name);

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

len = strlen(name);

//Prints upper part of the heart shape
for(i=n/2; i<=n; i+=2)
{
for(j=1; j<n-i; j+=2)
{
printf("");
}

for(j=1; j<=i; j++)
{
printf("*");
}

for(j=1; j<=n-i; j++)
{
printf("");
}

for(j=1; j<=i; j++)
{
printf("*");
}

printf("\n");
}

//Prints lower triangular part of the pattern
for(i=n; i>=1; i--)
{
for(j=i; j<n; j++)
{
printf("");
}

//Prints the name
if(i == n)
{
for(j=1; j<=(n * 2-len)/2; j++)
{
printf("*");
}

printf("%s", name);

for(j=1; j<(n*2-len)/2; j++)
{
printf("*");
}
}
else
{
for(j=1; j<=(i*2)-1; j++)
{
printf("*");
}
}

printf("\n");
}

return 0;
}


Snapshot

C program to print heart star pattern with name in center

Happy coding ;)


Number pattern 36 in C

$
0
0
Write a C program to print the given number pattern. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print given pattern

Before I discuss the logic to print the given pattern I recommend you to have a careful eye on to the pattern. Following observations can be made about the pattern.
  1. It contains of N rows (where N is the total rows to be printed).
  2. Each row contains exactly double column as in previous row. And the first row contains 1 column.
  3. As per each column contains an integer from 1 to 9 in increasing order. When the number count reaches 10 it restarts to 1.
Below is the program that converts the above logic to computer code to print the given pattern.

Program to print the given pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N, colCount, value;

colCount = 1;
value = 1;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=1; j<=colCount; j++)
{
if(value == 10)
value = 1; //Restart at 10

printf("%d ", value);

value++;
}

printf("\n");

//Increases the total number of columns by 2
colCount *= 2;
}

return 0;
}


Output
Enter rows: 5
1
2 3
4 5 6 7
8 9 1 2 3 4 5 6
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4


Screenshot

Number pattern 36 in C


Happy coding ;)


Number pattern 37 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern through C program.

Example:
Input N: 5


Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

Before solving the given pattern have a close look to the pattern. You will find that the pattern internally can be divided in two patterns which can be shown below.

Now, what we need to do is combine the logic to print both these two patterns in single program. Logic of print both these patterns are simple and are explained in previous posts separately. You can check out these two posts to get the logic of printing first pattern and second pattern if you are unfamiliar with these two patterns.
Let's combine them into single pattern.

Program to print the given 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 the first part of the pattern
for(j=1; j<=i; j++)
{
printf("%d", j);
}

// Prints the second part of the pattern
for(j=i-1; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
121
12321
1234321
123454321


Screenshot

Number pattern program in C


Happy coding ;)


Number pattern 38 in C

$
0
0
Write a C program to print the given number pattern series. How to print the given number pattern series using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N: 5
Output:


Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

If at first the pattern seems confusing. Take your time and have a closer look to the pattern for a minute. You might find below things about the pattern.
  1. It contains of total N rows (where N is the total number of rows to be printed).
  2. To make things easier you can divide the pattern in two parts.
  3. Now, when you look to the first part of the pattern, you will find that it starts from 3rd row and goes till 5th row. Hence the inner loop formation to print this part will be something like for(j=3; j<=i; j++).
  4. The second pattern is an odd pattern, whose first column starts with an odd number. I have already explained the logic to print this pattern in one of my previous post.
    It contains total N rows and each row contains exactly i columns (where i is the current row number. Each column start with a value (i * 2) - 1. Hence the second inner loop formation for this part will be for(j=(i * 2)-1; j>=i; j--).
Lets, code down the logic.

Program to print the given pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, value, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
value = i + 1;
// Prints the first part of pattern
for(j=3; j<=i; j++)
{
printf("%d ", value);
value++;
}

// Prints the second part of pattern
for(j=(i*2)-1; j>=i; j--)
{
printf("%d ", j);
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
3 2
4 5 4 3
5 6 7 6 5 4
6 7 8 9 8 7 6 5


Screenshot

Number pattern program in C


Happy coding ;)


Number pattern 39 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern through C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

When you look to the pattern, it might look similar to the odd number pattern. Logic to the pattern would be very simple if you know how to print odd number pattern.

Now, lets have a quick look on to the pattern. The following observations can be made about the pattern.
  1. It consists of N rows (where N is the total number of rows to be printed).
  2. To make things little easier you can internally divide the pattern in two parts.
  3. The first part is completely odd number pattern and the loop structure to print the given pattern is for(j=1; j<=(i*2) - 1; j++).
  4. The second part is also an odd pattern in decreasing order. The loop structure to print the second part will be for(j=(i-1)*2-1; j>=1; j--).
Now, what we need to do is combine the logic of both programs into single program.

Program to print the given 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 the first part of pattern
for(j=1; j<=(i*2)-1; j+=2)
{
printf("%d", j);
}

// Prints the second part of pattern
for(j=(i-1)*2-1; j>=1; j-=2)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
131
13531
1357531
135797531


Screenshot

Number pattern program in C


Now, once you got the logic to print this pattern you can easily print the second pattern. You just need to add extra spaces at the beginning.

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=1; j<=(i*2)-1; j+=2)
{
printf("%d", j);
}

// Prints second part of the pattern
for(j=(i-1)*2-1; j>=1; j-=2)
{
printf("%d", j);
}

printf("\n");
}

scanf("%d", &i);

return 0;
}


Screenshot

Number pattern program in C


Happy coding ;)


Viewing all 132 articles
Browse latest View live