C Programming Code for this display

Asked By 10 points N/A Posted on -
qa-featured

Good day!

I need some experts to help me on this matter.

Could somebody give me a code for this display?

*

* *

* * *

* * * *

* * *

* *

*

Above should be the display if I input 4 as the number.

The display depends on the integer to be inputted.

I need the codes ASAP.

Thanks.

SHARE
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #99365

C Programming Code for this display

qa-featured

Such a formation is very interesting in learning the loop structures. These are examples used to describe loop structures in almost every programming language.

The work is simple, all you need to do is that you need two separate for loops.

Take the input.
First loop prints star(*), increasing one per row until 4.

Second loop prints star(*), decreasing form 3 to 1 per row.

Thus you get such a formation with leading stars and trailing stars with some features at the input.

However you can go through other such formations for better understanding of loop skills.

Try yourself another one so that you develop your skills.

for(i=1;i<=4;i++){

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

printf(“*”);

}

for(i=3;i>=1;i–){

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

printf(“*”);

}

 
Answered By 0 points N/A #99367

C Programming Code for this display

qa-featured

These type of questions are asked to track your analytical skills ,creativity and problem solving skills. good understanding of loop is also needed to solve this question.

For most of these questions there are a lot of solutions in which a few are explained here.

  • let us solve this question without loop.
     

algorithm 

  1. At first clear the console screen.
  2. Then print string " * " 
  3. Go to the next line
  4. Then pint string " * * "
  5. Go to the next line
  6. Then pint string " * * * "
  7. Go to the next line
  8. Then pint string " * * * * "
  9. Go to the next line
  10. Then pint string " * * * "
  11. Go to the next line
  12. Then pint string " * * "
  13. Go to the next line
  14. Then pint string " * "
  15. stop the program
  16.  

program will be like this

 

#include <stdio.h>

#include <conio.h>

 

int main(){

clrscr();

printf(“ * n”);

printf(“ * * n”);

printf(“ * * * n”);

printf(“ * * * * n”);

printf(“ * * * n”);

printf(“ * * n”);

printf(“ * n”);

getch();

return 0;

}

this explain your analytical skills ,creativity and problem solving skills but decrease your technical knowledge rating. if you do like this next they will ask  about loop. you can replace all print statements to a single print statement like this 

 

int main(){

clrscr();

 

printf(“ * n * * n * * * n * * * * n * * * n * * n * ”);

 

getch();

return 0;

}

  • you can even solve this by saving this string in an array and display it like this.

 

algorithm 

  1. At first clear the console screen.
  2. store this string in an character array " * n * * n * * * n * * * * n * * * n * * n * "
  3. display the array using loop
  4. stop the program

 

#include <stdio.h>

#include <conio.h>

 

int main(){

char a[20]=" * n * * n * * * n * * * * n * * * n * * n * ";

int count;

clrscr();

for (count =0;a[count]!='';count++){

        printf("%c",a[count]);

}

getch();

return 0;

}

 

here you are explaining technical knowledge like array concept.
 
  • next is using nested for loops

algorithm 

  1. At first clear the console screen.
  2. initialize both counters to one
  3. print ' * ' in the display
  4. repeat step 3 till both counters are equal
  5. go to next line
  6. repeat steps from step 3 to 3 times
  7. initialize one counters to one and other to four
  8. print ' * ' in the display
  9. repeat step 8 till both counters are equal
  10. go to next line
  11. repeat steps from step 8 to 3 times
  12. stop the program

 

#include <stdio.h>

#include <conio.h>

 

int main(){

int i,j;

clrscr();

for(i=1;i<4;i++){

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

           printf("*");

       }

       printf("n");

}

for (i=4;i>0;i–){

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

                printf("*");

           }

           printf("n");

}

getch();

return 0;

}

 

good luck for your interview.
 
Answered By 0 points N/A #99368

C Programming Code for this display

qa-featured

int I,j,n;……………1

printf(“Enter the number of * in middle line:”);…………….2

scanf(%d, &n);…………….3

for(i=1;i<=n;i++)                       // first for loop

{

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

                {

                                printf(“*”);

                }

printf(“n”);

}

for(i=n-1;i>=1;i–)                    //second for loop

{

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

                {

                                printf(“*”);

                }

printf(“n”);

}

Let’s understand the logic behind the C code:

If we consider the * layout in the form of table then we have to print 1star in 1St row, then leave the line, 2 starts in 2ND row, then again leave the line and so on till the loop reaches to 4 (number that you enter as input).

DRY run of the code:

Line number 1 is the variable declaration

Line number 2 and 3 will take input from the user, suppose 4 in this case. So n=4.

First for loop:

1.       First Iteration:

I=1, check i<=n (1<=4, true) – Enter into the loop

             J=1, check j<=I (1<=1, true) – Enter into the loop

                          Print *

             Now j++ i.e. j=2, check j<=i (2<=1, false) – Exit from the loop

Print(“n”) – leave this line

Now i=2.

2.       Second Iteration:

I=2, check i<=n (2<=4, true) – Enter into the loop

             J=1, check j<=i (1<=2, true) – Enter into the loop

                          Print *

                         Now j++ i. e. j=2, check j<=i (2<=2, true) – be in the loop

                         Print 2nd *

            Now j++ i. e. j=3, check j<=i (3<=2, false) – Exit from the loop

Print(“n”) – leave this line

Now i=3.

And so on. Like this you can continue your dry run till the condition i<=n become false.

Second for loop: Same logic as there for first for loop.

Try it yourself now and learn the star series

Answered By 10 points N/A #99369

C Programming Code for this display

qa-featured

This program reads input n and gets the desired output.

#include<stdio.h>
main()
{
    int i,j,n;
    printf("nEnter the limit:");
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        for(j=1;j<=i;++j)
            printf("* ");
        printf("n");
    }
    for(i=n-1;i>0;–i)
    {
        for(j=1;j<=i;++j)
            printf("* ");
        printf("n");
    }
}

Related Questions