Home » C programs

C programs to display Pattern according to number of rows (3)

In these C programs (Page 3), we are going to learn how to print display patterns using numbers and asterisks where number of rows is given?
Submitted by Preeti Jain, on February 27, 2018

Example 1: When number of rows: 4

       1
      *2*
     3*4*5
    *6*7*8*

Example 2: When number of rows: 5

        1
       *2*
      3*4*5
     *6*7*8*
    9*10*11*12*13

Here is the C program, that will read number of rows from user and print the display pattern of numbers and asterisks according to given input.



#include <stdio.h>

int main()
{
	int num_of_rows,i,j,num=1,b=0;

	printf("Enter Number Of Rows: ");
	scanf("%d",&num_of_rows);

	for(i=1;i<=num_of_rows;++i)
	{
		for(j=1;j<=(2*num_of_rows-1);++j)
		{
			if(j>=(num_of_rows+1-i) && j<=(num_of_rows-1+i))
				if(b==0)
				{
					printf("%d",num);
					b=1;
					num++;
				}
				else
				{
					printf("*");
					b=0;
				}
			else
				printf(" ");
		}
		printf("\n");
	}

	return 0;
}






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.