C program to Print Sandglass Number Pattern

Write a C program to print sandglass number pattern using for loop. 

#include <stdio.h>

int main()
{
    int i, j, k, rows;
    
    printf("Enter Sandglass Number Pattern Rows = ");
    scanf("%d",&rows);

    printf("The Sandglass Number Pattern\n"); 

	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = 1; j < i; j++ ) 	
		{
			printf(" ");
		}
		for (k = i; k <= rows; k++ ) 	
		{
			printf("%d ", k);
		}
		printf("\n");
	}
    
    for (i = rows - 1; i >= 1; i-- ) 
	{
		for (j = 1; j < i; j++ ) 	
		{
			printf(" ");
		}
		for (k = i; k <= rows; k++ ) 	
		{
			printf("%d ", k);
		}
		printf("\n");
	}
		
    return 0;
}
C program to Print Sandglass Number Pattern

This C example prints the numbers in a sandglass pattern using a while loop.

#include <stdio.h>

int main()
{
    int i = 1, j, k, rows;
    
    printf("Enter Sandglass Number Pattern Rows = ");
    scanf("%d",&rows);

    printf("\nThe Sandglass Number Pattern\n"); 

	while( i <= rows)
    {
    	j = 1;
    	while( j < i)	
		{
			printf(" ");
		    j++;
        }

        k = i;
        while( k <= rows )
        {
			printf("%d ", k);
			k++;
		}
		printf("\n");
		i++;
	}
    
    i = rows - 1;
    while( i >= 1)
    {
        j = 1;
    	while( j < i)
		{
			printf(" ");
		    j++;
        }

        k = i;
        while( k <= rows )
        {
			printf("%d ", k);
		    k++;
        }
		printf("\n");
		i--;
	}
		
    return 0;
}
Enter Sandglass Number Pattern Rows = 7

The Sandglass Number Pattern
1 2 3 4 5 6 7 
 2 3 4 5 6 7 
  3 4 5 6 7 
   4 5 6 7 
    5 6 7 
     6 7 
      7 
     6 7 
    5 6 7 
   4 5 6 7 
  3 4 5 6 7 
 2 3 4 5 6 7 
1 2 3 4 5 6 7

C program to display the sandglass number pattern using a do while loop.

#include <stdio.h>

int main()
{
    int i = 1, j, k, rows;
    
    printf("Enter Sandglass Number Pattern Rows = ");
    scanf("%d",&rows);

    printf("\nThe Sandglass Number Pattern\n"); 

	do
    {
    	j = 1;
    	do	
		{
			printf(" ");

        } while( j++ < i);

        k = i;
        do
        {
			printf("%d ", k);

		} while( ++k <= rows );
		printf("\n");

	} while( ++i <= rows);
    
    i = rows - 1;
    do
    {
        j = 1;
    	do
		{
			printf(" ");
			
        } while( j++ < i);

        k = i;
        do
        {
			printf("%d ", k);

        } while( ++k <= rows );
		printf("\n");

	} while( --i >= 1);
		
    return 0;
}
Enter Sandglass Number Pattern Rows = 9

The Sandglass Number Pattern
 1 2 3 4 5 6 7 8 9 
  2 3 4 5 6 7 8 9 
   3 4 5 6 7 8 9 
    4 5 6 7 8 9 
     5 6 7 8 9 
      6 7 8 9 
       7 8 9 
        8 9 
         9 
        8 9 
       7 8 9 
      6 7 8 9 
     5 6 7 8 9 
    4 5 6 7 8 9 
   3 4 5 6 7 8 9 
  2 3 4 5 6 7 8 9 
 1 2 3 4 5 6 7 8 9 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.