What's the output of this C program?
[HASHTAG]#include[/HASHTAG]
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.
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.
Replies
-
smorgasbord
#include
Courtesy// 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. }
@SJC1000
Channel: #ahkscript
server: Freenode
Internet Relay Chat -
Mansi Munjalhey...can anyone help me to understand the logic of star patterns program??pls help...
-
Neeraj Kumar Singh
It works like 2 D array.Mansi Munjalhey...can anyone help me to understand the logic of star patterns program??pls help... -
Mansi Munjal
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
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
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
#include
int main()
{
clrscr();
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout< }
cout<}
getch();
} -
Mansi Munjalthank 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
#include
outputvoid main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include
outputvoid main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j=i;k--) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
but why in this loop i and k started from 5?????? -
Saandeep Sreerambatla
You have to understand one thing here,VIJAYSY[HASHTAG]#include[/HASHTAG]
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
output for above program is thz..Mansi Munjal#include
outputvoid main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include
outputvoid main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j=i;k--) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
but why in this loop i and k started from 5?????? -
Neeraj Kumar Singh
[HASHTAG]#include[/HASHTAG]Mansi Munjal#include
outputvoid main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include
outputvoid main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j=i;k--) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
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 {
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
we can write thz code also like thzMansi Munjal#include
outputvoid main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
i understand this program cz loop is start from 1.
but in
#include
outputvoid main() { int i,j,k; for(i=5;i>=1;i--) { for(j=1;j=i;k--) { printf("*"); } printf("\n"); } }
*
**
***
****
*****
but why in this loop i and k started from 5??????
[HASHTAG]#include[/HASHTAG]
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
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]
int main(){
int i=2,j=2;
while(i+1?--i:j++){
j=0;
}
printf("%d",i);
return 0;
} -
Gaurav ZagadeThere 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
thankuu so much...seriously thanks..Neeraj Kumar Singhwe can write thz code also like thz
[HASHTAG]#include[/HASHTAG]
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
U r Most welcome #-Link-Snipped-# .... ๐Mansi Munjalthankuu so much...seriously thanks..
You are reading an archived discussion.
Related Posts
HAPPY 75th BIRTHDAY @A.V.Ramani SIR!!! ๐
Hope you get the sweetest surprises today! God bless!
I just had a phone call with the local BSNL officer about the Internet connectivity issues I've been facing since last ~2 days. The officer informed to me that 8...
I'm trying to learn how to undrestand being hacked and learn hacking ways to protect my own systems.what do you know about hacking?
I am looking to change my domain to testing, can someone guide me from where to do? Also which all points and which all technologies I should master in?
Flipkart has announced the 'Same Day' delivery guarantee, if you are located in Mumbai, Delhi, Bangalore, Noida, Gurgaon, Faridabad, Kolkata, Navi Mumbai or Thane. The condition is that you should...