hiii,

Usin' for loop i've already sent a program to print diamond.now i've tried to make it with recursion.

guys i'm sendin'  a solution to saud's prob with recursion in two ways.

first one is with while loop .

function is recursive after printin' one line.

*************************************************************

#include<stdio.h>

#include<conio.h>

void diamond(int, int, int);

void main()

{

int n;

clrscr();

printf("Input a number");

scanf("%d",&n);

diamond (1,1,n);

getch();

}

 

void diamond(int i, int j, int n)

{

 if(i<=n)

 {

   while(j<=i)

   {

   printf("*");

   j++;

   }

   printf("\n");

 return diamond(i+1,1,n);

 }

 

 if(n-1>=1)

 {

   while(j<=i-n)

   {

   printf(" ");

   j++;

   }

   j=1;

   while(j<=n-1)

   {

   printf("*");

   j++;

   }

   printf("\n");

 

 return diamond(i,1,n-1);

 }

 else

 return;

 }

 ****************************************************

in this program i've tries not to use  even a single while  loop.

function is recursive after printin' each * and even space.

if any can make this  program in a better  way please mail it in the group

 

#include<stdio.h>

#include<conio.h>

void diamond(int, int, int,int);

void main()

{

int n;

clrscr();

printf("Input a number");

scanf("%d",&n);

diamond (1,1,n,1);

getch();

}

 

void diamond(int i, int j, int n,int k)

{

 

 if(i<=n)

 {

   if(j<=i)

   {

   printf("*");

   diamond(i,j+1,n,k);

   }

   else

   {

   printf("\n");

   diamond(i+1,1,n,k);

   }

 }

 

   if(n-1>=1 && k==1)

   {

    if(j<=i-n )

    {

    printf(" ");

    diamond(i,j+1,n,1);

    }

    else

    diamond(i,1,n,0);

 

   }

 

 

   if(n-1>=1 && k==0)

   {

 

   if(j<=n-1 )

   {

   printf("*");

   diamond(i,j+1,n,0);

   }

   else

   {

   printf("\n");

   diamond(i,1,n-1,1);

   }

 }

 getch();

 exit();

 

}

***************************************************************************

thx

girish



saud aziz <[EMAIL PROTECTED]> wrote:

Can anyone tell me using for loop as well as (if possible) with
recursion how would i make a program that creates diamond like this:

1     *

2     *
      **
       *

3     *
      **
     ***
      **
       *

for a function that takes in # of lines, so the last diamond above
should be displayed if user enters 3.


--
"Our lives begin to end the day we become silent about things that
matter." Dr. Martin Luther King Jr.





To unsubscribe : [EMAIL PROTECTED]





Do you Yahoo!?
vote.yahoo.com - Register online to vote today!

To unsubscribe : [EMAIL PROTECTED]




Yahoo! Groups Sponsor
ADVERTISEMENT
click here
Web Bug from http://us.adserver.yahoo.com/l?M=315388.5526708.6599542.3001176/D=groups/S=:HM/A=2372354/rand=949315021


Yahoo! Groups Links

Reply via email to