Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@smorgasbord-Aq7fYb • Apr 27, 2014
#include<stdio.h> // Includes the standard library. int main(){ // Main function int i=2,j=2; // Sets 2 variables ( i and j ) to values of 2. while(i+1?--i:j++) // Loops while i+1 is true ( not zero ), and decreases i and increases j printf("%d",i); // Shows the output of i at every iteration of the loop return 0; // end of the main function, returns 0. }Courtesy
@SJC1000
Channel: #ahkscript
server: Freenode
Internet Relay Chat -
@mansi-munjal-5bqy46 • Oct 26, 2014
hey...can anyone help me to understand the logic of star patterns program??pls help... -
@neeraj-kumar-singh-101V1v • Oct 26, 2014
It works like 2 D array.Mansi Munjalhey...can anyone help me to understand the logic of star patterns program??pls help... -
@mansi-munjal-5bqy46 • Oct 26, 2014
Actually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplainNeeraj Kumar SinghIt works like 2 D array. -
@neeraj-kumar-singh-101V1v • Oct 26, 2014
first loop is for no of rows.Mansi MunjalActually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplain
it doesnt matter whether it z strt from 0 or 5.
if we strt from 0
den the for loop like thz for(int i=0;i<5;i++)
if we strt from 5
den the for loop like for(int i=5;i>0;i--)
in both d cases no of rows remains same i.e 5. -
@neeraj-kumar-singh-101V1v • Oct 26, 2014
sometime first for loop depends upon pattern.Mansi MunjalActually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplain
foe eg.
pattern like thz
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
code:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout<<i<<" ";
}
cout<<endl;
}
getch();
} -
@mansi-munjal-5bqy46 • Oct 26, 2014
thank you so much....but can you explain me more clearly that how can i get to know from where i will start the loop. -
@mansi-munjal-5bqy46 • Oct 26, 2014
#include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include<stdio.h> void main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
but why in this loop i and k started from 5?????? -
@saandeep-sreerambatla-hWHU1M • Oct 27, 2014
You have to understand one thing here,VIJAYSY[HASHTAG]#include[/HASHTAG]<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
The above question was asked for my seniors in Mu-Sigma written test..
The output for the above code is 1. I don't know how it is 1 , help me to
to understand.
there are no brackets after while loop so it executes only once.
so what does the statement say?
while(i+1"--i:j++)
so value of i is 2, so while(3) -- this is true. so --i will return you 1 and that is printed. -
@neeraj-kumar-singh-101V1v • Oct 27, 2014
output for above program is thz..Mansi Munjal#include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include<stdio.h> void main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
but why in this loop i and k started from 5??????
-
@neeraj-kumar-singh-101V1v • Oct 27, 2014
[HASHTAG]#include[/HASHTAG]<stdio.h>Mansi Munjal#include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include<stdio.h> void main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
but why in this loop i and k started from 5??????
void main()
{
int i,j,k;
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++) // if i=5 den 4 spaces
{
printf(" ");
}
for(k=5;k>=i;k--) /*if i=5 thz loop runs only one time so,its print 1 star,if i=4 den runs two times 2 star bcz k =5 and i=4 5>=4 (true) ,k-- k=4 ,i=4,4>=4 (true) and den k-- k=3 ,i=4 3>=4 (false) it comes out of the loop.*/
{
printf("*");
}
printf("\n");
}
} -
@neeraj-kumar-singh-101V1v • Oct 27, 2014
we can write thz code also like thzMansi Munjal#include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include<stdio.h> void main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } }output
*
**
***
****
*****
but why in this loop i and k started from 5??????
[HASHTAG]#include[/HASHTAG]<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=4;j>=i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
}
basically its all depend upon ur logic .
always dry run ur code on paper.
it is the best way to understand the any program. 😀
Happy coding.. 😀 -
@rajeevkumarsingh-sIcD9o • Oct 28, 2014
Hey Sandeep you are right.Saandeep SreerambatlaYou have to understand one thing here,
there are no brackets after while loop so it executes only once.
so what does the statement say?
while(i+1"--i:j++)
so value of i is 2, so while(3) -- this is true. so --i will return you 1 and that is printed.
Since i+1 will always be true so first value is going to execute that is --i. This make the value of i to be 1.
Next is while loop, with null statement nothing will happen. The value of i remain 1 which get printed as output.
At the same contrary to this question. Execute the following and tell the output and the reason.
[HASHTAG]#include[/HASHTAG]<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++){
j=0;
}
printf("%d",i);
return 0;
} -
@gaurav-zagade-ZI6bf2 • Oct 28, 2014
There is no 1D or 2D array taken i mean to say doesn't taken any array..In while the conditional operator(?) is used if you dont know the concept of conditional operator then you will never understand the program. before entering into the program while i+1 if this condition is treated as true then it will goes to --i which is predecrementation now you will ask me how this i+1 will treated as true,In conditional operator or any if else statement non zero value is treated as true So in first loop i+1 value becomes 3 which is non zero value so it will treated as true and execute --i statement which is predecrementation,now the value of i was 2 after the predecrementation it will becomes 1,Therefore it will print 1.if you write i--(Postdecrementation)in that while condition then 1st it will print 1 then after it will print 0,because in Postdecrementation the old value(which is 1) is first assign and then it will decremented that means
2-1=1//old value
1-1=0
the output will print 1 and 0
now here 0 is treated as false so loop will stop here. -
@mansi-munjal-5bqy46 • Oct 28, 2014
thankuu so much...seriously thanks..Neeraj Kumar Singhwe can write thz code also like thz
[HASHTAG]#include[/HASHTAG]<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=4;j>=i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
}
basically its all depend upon ur logic .
always dry run ur code on paper.
it is the best way to understand the any program. 😀
Happy coding.. 😀 -
@neeraj-kumar-singh-101V1v • Oct 28, 2014
U r Most welcome #-Link-Snipped-# .... 😀Mansi Munjalthankuu so much...seriously thanks..