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

16y 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


Sample program for sine series in vbnet?

Here’s a simple VB.NET program to calculate the sine of an angle using the Taylor series expansion: Module SineSeries Function Sine(x As Double, terms As Integer) As Double Dim sineValue As Double = 0.0 For n As Integer = 0 To terms - 1 Dim term As Double = Math.Pow(-1, n) * Math.Pow(x, 2 * n + 1) / Factorial(2 * n + 1) sineValue += term Next Return sineValue End Function Function Factorial(n As Integer) As Double Dim result As Double = 1 For i As Integer = 2 To n result *= i Next Return result End Function Sub Main() Dim angle As Double = Math.PI / 4 ' 45 degrees Dim terms As Integer = 10 Console.WriteLine(&quot;Sine of 45 degrees: &quot; &amp; Sine(angle, terms)) End Sub End Module This program defines the sine function using Taylor series and calculates the sine of 45 degrees using 10 terms of the series.


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.


How many value does a procedure and a function return?

A procedure typically does not return a value; it performs a specific task or series of tasks. In contrast, a function is designed to return a single value after performing a calculation or operation. However, functions can also return data structures that contain multiple values, such as arrays or objects, depending on the programming language.


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.


Do Fibonacci Series program in dot net?

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


Will Merlin return in 2012 for series 5?

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


Write a program to print the series 072663124--term n?

To print the series 072663124 for a given term n, you can use a simple program in Python. The series appears to follow a specific pattern, so you can create a function that generates and prints the series up to the nth term. Here’s a basic implementation: def print_series(n): series = &quot;072663124&quot; print(series[:n]) # Example usage: n = 9 # specify the term you want to print print_series(n) This program prints the first n characters of the series. Adjust n to get the desired output.


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 did Return to Peyton Place - TV series - end?

The duration of Return to Peyton Place - TV series - is 1800.0 seconds.