EmbLogic's Blog

E14 Star Pattern Using for Loop.

#include<stdio.h>
int main ()
{
printf(“Program to print a pattern of stars.\n”);
int i1,i2,i3,val;
printf(“Enter a number:\n”);
scanf(“%d”,&val);
for(i1=0;i1<val;i1++)
{
/*for(i3=val;i3>i1;i3–)
{
printf(” “);
}*/
for(i2=0;i2<=i1;i2++)
{
printf(“*”);

}
printf(“\n”);
}
return 0;
}

I tried to print a pattern of stars using for loop. The problem i am facing with the logic is that i want to print the following pattern:

*

***

*****

******* /*i.e is odd number of stars to be printed in n number of lines ; Where n is specified by the user;*/ I wanted to print this pattern using for loop only.
anyone wid solution?

 

3 Responses to E14 Star Pattern Using for Loop.

  1. At the place of i1++ in first loop ,just apply i1=i1+2….

    hope this will give u the correct one….

  2. rahul1989 says:

    for(i2=0;i2<=i1;i2++)
    {
    printf(“*”);

    }

    look the for loop initialization you have initialized i2 with 0.
    therefore you are getting one extra * at each line.
    from 0 to i1 (updating according to upper llop) is the printing start's from 0 to i1.
    so i suggest you to initialize i2 from 1 instead of 0.

    check it out and reply me also

  3. ayush.gupta says:

    Shalini Singhal thanx for the help, it did solve my purpose for printing the star pattern as i wanted to print. Here is the successful program
    The Current Print pattern for a value of 3 entered by the user gives:
    "___* "
    "__*** "
    "_***** "

    Replace"_" by space…

    rahul1989… initializing i2 with 1 will produce a different result. The logic is to increment i1 by 2…
    :)

Leave a Reply to rahul1989 Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>