Nested for Loop to Print a Pattern. Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A nested for loop in R is used when we want to iterate through multiple dimensions, such as rows and columns of a matrix, or for creating patterns. we will discuss how to print multiplication tables up to a certain number with its working example in the R Programming Language using R for loop conditions. Everyone found an easy way for pattern printing in other programming languages. But it's a tough task using the R language Approach:Start the first loop for the number of linesStart the second loop for the item to be displayedPrint itemIncrement the second loop until a condition is reachedIncrement the first loop until the condition is trueContinue in this fashionSyntax:for (i in 1:n) { for (j in 1:m) { # Pattern generation logic }}Example 1: Printing a right-angle triangle R n <- 5 for (i in 1:n) { for (j in 1:i) { cat("* ") } cat("\n") } Output: * * * * * * * * * * * * * * * Initialize n=5, indicating the number of rows in the pattern.The outer loop iterates over the range of i from 1 to n (inclusive), controlling the number of rows in the pattern.Inside the outer loop, the inner loop iterates over the range of j from 1 to the value of i (inclusive). This loop controls the number of asterisks to be printed in each row.Inside the inner loop, cat("* ") prints an asterisk followed by a space, generating the pattern elements.After the inner loop, cat("\n") is used to print a newline character, moving to the next line after printing the pattern for each row.Example 2: Printing a rectangular pattern R n <- 4 m <- 6 for (i in 1:n) { for (j in 1:m) { cat("* ") } cat("\n") } Output: * * * * * * * * * * * * * * * * * * * * * * * * Initialize n=4, indicating the number of rows in the pattern.The outer loop iterates over the range of i from 1 to n (inclusive), controlling the number of rows in the pattern.Inside the outer loop, the inner loop iterates over the range of j from 1 to the value of i (inclusive). This loop controls the number of asterisks to be printed in each row.Inside the inner loop, cat("* ") prints an asterisk followed by a space, generating the pattern elements.After the inner loop, cat("\n") is used to print a newline character, moving to the next line after printing the pattern for each row.Example 3: Draw inverted triangle R star = c() i=1 j=5 while(i<=5){ for(j in 1:j){ star = c(star, "*") } print(star) star = c() i=i+1 j=j-1 } Output: [1] "*" "*" "*" "*" "*"[1] "*" "*" "*" "*"[1] "*" "*" "*"[1] "*" "*"[1] "*"star = c(): Initializes an empty vector star to store the stars for each row.i = 1: Initializes the row counter to 1.j = 5: Initializes the column counter to 5 (this represents the number of stars in the first row).while (i <= 5) { ... }: This is the outer while loop that iterates through 5 rows.for (j in 1:j) { ... }: This is the inner for loop that iterates through the stars within a row. star = c(star, "*"): Adds a star to the current row.star = c(): Clears the star vector to prepare for the next row.i = i + 1: Increments the row counter for the next iteration.j = j - 1: Reduce the number of stars for the next row. Comment More infoAdvertise with us Next Article Pattern of Strings A anjugaeu01 Follow Improve Article Tags : R Language Similar Reads How to Create a Nested For Loop in R? A loop in a programming language is a sequence of instructions executed one after the other unless a final condition is met. Using loops is quite frequent in a program. Need of a loop Let us consider a scenario where we want to print natural numbers from 1 to 3. We can simply print them one by one. 6 min read Print the pattern 2 2 1 1 $2 1 Given the number N, the task is to print a pattern such that in each line all the digits from N to 1 are present in decreasing order and the frequency of the elements in ith line is N-i (the lines are 0 based i.e., i varies in the range [0, N-1]). Note: Instead of printing a new line print a "$" wit 3 min read Bash Scripting - For Loop Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this arti 5 min read Program to Print Inverted Right Half Pyramid Pattern (Star Pattern) Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Ou 3 min read Pattern of Strings Given a string S of length N, find the pattern of the strings as shown below in the examples. Examples: Input: S = "Geek"Output: Geek, Gee, Ge, GExplanation: Decrease one character after each line Input: S = "G*g" Output: G*g, G*, GExplanation: Decrease one character after each line Using two Nested 5 min read Y shaped pattern Print a âYâ shaped pattern from asterisks in N number of lines. Examples: Input: N = 12Output: * * * * * * * * * * * * * * * * * * * Input: 8Output: * * * * * * * * * * * * * Approach: Follow the steps to solve this problem: Initialize two variable s = N / 2 and t = N / 2.Traverse a loop on i from 0 4 min read Like