This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Showing posts with label Star Pattern. Show all posts
Showing posts with label Star Pattern. Show all posts

Thursday, 25 July 2013

Program to print the Star pattern 6 (Right-angled Triangle)

#include <stdio.h>
void main()
{
int n, c, k;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
getch();
}




Monday, 1 July 2013

Program to print the Star pattern 5 (Diamond)

#include <stdio.h>
void main()
{
int n, c, k, space = 1;
clrscr();
printf("Enter the number of rows in Diamond:");
scanf("%d", &n);
space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
getch();
}


Friday, 28 June 2013

Program to print the Star pattern 4 (Pyramid)

#include <stdio.h>
void main()
{
int row, c, n, temp;
clrscr();
printf("Enter the number of rows in Pyramid: ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
getch();
}


Sunday, 23 June 2013

Program to print the Star pattern 3

#include<stdio.h>
main()
{
char prnt = '*';
int i, j, k, s, nos = -1;
clrscr();
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{
printf("  ");
}
for (k = 1; k <= i; k++)
{
if (i == 5 && k == 5)
{   
continue;  
}  
printf("%2c", prnt); 

nos = nos + 2; 
printf("\n");
}
nos = 5;
for (i = 2; i <= 5; i++)

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

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{  
printf("  "); 

for (k = 1; k <= i; k++)
{  
if (i == 5 && k == 5)
{   
break;  
}  
printf("%2c", prnt); 

nos = nos - 2; 
printf("\n");
}
getch();
}


Saturday, 22 June 2013

Program to print the Star pattern 2

#include<stdio.h>
main()
{
char prnt = '*';
int i, j, k, s, c = 1, nos = 9;
clrscr();
for (i = 1; c <= 4; i++)
{
if ((i % 2) != 0)
{
for (j = 1; j <= i; j++)

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{
if (c == 4 && s == 1)
{
break;
}
printf("  ");
}
for (k = 1; k <= i; k++)
{
if (c == 4 && k == 5)
{
break;
}
printf("%2c", prnt);
}
printf("\n");
nos = nos - 4;
++c;
}
}
getch();
}


Program to print the Star pattern 1 (Right-angled Triangle)

#include<stdio.h>
main()
{
char prnt = '*';
int i, j, nos = 4, s;

clrscr();
for (i = 1; i <= 5; i++)
{
for (s = nos; s >= 1; s--)
{
printf("  ");
}
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
printf("\n");
  --nos;  

}
getch();
}