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.

Replies

  • smorgasbord
    smorgasbord
    #include // 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
    Mansi Munjal
    hey...can anyone help me to understand the logic of star patterns program??pls help...
  • Neeraj Kumar Singh
    Neeraj Kumar Singh
    Mansi Munjal
    hey...can anyone help me to understand the logic of star patterns program??pls help...
    It works like 2 D array.
  • Mansi Munjal
    Mansi Munjal
    Neeraj Kumar Singh
    It works like 2 D array.
    Actually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplain
  • Neeraj Kumar Singh
    Neeraj Kumar Singh
    Mansi Munjal
    Actually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplain
    first loop is for no of rows.
    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
    Neeraj Kumar Singh
    Mansi Munjal
    Actually i am confused in first loop which is for row.Sometime it initialized from 1 and sometime frim 5.pls xplain
    sometime first for loop depends upon pattern.

    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 Munjal
    Mansi Munjal
    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
    Mansi Munjal
    #include
    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
    void main()
    {
    int i,j,k;
    for(i=5;i>=1;i--)
    {
    for(j=1;j=i;k--)
    {
    printf("*");
    }
    printf("\n");
    }
    } 
    output
    *
    **
    ***
    ****
    *****
    but why in this loop i and k started from 5??????
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    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.
    You 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.
  • Neeraj Kumar Singh
    Neeraj Kumar Singh
    Mansi Munjal
    #include
    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
    void main()
    {
    int i,j,k;
    for(i=5;i>=1;i--)
    {
    for(j=1;j=i;k--)
    {
    printf("*");
    }
    printf("\n");
    }
    } 
    output
    *
    **
    ***
    ****
    *****
    but why in this loop i and k started from 5??????
    output for above program is thz..c
  • Neeraj Kumar Singh
    Neeraj Kumar Singh
    Mansi Munjal
    #include
    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
    void main()
    {
    int i,j,k;
    for(i=5;i>=1;i--)
    {
    for(j=1;j=i;k--)
    {
    printf("*");
    }
    printf("\n");
    }
    } 
    output
    *
    **
    ***
    ****
    *****
    but why in this loop i and k started from 5??????
    [HASHTAG]#include[/HASHTAG]
    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
    Neeraj Kumar Singh
    Mansi Munjal
    #include
    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
    void main()
    {
    int i,j,k;
    for(i=5;i>=1;i--)
    {
    for(j=1;j=i;k--)
    {
    printf("*");
    }
    printf("\n");
    }
    } 
    output
    *
    **
    ***
    ****
    *****
    but why in this loop i and k started from 5??????
    we 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.. ๐Ÿ˜€
  • RajeevKumarsingh
    RajeevKumarsingh
    Saandeep Sreerambatla
    You 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.
    Hey Sandeep you are right.
    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 Zagade
    Gaurav Zagade
    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
    Mansi Munjal
    Neeraj Kumar Singh
    we 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.. ๐Ÿ˜€
    thankuu so much...seriously thanks..
  • Neeraj Kumar Singh
    Neeraj Kumar Singh
    Mansi Munjal
    thankuu so much...seriously thanks..
    U r Most welcome #-Link-Snipped-# .... ๐Ÿ˜€

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...