-1

Here a print message "Do you want to run again?" is asked, if we enter "y" then program will repeat.

I tried this code

#include<stdio.h>
int main()
{
    int a, b, c;
    char ch;
    ch = 'y';
    printf("enter 1st and 2nd no.");
    scanf("%d%d", &a, &b);
    {
        c = a + b;
        printf("%d", c);
        printf("Do you want to run again?");
        scanf("%s", &ch);
    }
    while(ch == 'y')
        return 0;
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
HuHu
  • 59
  • 1
  • 10
  • 1
    This isn't a do-while. It's a block, followed by an unrelated `while` whose body is `return 0;` (which makes no sense). You need to add the keyword `do` in front of the block, and a semicolon after the while, e.g. `do { ... } while (...);` – Tom Karzes Nov 07 '19 at 03:02
  • @TomKarzes Thank you I got the answer as I am a noob and this is my first program using do while loop, I was not knowing the process properly, Thank you very much. If you want you can post your comment as answer – HuHu Nov 07 '19 at 03:06
  • 1
    You may want to look at [Sum and Average with Dynamic Array](https://stackoverflow.com/questions/58723670/sum-and-average-with-dynamic-array/58726453#58726453) for how to simplify handling the `'Y'` or `'y'` check. – David C. Rankin Nov 07 '19 at 05:47

2 Answers2

1
#include<stdio.h>
int main()
{
   int a,b,c;
   char ch;

   do
   {
   printf("enter 1st and 2nd no.");
   scanf("%d%d",&a,&b);
   c = a + b;
   printf("%d",c);
   printf("Do you want to run again?");
   scanf(" %c",&ch);
   }
   while(ch == 'y');
   return 0;
   }
HuHu
  • 59
  • 1
  • 10
0

Use a do-while loop:

#include<stdio.h>

int main()
{
    int a, b, c;
    char ch;

    do {
        printf("enter 1st and 2nd number: ");
        scanf("%d %d", &a, &b);
        c = a + b;
        printf("%d\n", c);
        printf("Do you want to run again?: ");
        scanf("%s",&ch);
    }
    while(ch=='y');

    return 0;
}
anon_g
  • 16
  • 1