UP | HOME

Exercise 4 solutions

Table of Contents


Python

e04.py

sum = 0
for i in range(11):
    sum = sum + i
print "the sum of the first 10 positive integers is:", sum

MATLAB / Octave

e04.m

sum = 0;
for i = 1:10
    sum = sum + i;
end;
disp(['the sum of the first 10 positive integers is: ',num2str(sum)]);

R

e04.R

sum <- 0
for (i in 1:10) {
    sum <- sum + i
}
cat("the sum of the first 10 positive integers is:",sum,"\n")

C

e04.c

// compile with: gcc -o e04 e04.c

#include <stdio.h>

int main(int argc, char *argv[]) {

  int sum = 0;
  int i;
  for (i=1; i<=10; i++) {
    sum = sum + i;
  }
  printf("the sum of the first 10 positive integers is: %d\n", sum);

  return 0;
}

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License