CrazyEngineers
  • What's the output of this C program?

    VIJAYSY

    VIJAYSY

    @vijaysy-L0Tg5s
    Updated: Oct 26, 2024
    Views: 1.3K
    [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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • smorgasbord

    MemberApr 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
    Are you sure? This action cannot be undone.
    Cancel
  • Mansi Munjal

    MemberOct 26, 2014

    hey...can anyone help me to understand the logic of star patterns program??pls help...
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 26, 2014

    Mansi Munjal
    hey...can anyone help me to understand the logic of star patterns program??pls help...
    It works like 2 D array.
    Are you sure? This action cannot be undone.
    Cancel
  • Mansi Munjal

    MemberOct 26, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 26, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 26, 2014

    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<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();
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Mansi Munjal

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Mansi Munjal

    MemberOct 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??????
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberOct 27, 2014

    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.
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 27, 2014

    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??????
    output for above program is thz..c
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 27, 2014

    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??????
    [HASHTAG]#include[/HASHTAG]<stdio.h>
    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");
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 27, 2014

    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??????
    we 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.. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • RajeevKumarsingh

    MemberOct 28, 2014

    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]<stdio.h>
    int main(){
    int i=2,j=2;
    while(i+1?--i:j++){
    j=0;
    }
    printf("%d",i);
    return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Gaurav Zagade

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Mansi Munjal

    MemberOct 28, 2014

    Neeraj Kumar Singh
    we 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.. 😀
    thankuu so much...seriously thanks..
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Kumar Singh

    MemberOct 28, 2014

    Mansi Munjal
    thankuu so much...seriously thanks..
    U r Most welcome #-Link-Snipped-# .... 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register