answersLogoWhite

0

Standard iterative solution:

int sumEvens = 0;
int sumOdds = 0;

int i;
for(i = 0; i <= n; ++i) {

if(i % 2 == 0) {
sumEvens += i;

}else {
sumOdds += i;

}

}


Clever way:

(This assumes all values are integer)

int num_odds = ((n - 1) >> 1) + 1; /* number of odd numbers from 1 to n */

int num_evens = n >> 1; /* number of even numbers from 1 to n */

int sum_odds = num_odds * num_odds;

int sum_evens = (num_evens * num_evens) + num_evens;

summing 10 numbers with variables

#include

main()

{

int a,b,c,d,e,f,g,h,i,j,add;

printf("Enter 10 numbers to sum:\t");

scanf("%d %d %d %d %d %d %d %d %d %d",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);

add = a+b+c+d+e+f+g+h+i+j;

printf("%dResult:\t",add);

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Write a javascript program for Fibonacci series?

I did this as an answre to some common js questions , the question wasWrite a function which will return you first two times 1, then 2, then 3, then 5 and so on (Fibonacci numbers). Don't use any global variables.var fibonacci = (function () {var arr = [0, 1];return function () {var num = arr[arr.length - 1],len = arr.length;arr.push(arr[len - 1] + arr[len - 2]);return num;};}());//testvar i;for (i = 0; i < 10; i++) {console.log(fibonacci());}//1,1,2,3,5,8,13,21,34,55


Can you have the program in C plus plus for recursive function generating a series of terms?

Yes, this can be done. For example for Fibonacci series. You will find plenty of examples if you google for the types of series you need to be generated.


What is the structure of logical argument?

An argument is a connected series of statements to establish a definite proposition. 3 stages to an argument: Premises, inference, and conclusion.


Is the word program a noun?

Yes, &quot;program&quot; can function as a noun. It can refer to a planned series of activities or events, a set of coded instructions input into a computer, or a specific performance or broadcast on television or radio.


Will Merlin return in 2012 for series 5?

Merlin will return for a 5th series in the autumn of 2012.


Do Fibonacci Series program in dot net?

public static int fib(int n) {return fib(n-1) + fib(n-2);}


What is the duration of Point of No Return TV series?

The duration of Point of No Return - TV series - is 2700.0 seconds.


Discontinuous function in fourier series?

yes a discontinuous function can be developed in a fourier series


When will Merlin return?

depends on which series though but i know that it will return for the 4th and 5th series on the 10th of auguest and in aututerm


When the regular series of MEDIUM will return when will the series MEDIUM return?

Sunday @ 9:00 EST in early 2009


What year will Green Lantern the Animated Series return?

It will never return. The series ended after one season in 2013.


When was Point of No Return - TV series - created?

Point of No Return - TV series - was created on 2003-11-03.