jeudi 28 septembre 2017

Calculate sum with mathematical indunction

In for function . How i do i to number from 2 to 2. I mean "for(i=1; i<=n; i++)" . At i++ it adds 1 but how to add 2 ? I tried 30 minutes to make that but i can't .. i tried for(i=; i<=n; i= i+2 ) or i+=2 .. i tried to add i+1 in for function .. but nothing worked .. How to do that ? Thanks.

$ int sum = 1;

for(int i=1; i<=n ; i+=2) {
    sum = sum + i;
}   

return sum;

or { int sum = 1;

for(int i=1; i<=n ; i= i +2) {
    sum = sum + i;
}   

return sum;

Aucun commentaire:

Enregistrer un commentaire