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