Create Your First Functional Component in React JS – Beginner's Guide

Image
Create Your First Functional Component in React JS  1. Introduction When you start learning React JS, one of the first things you will come across is the concept of components. At first, components may seem like just another programming concept, but they are actually the foundation of almost every React application. Think about a website you use every day. It may have a header, navigation menu, login form, product section, and footer. Instead of writing all these elements together in one large file, React allows us to create separate parts and manage them independently. These separate parts are called components. In this tutorial, we are going to create our first functional component in React. We will start with a very simple example and gradually understand how components are created, exported, imported, and displayed on a webpage.  2. What Is a Functional Component in React?   A functional component is simply a JavaScript function that returns a user interface using JSX...

C Program to print Hollow Hourglass pattern

   C  Program Hollow Hourglass pattern  

C Program to print Hollow Hourglass pattern


C Program to print Hollow Hourglass pattern 
Example :-


#include <stdio.h>
 
int main()
{
  int i, j, k, rows;
 
  printf("Enter the no. of rows: ");
  scanf("%d", &rows);
 
  printf("Output: \n\n");
 
  for (i = 1; i <= rows; i++)
  {
    for (k = 1; k < i; k++)
      printf(" ");
 
    for (j = i; j <= rows; j++)
      printf("* ");
 
    printf("\n");
  }
 
  for (i = rows - 1; i >= 1; i--)
  {
    for (k = 1; k < i; k++)
      printf(" ");
 
    for (j = i; j <= rows; j++)
      printf("* ");
 
    printf("\n");
  }
 
  return 0;
}


C Code for Printing a Hollow Hourglass Shape

Explain Program

Step :1

Variable Declarations:

int i, j, k, rows;

  • i, j, k: Loop control variables
  • rows: Number of rows (input from the user)

Step :2

 Input from the user:

printf("Enter the no. of rows: ");

scanf("%d", &rows);

  • The user is ask to input the total number of row.

  • example, if rows = 4, the pattern 
  • 4 rows in the upper part
  • 3 rows in the lower part

Step :3

heading:


printf("Output: \n\n");

 heading for clarity.

Step :4

 Upper Part of the Pattern:

for (i = 1; i <= rows; i++)

{

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

        printf(" ");

     for (j = i; j <= rows; j++)

        printf("* ");

    printf("\n");

}

loop prints an inverted right-aligned triangle of stars.


  • i goes from 1 to rows — each iteration is a new row.
  • k prints spaces before the stars. The number of spaces increases each row to push the stars to the right.

        j prints * from the current row number to rows. So, stars decrease in each row

Step :5

Lower Part of the Pattern:

for (i = rows - 1; i >= 1; i--)

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

        printf(" ");

     for (j = i; j <= rows; j++)

        printf("* ");

     printf("\n");

}

 loop prints a right-aligned triangle of stars

This loop prints a right-aligned triangle of stars (opposite of the first).

  • Starts from rows - 1 and goes down to 1.
  • In each row:
  • Prints spaces first (decreasing).
  • Then prints stars (increasing count).


C Program to print Hollow Hourglass pattern 
Output :-


C Program to print Hollow Hourglass pattern


C Program to print Hollow Hourglass pattern 
Summary 

  • The upper triangle shrinks (top-down).
  • The lower triangle grows (bottom-up).
  • Both are right-aligned because of the space handling (printf(" ")).
  • Nested loops (loop inside loop)
  • Controlling spaces and stars to format patterns
  • Symmetry in output (first half decreasing, second half increasing)
  • User input and loops for dynamic patterns

Related Post :-
 

C program to print full pyramid of star

C program to print inverted half

Comments

Popular posts from this blog

HTML Tag

CSS Text Color Explained with Syntax and HTML Examples

CSS Outline Offset Property with Practical Examples