Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to run this program in do while loop

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 251
    Answer it

    Hi, I want to run the below program in do while loop, please help me.

    #include<stdio.h>
    int main()
    {
      int num;
      char another;
     
      do
      {
        printf("enter the number");
        scanf("%d", &num);
        printf("the square is %d" ,num*num);
        printf("want another y/n");
        scanf("%c",another);
    
      }while(another=='y');
      return 0;
    }


    Thanks in advance.

 2 Answer(s)

  • Hi, I'm a C++ programmer but basically there were some errors. You didn't initiaize y. Therefore the do loop stops after the first iteration because it is not 'y'. Furthermore,, it works for me if I change the type in the last scanf. Hope that helps. Cheers, Konrad #include int main() { int num; char* another; *another = 'y'; do { printf("enter the number"); scanf("%d", &num); printf("the square is %d" ,num*num); printf("want another y/n"); scanf("%s", another); }while(*another =='y'); return 0; }
  • Hi,

    you did not initialize another. Thus the program ends after the first loop iteration.

    Furthermore I think the last scanf is not correct.

    Here the code that worked on my machine.

    Cheers,

    Konrad

    #include<stdio.h>
    int main()
    {
      int num;
      char* another;
      *another = 'y';

      do
      {
        printf("enter the number");
        scanf("%d", &num);
        printf("the square is %d" ,num*num);
        printf("want another y/n");
        scanf("%s", another);
      }while(*another =='y');

      return 0;
    }

     
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: