-1

• Fill the array

• Total the odds and evens

• Compare the totals and display the report.

Not Yet Answered # please help

Stefan_1 3 Discussion Starter Bradley_3 tinstaafl 839 Xavier_5 Stephanie_3

-1

• Fill the array

• Total the odds and evens

• Compare the totals and display the report.

0

You joined 16mins. ago, created this first post on the forum and you are requesting a whole program to be made by us ? i mean ....

0

heres what i have

```
/*
* C program to find the sum of odd and even numbers from 1 to N
*/
#include <stdio.h>
void main()
{
srand(time(NULL));
int arry[10];
int i;
int num;
int odd_sum = 0;
int even_sum = 0;
for(i = 0; i < 10; i++){
arry[i] = rand()%5+0;
}
for(i = 0; i <10; i++){
printf("\nElement[%d]: %d", i, arry[i]);
}
for (i = 1; i <= num; i++){
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("\nSum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
```

0

As pointed out your problem is here:

```
for ( i = 1; i <= num; i++ ) {
if ( i % 2 == 0 )
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
```

Instead of using `i`

you should using the index of `arry[i]`

:

```
for ( i = 1; i <= num; i++ )
{
if ( arry[i] % 2 == 0 )
{
even_sum += arry[i];
}
else
{
odd_sum += arry[i];
}
}
```

Something else to consider. Instead of printing the array and checking for odd and even numbers in extra loops, you can print and check when they're entered into the array:

```
void main()
{
srand( time( NULL ) );
int arry[10];
int i;
int num;
int odd_sum = 0;
int even_sum = 0;
for ( i = 0; i < 10; i++ ) {
arry[i] = rand() % 5;
printf( "\nElement[%d]: %d" , i , arry[i] );
if ( arry[i] % 2 == 0 ) {
even_sum += arry[i];
}
else {
odd_sum += arry[i];
}
}
printf( "\nSum of all odd numbers = %d\n" , odd_sum );
printf( "Sum of all even numbers = %d\n" , even_sum );
}
```

*Edited 1 Week Ago by tinstaafl*

0

I would change tinstaafl's line 12 back to `if (i % 2 ==0)`

since the problem was adding `i`

contents instead of `arry[i]`

contents where the random values lay.

0

well..doubt anyone would do it for you for free...that's a lot of work...surely you can try asking guys at https://www.chegg.com/tutors/Programming-online-tutoring/ or http://yourhomeworkhelp.org/do-my-programming-homework/ for help....still that's not a solution...if you actually want to learn how to programm try do it on your own, if you run into some errors, figure out how to correct them or just ask guys here..best

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`