Introduction
Generating number patterns in a triangular form can be a fun and creative way to explore mathematics. It can also be a great way to practice problem-solving skills. In this article, we will discuss how to generate number patterns in a triangular form. We will look at different methods and techniques that can be used to create these patterns. We will also discuss some of the benefits of creating these patterns and how they can be used in various applications.
Solution
//Solution 1
for (let i = 1; i <= 5; i++) {
let output = '';
for (let j = 1; j <= i; j++) {
output += j;
}
console.log(output);
}
//Solution 2
let output = '';
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= i; j++) {
output += j;
}
console.log(output);
output = '';
}
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0"
between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
4
solved How do I generate number pattern in triangular form