Pattern generator in c ::
1. Write a c code to print the pattern
*
* *
* * *
* * * *
Ans
#include<stdio.h>
int main()
{
int i,j,k,l;
for(i=1;i<=4;i++)
{
for(j=0;j<=i-1;j++)
{
printf("* ");
}
printf("\n");
}
getch();
return 0;
}
2. Write a c code to print the pattern
*
* *
* * *
* * * *
Ans.
#include<stdio.h>
int main()
{
int i,j,k,l;
for(i=1;i<=4;i++)
{
for(k=3;k>=i;k--)
{
printf(" ");
}
for(j=0;j<=i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
return 0;
}
3.Write a c code to print the pattern
*
* *
* * *
* * * *
Ans.
#include<stdio.h>
int main()
{
int i,j,k,l;
for(i=1;i<=4;i++)
{
for(k=3;k>=i;k--)
{
printf(" ");
}
for(j=0;j<=i-1;j++)
{
printf("* ");
}
printf("\n");
}
getch();
return 0;
}
in the first progrm what is the need to take the 4rth variable?
ReplyDeleteit's not really required. you may omit the 4th variable.
Deletecan anyone help me to write code for this
ReplyDeletep
pr
pro
prog
progr
progra
program
Your ans Mr. Rit.....
ReplyDeleteint main()
{
char a[100]={'p','r','o','g','r','a','m'};
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",a[j]);
}
printf("\n");
}
getch();
return 0;
}
Try this. all the best.
Here is another answer Rit : u can try it out with any word...
ReplyDelete#include
#include
main()
{
char string[100];
int c, k, length;
printf("Enter a string\n");
gets(string);
length = strlen(string);
for ( c = 0 ; c < length ; c++ )
{
for( k = 0 ; k <= c ; k++ )
{
printf("%c", string[k]);
}
printf("\n");
}
getch();
return 0;
}
//and let us know how it works... :)