Asked By
macraw
10 points
N/A
Posted on - 04/07/2012
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.
C Programming Code for this display
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
junujon
0 points
N/A
#99367
C Programming Code for this display
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
-
At first clear the console screen.
-
Then print string " * "
-
Go to the next line
-
Then pint string " * * "
-
Go to the next line
-
Then pint string " * * * "
-
Go to the next line
-
Then pint string " * * * * "
-
Go to the next line
-
Then pint string " * * * "
-
Go to the next line
-
Then pint string " * * "
-
Go to the next line
-
Then pint string " * "
-
stop the program
-
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
-
At first clear the console screen.
-
store this string in an character array " * n * * n * * * n * * * * n * * * n * * n * "
-
display the array using loop
-
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
-
At first clear the console screen.
-
initialize both counters to one
-
print ' * ' in the display
-
repeat step 3 till both counters are equal
-
go to next line
-
repeat steps from step 3 to 3 times
-
initialize one counters to one and other to four
-
print ' * ' in the display
-
repeat step 8 till both counters are equal
-
go to next line
-
repeat steps from step 8 to 3 times
-
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.
C Programming Code for this display
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
E.Robe
10 points
N/A
#99369
C Programming Code for this display
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");
}
}