Showing posts with label print starts. Show all posts
Showing posts with label print starts. Show all posts

Monday, May 16, 2016

c program to print box using stars

Problem Statement:- print square box using stars in c, you can use nested loops to solve this problem.

#include<stdio.h>
void main()
 {
  int a,n=6,i,j,k;
  for(i=1;i<=n-2;i++)
   {
    if(i==1||i==(n-2))
     {
      for(j=1;j<=n;j++)
       printf("*");
      printf("\n");
     }
    else
     {
      printf("*");
      for(j=1;j<=n-2;j++)
       printf(" ");
      printf("*");
      printf("\n");
     }
   }
}

output
******
*        *
*        *
******

Saturday, May 14, 2016

C program to print stars in diamond form

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int a,n=4,i,j,k;
  for(i=1;i<=n;i++)
   {
    for(k=1;k<=n-i;k++)
     printf(" ");
    for(j=1;j<=2*i-1;j++)
     printf("*");
     printf("\n");
   }
   for(i=n;i>=1;i--)
   {
    for(j=1;j<=n-i;j++)
     printf(" ");
    for(k=1;k<=2*i-1;k++)
     printf("*");
     printf("\n");
   }
 getch();
}

output
      *
    ***
  *****
*******
*******
  *****
   ***
     *