Computer Science, asked by aditya721334, 1 month ago

write a C program to find sum of first 10 natural numbers using do while loop​

Answers

Answered by anindyaadhikari13
1

Solution:

The given problem is solved in C.

#include <stdio.h>

int main() {

   int i=1,sum=0;

   do{

       sum+=i;

       i++;

   } while(i<11);

   printf("Sum of first ten natural numbers = %d", sum);

   return 0;

}

Explanation:

  1. Line 1: Includes a header file containing all the standard input output library functions.
  2. Line 2: Start of main() method.
  3. Line 3: Declare two variables i and sum. Variable i stores the natural number 1 and sum stores the sum of the numbers.
  4. Line 4: Start of do-while loop.
  5. Line 5: Value of i is added to the sum.
  6. Line 6: Value of i is incremented to get the next natural number.
  7. Line 7: End of loop with the condition i < 11 since we have to calculate the sum of first ten natural numbers.
  8. Line 8: Sum is displayed on the screen.
  9. Line 9: Function returns 0 since the function is of return-type and not void.
  10. Line 10: End of main() method.

Refer to the attachment for output.

Attachments:
Similar questions