Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:20db2fce0d5c, 2021-05-10 (annotated)
- Committer:
- darrendaly
- Date:
- Mon May 10 16:09:34 2021 +0000
- Revision:
- 0:20db2fce0d5c
lab 2 pt 1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| darrendaly | 0:20db2fce0d5c | 1 | // Program to calculate the sum of first n natural numbers |
| darrendaly | 0:20db2fce0d5c | 2 | // Positive integers 1,2,3...n are known as natural numbers |
| darrendaly | 0:20db2fce0d5c | 3 | |
| darrendaly | 0:20db2fce0d5c | 4 | #include <stdio.h> |
| darrendaly | 0:20db2fce0d5c | 5 | int main() |
| darrendaly | 0:20db2fce0d5c | 6 | { |
| darrendaly | 0:20db2fce0d5c | 7 | int num, count, sum = 0; |
| darrendaly | 0:20db2fce0d5c | 8 | |
| darrendaly | 0:20db2fce0d5c | 9 | printf("Enter a positive integer: "); |
| darrendaly | 0:20db2fce0d5c | 10 | scanf("%d", &num); |
| darrendaly | 0:20db2fce0d5c | 11 | |
| darrendaly | 0:20db2fce0d5c | 12 | // for loop terminates when num is less than count |
| darrendaly | 0:20db2fce0d5c | 13 | for(count = 1; count <= num; ++count) |
| darrendaly | 0:20db2fce0d5c | 14 | { |
| darrendaly | 0:20db2fce0d5c | 15 | sum += count; |
| darrendaly | 0:20db2fce0d5c | 16 | } |
| darrendaly | 0:20db2fce0d5c | 17 | |
| darrendaly | 0:20db2fce0d5c | 18 | printf("Sum = %d", sum); |
| darrendaly | 0:20db2fce0d5c | 19 | |
| darrendaly | 0:20db2fce0d5c | 20 | return 0; |
| darrendaly | 0:20db2fce0d5c | 21 | |
| darrendaly | 0:20db2fce0d5c | 22 |