answersLogoWhite

0


Best Answer
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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include

void main()

{

int n,i;

int addeven=0;

int addodd=1;

printf("\nEnter a number\n");

scanf("%d",&n);

fflush(stdin);

for(i=2;i<=n;i++)

{

if(i%2==0)

addeven+=i;

else

addodd+=i;

}

printf("\nThe sum of odd numbers is %d \nThe sum of even numbers is %d\n",addeven,addodd);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

// simple C++ program that calculate sum or odd and even numbers #include


void main()
{
int n, odd = 0, even = 0;
while (std::cin >> n)
(n%2?odd:even)+=n;
std::cout<<"odd: "<<<"even: "<}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Summation of odd numbers: #include <stdio.h>

int main()

{

int num;

int total = 0; for(int i=1; i<=10; i++)

{

printf("Enter the number:\n");

scanf("%d", &num); if(num % 2 != 1)

continue;

else

total = total + num;

} printf("\nTotal number = %d\n", total); return 0;

}

Written by Simply. Hope this helps!

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Program to Find Sum of Odd & Even Numbers Using Functions

#include
#include
int j;
void main()
{
int sum_odd(int b[],int m);
int sum_even(int b[],int m);
int i,n,a[20];
clrscr();
printf("How many Numbers : ");
scanf("%d",&n);
printf("Enter n Numbers : \n");
for(i=0;iscanf("%d",&a[i]);
printf("Summation of Odd Numbers : %d\n",sum_odd(a,n));
printf("Summation of Even Numbers : %d",sum_even(a,n));
getch();
}
int sum_odd(int b[],int m)
{
int sum=0;
for(j=0;j{
if(b[j]%2!=0)
sum+=b[j];
}
return sum;
}
int sum_even(int b[],int m)
{
int sum=0;
for(j=0;j{
if(b[j]%2==0)
sum+=b[j];
}
return sum;
}

Output :

How many Numbers : 10
Enter n Numbers :
1
2
3
4
5
6
7
8
9
10
Summation of Odd Numbers : 25
Summation of Even Numbers : 30

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Is this what you mean?

#include

int main(){

int number;

printf("Enter any integer: ");

scanf("%d",&number);

if(number % 2 ==0)

printf("%d is even number.",number);

else

printf("%d is odd number.",number);

return 0;

}

Sample output:

Enter any integer: 5

5 is odd number.

Code 2:

1. Display odd numbers in c

2. How to print odd numbers in c

#include

int main(){

int number;

int min,max;

printf("Enter the minimum range: ");

scanf("%d",&min);

printf("Enter the maximum range: ");

scanf("%d",&max);

printf("Odd numbers in given range are: ");

for(number = min;number <= max; number++)

if(number % 2 !=0)

printf("%d ",number);

return 0;

}

Sample output:

Enter the minimum range: 1

Enter the maximum range: 20

Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19

Code 3:

1. Even and odd numbers program in c

2. C program to find even or odd

#include

int main(){

int number;

int min,max;

printf("Enter the minimum range: ");

scanf("%d",&min);

printf("Enter the maximum range: ");

scanf("%d",&max);

printf("Odd numbers in given range are: ");

for(number = min;number <= max; number++)

if(number % 2 !=0)

printf("%d ",number);

printf("\nEven numbers in given range are: ");

for(number = min;number <= max; number++)

if(number % 2 ==0)

printf("%d ",number);

return 0;

}

Sample output:

Enter the minimum range: 1

Enter the maximum range: 20

Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19

Even numbers in given ranges are: 2 4 6 8 10 12 14 16 18 20

Code 4:

1. Sum of odd numbers in c

#include

int main(){

int number;

int min,max;

long sum =0;

printf("Enter the minimum range: ");

scanf("%d",&min);

printf("Enter the maximum range: ");

scanf("%d",&max);

for(number = min;number <= max; number++)

if(number % 2 !=0)

sum = sum + number;

printf("Sum of odd numbers in given range is: %ld",sum);

return 0;

}

Sample output:

Enter the minimum range: 1

Enter the maximum range: 100

Sum of odd numbers in given range is: 2500

Code 5:

1. Sum of odd and even numbers c program

#include

int main(){

int number;

int min,max;

long odd_sum =0,even_sum = 0;

printf("Enter the minimum range: ");

scanf("%d",&min);

printf("Enter the maximum range: ");

scanf("%d",&max);

for(number = min;number <= max; number++)

if(number % 2 != 0)

odd_sum = odd_sum + number;

else

even_sum = even_sum + number;

printf("Sum of even numbers in given range is: %ld\n",even_sum);

printf("Sum of odd numbers in given range is: %ld",odd_sum);

return 0;

}

Sample output:

Enter the minimum range: 1

Enter the maximum range: 10

Sum of even numbers in given range is: 30

Sum of odd numbers in given range is: 25

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

void odd();

void main()

{

printf("hello");

odd();

printf("i am in main");

printf(" bye...");

getch();

}

void odd()

{

int i,n;

clrscr();

printf("enter any number");

scanf("%d",&n);

printf("\nodd number upto the given number are");

for(i=1;i<=n;i++)

{

if(i%2!=0)

{

printf("%d\t",i);

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Since the function requires two return values we can either use two separate functions or we can use two output parameters in a single function. The latter is the best option as it means the return value can be used to indicate if the function succeeded or failed. For instance, if you passed the value 0, the function should fail, returning false. Otherwise it should return true and the output parameters will contain the two sums. We don't need to consider negative values since we can pass an unsigned value.

Here's the function we need.

bool sum_odds_evens(unsigned num, unsigned& odds, unsigned& evens)

{

if (num==0) return false;

odds = 0;

evens = 0;

do

{

if (num & 0x1)

odds += num;

else

evens += num;

} while (--num > 0);

return true;

}

Note that the declaration accepts a constant unsigned value and two positive references. We have to pass the output parameters by reference otherwise we cannot update the actual parameters passed by the caller. We could pass pointers instead, but pointers might be NULL (in which case we'd need to test for that possibility) whereas references must not be NULL (if they are, then your entire program is invalid).

The first line in the body simply tests if the given argument num is zero and, if so, returns false, indicating the function failed. Note that the output parameters, odds and evens, are not altered in any way at this point.

If num is greater than zero then we proceed to the function proper, where we initialise odds and evens to zero. This is important because odds and evens might already have values greater than zero, and that would really muck up our sums.

Next, we start a do-while loop. Note that if we get this far then we already know that num has a value greater than zero, thus we know we must iterate the loop at least once. Hence we use a do-while loop rather than a while loop.

Upon each iteration of the loop, we evaluate the expression (num & 1). This is a sneaky (and very efficient) method of determining if the value is odd or even. The bitwise AND operator (&) allows us to examine the individual bits in any value. The value 1 in binary is 00000001 and if we AND this value with any other value, the result can only be 00000001 or 00000000. In reality there are more leading zeroes of course, but it does not matter because no matter how many leading zeroes there actually are, the result can only ever be 1 or 0 in decimal. Moreover, it does not matter which bits are set in the other value -- the result will always be 1 or 0. This is because we are only ANDing the least significant bit. If the result is 1, the bit is set, otherwise it is not set. In other words, 1 & 1 is 1, but 1 & 0 is 0 (note we're not adding values, we're ANDing values at the bit level). Any value that has the least-significant bit set must be odd. The least-significant bit is at index 0, thus it is known as bit-0. Bit-1 represents the presence or absence of the decimal value 2, while bit-2 represents the decimal value 4. Each subsequent bit is double the previous bit (8, 16, 32, 64, 128, etc) and is therefore even. Remember that to convert any binary value to decimal, we simply convert each set bit to its equivalent decimal value and add them all together. If we add any combination of even numbers together, the answer is always even. So if bit-0 is set, the value must be odd because bit-0 is the only bit that represents an odd decimal value (1). And if it is not set, it must be even. There's no need to test any of the other bits since we're only interested in bit-0.

Of course, another way to achieve the same thing is to evaluate the expression (num % 2) using the modulus operator (%). That is, divide by two and examine the remainder (which will again be 1 or 0). While much easier to understand than a bitwise AND, integer division at the binary level is far more complex than simply ANDing two bits together. That said, a good compiler will replace the expression (num % 2) with (num & 1), but you must never assume all compilers work this way. If you want to ensure maximum efficiency on all compilers, be as explicit as you possibly can.

Having determined if bit-0 is set or not, we can then add the num to the appropriate output parameter, evens or odds. Note that we use the += operator. This is simply C++ shorthand for odds = odds + num, or evens = evens + num. Both forms do exactly the same thing and one form is no more efficient than the other. As a C++ programmer, get into the habit of using shorthand notation. Longhand is arguably easier to understand when you're learning the language, but shorthand saves a lot of keystrokes and is no less readable.

We then come to the end of the loop where we test the value of num after subtracting 1. Note that --num is the prefix decrement operator and is the same as saying num -= 1 or num = num - 1. Again, C++ shorthand notation saves many keystrokes. However, do not confuse --num with num--, the postfix decrement operator. num-- also subtracts 1 from num but evaluates the value of num BEFORE the subtraction. We want to evaluate the value of num AFTER subtraction so we use the prefix decrement operator. The while statement simply checks if the new value of num is greater than 0 and, if so, we immediately start another iteration of the loop. But if num is zero, the loop ends. Thus we will have performed exactly one iteration for each value of num from 1 to the original value of num (albeit in reverse). When num is 1, when we reach the while statement at the end of the loop and subtract 1, num will be zero and we can end the loop.

The final statement returns the value true, thus signifying the function was successful, and the odds and evens values are in a valid state. Note that there is only one fail point at very beginning of the function. If we pass the fail point, the return value is always true regardless of the initial value of num.

Now that we have our function and fully understand its code, let's quickly put it through its paces by completing the program:

#include<iostream> // implements std:cout and std::endl

// Our hero function!

bool sum_odds_evens(unsigned num, unsigned& odds, unsigned& evens)

{

if (num==0) return false;

odds = 0;

evens = 0;

do

{

if (num & 0x1)

odds += num;

else

evens += num;

} while (--num > 0);

return true;

}

// Main entry point of the program: tests the function

int main() // always return an int from main in C++

{

// Initialise the output parameters (actual arguments).

unsigned odds = 0;

unsigned evens = 0;

// Test the function for all values in the range 0 to 10 inclusive.

for (unsigned num=0; num<=10; ++num)

{

// Remember to check the return value!

if (sum_odds_evens (num, odds, evens) == true)

{

// Print the result.

std::cout << "For all values 1 to " << num;

std::cout << " the sum of odd values is " << odds;

std::cout << " and the sum of even values is " << evens;

std::cout << std::endl;

}

}

// No need to return anything as the main function

// implicitly returns the integral value 0 (no error)

// unless explicitly coded otherwise. All compilers

// must support this feature.

}

Output:

For all values 1 to 1 the sum of odd values is 1 and the sum of even values is 0

For all values 1 to 2 the sum of odd values is 1 and the sum of even values is 2

For all values 1 to 3 the sum of odd values is 4 and the sum of even values is 2

For all values 1 to 4 the sum of odd values is 4 and the sum of even values is 6

For all values 1 to 5 the sum of odd values is 9 and the sum of even values is 6

For all values 1 to 6 the sum of odd values is 9 and the sum of even values is 12

For all values 1 to 7 the sum of odd values is 16 and the sum of even values is 12

For all values 1 to 8 the sum of odd values is 16 and the sum of even values is 20

For all values 1 to 9 the sum of odd values is 25 and the sum of even values is 20

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include <stdio.h>

int main (void)

{

printf ("even: %d odd: %d\n", (100-2)/2 + 1, (99-1)/2 + 1);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

printf ("%d\n", (1+99)/2*50).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program using function with argument and return value to find the sum of odd and even series?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


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.


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.


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);}


Discontinuous function in fourier series?

yes a discontinuous function can be developed in a fourier series


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

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


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

Sunday @ 9:00 EST in early 2009


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


What year will Green Lantern the Animated Series return?

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


When did Point of No Return - TV series - end?

Point of No Return - TV series - ended on 2003-12-12.