Print Rhombus Star Pattern
PRINTING PATTERN:
****
****
****
****
PREREQUISITE:
Basic knowledge of C language and use of loops.
ALGORITHM
- Take the number of rows as input from the user (length of side of rhombus) and store it in any variable.(‘l‘ in this case).
- Run a loop ‘l’ number of times to iterate through each of the rows. From i=0 to i<l. The loop should be structured as for( i=0 ; i<l : i++).
- Run a nested loop inside the main loop to print the spaces before the rhombus. From j=0 to j<i. The loop should be structured as for( j=0; j<i ; j++).
- Run another nested loop inside the main loop after the previous loop to print the stars in each column of a row. From j=0 to j<l. The loop should be structured as for( j=0 ; j<l ; j++).
- Move to the next line by printing a new line . printf(“/n”).
Code in C:
#include<stdio.h> int main() { int i,j,l; //declaring integer variables i,j for loops and l for number of rows/columns printf("enter number of rows/columns\n"); //asking the user for input scanf("%d",&l); //taking input from the user for(i=0;i<l;i++) //loop controlling number of rows { for(j=0;j<i;j++) //inner loop for spaces { printf(" "); } for(j=0;j<l;j++) //inner loop for printing the stars in each column of a row { printf("*"); } printf("\n"); // printing a new line after each row } }
TAKING INPUT:
DISPLAYING OUTPUT:
Login/Signup to comment
Code in JAVA:
import java.util.*;
public class Star3 {
public static void main(String arg[])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=0; i<n; i++)
{
for(int k=0; k<i; k++)
{
System.out.print(" ");
}
for(int j=1; j<n; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
#include
int main()
{
int r,count=0;
scanf(“%d”,&r);
for(int i=0;i<r;i++)
{
for(int j=0;j<r;j++)
{
printf("*");
}printf("\n");
count++;
int temp=count;
while(temp!=0)
{
printf(" ");
temp–;
}
}
}
java solution//
public class Main {
public static void main(String[] args) {
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print(" ");
}
for(int j=1;j<=4;j++){
System.out.print("*");
}
System.out.println();
}
}
}