Computer Science, asked by Anonymous, 9 months ago

write an program to calculate sum of first 10 odd number

Answers

Answered by sujeetrsskmp
4

Answer:

First let's see the math behind it.

We have 1,3,5,7......

So this is Arithmetic progression with

a =1; d =2

Let's denote sum of this numbers be S

So after applying formula of first n numbers in A.P. ,we get

S = n/2[2a+(n-1)d]

S = n/2[2+2n-2]

S = n^2

Hence sum of first 10 odd numbers will be 10^2 = 100 (I think coding this is much simpler)

Other most simple way in program you can use is have one for loop running from 1 to 19 (because this are first 10 odd numbers) and add it with the variable named sum (or anything you prefer) and increment value of variable which you are adding by 2 (as you want to add only odd numbers).

It will look like this (code snippet in c++)

#include <iostream>

using namespace std;

int main()

{

int sum = 0;

for(int i = 1; i < 20; i = i+2)

sum += i;

cout << sum;

return 0;

}

Similar questions