Perform the calculation on each string sum them up and print whether their sum is EVEN or ODD

The general idea is ok, that is you go through the list look if the number itself is even (if %2 == 0) and then either add it to the one or the other group.

Though there are a number of things that simply don't work this way in python.

calculate_odd_even(odd_number, even_number):

The value that you'd want to pass as parameter to the function would be the list of numbers. using odd_number and even_number here is especially useless as you directly set them to 0 on the next line. So whatever value is passed to the function will be erased by that.

t1 = tuple()

You seem to want to add the numbers to a list here. But a) tuples are immutable so adding stuff to them simply doesn't work to begin with and b) even if you would replace that with [] or list() you'd still be left with a list but you want a sum. So you'd still need to take sum(t1) in the end.

Next up:

for i in number_list:

goes through the elements of the list so in the first iteration i will be 1 then 2 and so on. So it is NOT an index in the way you used it in this line:

even_number = even_number + number_list[i]

So let's go through the first iteration of the loop. In that case i would be 1. Which would trigger the else path as (1%2=1 not equal 0). And there you would attempt to append odd_number+number_list[i]. Where the value you'd append (if that were possible) would be 0+2 = 2. Why 2? Because i=1 and therefor you'd take the 2nd value in your number_list as you start counting on 0. So even if all of that would have worked you'd have taken the exact opposite number from your list and for the last number it would crash as there is no index 6 for a list of length 6 (again counting starts at 0).

And again even if that would have worked the odd_number+number_list[i] would just add basically the next number in the number_list to a new list of numbers. Not to mention that the odd_number part is redundant as it is 0 and stays that way.

Last but not least a return statement exists the function so anything that is happening after a return statement is ignored. So your second return statement is treated as if it wouldn't exist. Fortunately for you, you don't have to do any hassle to return 2 values in a tuple just use:

return [valueA] [valueB]

to return 2 values in a tuple. So for example return 2, 3 would return (2,3).

Though you could make that a whole lot easier in the first place:

number_list = [1, 2, 3, 4, 5, 6] def calculate_odd_even(num_list): odd_number = 0 even_number= 0 for i in num_list: if i%2 ==0: even_number = even_number + i else: odd_number = odd_number + i return even_number, odd_number my_tuple = calculate_odd_even(number_list)

In this tutorial, we will discuss how to use the  C program to calculate the sum of odd and even numbers in an array.

In this article, we are going to learn  how to  calculate the sum of odd and even numbers in the C programming language

What is an even or odd number?

When any integer ends in 0,2,4,6,8 and it can be divided by two with the remainder of zero, it is called as an even number.

Example of even numbers – 34,-64,78,788

When any integer ends in 0,1,3,5,7,9 and it cannot be divided without a remainder, it is called as an odd number.

Example for odd numbers – 33,-69,75,785

Here, we can use a modular operator to find odd or even number in the array.

if n%2==0,  n is an even number – if the number is even, the remainder is zero.

if n%2==1,  n is an odd number – if the number is odd, the remainder is one.

Calculating the sum of odd and even numbers using “for loop”

Program 1

This program allows the user to calculate the sum of odd and even numbers in the given array using “for loop”

#include <stdio.h> #include <stdlib.h> int main() { int arr[6]={5,10,15,20,25,30}; int i,oddSum=0,evenSum=0; for(i=0; i<6; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of even numbers are: %d",evenSum); getch(); return 0; }

When the above code is executed, it produces the following results

The sum of odd numbers are: 45 The sum of even numbers are: 60

Methodology:

  1. First, define an array with elements.
  2. Next, declare and initialize three variables to find sum as oddSum=0, evenSum=0 and i;
  3. Then, use the “for loop” to take the elements one by one from the array.
  4. The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by  “else statement”.
  5. The “else statement”  finds odd numbers and adds to oddSum.
  6. Finally, the sums of odd numbers and even numbers are displayed.

Program 2

This program allows the user to choose any value for array length and input values into the array.  The program will calculate the sum of odd and even numbers from the array using “for loop”.

#include <stdio.h> #include <stdlib.h> int main() { int oddSum=0,evenSum=0; int i,size; printf("Enter the size of the array: "); scanf("%d",&size); int arr[size]; printf("Enter the Array elements: "); for(i=0; i<size; i++){ scanf("%d",&arr[i]); } for(i=0; i<size; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of odd numbers are: %d",evenSum); getch(); return 0; }

When the above code is executed, it produces the following results

Enter the size of the array: 6 Enter the Array elements: 13 24 35 46 57 68 The sum of odd numbers are: 105 The sum of even numbers are: 138

Method:

  1. To begin with , declares and initializes three variables: oddSum=0, evenSum=0 and size.
  2. Next, receives input from the user regarding the array length.
  3. Then, defines an array without including the elements.
  4. Afterwards, the elements for an array is received from the user.
  5. Then, using the “for loop”, the elements are added one by one to the array.
  6. Then, using the second “for loop”, elements are picked one by one from the array to determine oddness and evenness.
  7. Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by  “else statement”.
  8. After that, the “else statement” adds to oddSum.
  9. Finally, the sums of odd numbers and even numbers are displayed.

Calculate the sum of odd and even numbers using “while loop”

Program 1

This program allows the user to calculate the sum of odd and even numbers in the given array using “while loop”

#include <stdio.h> #include <stdlib.h> int main() { int arr[6]={10,15,20,25,30,35}; int i,oddSum=0,evenSum=0; i=0; while(i<6){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of odd numbers are: %d",evenSum); getch(); return 0; }

When the above code is executed, it produces the following results

The sum of odd numbers are: 75 The sum of even numbers are: 60

Methodology:

  1. First, define an array with elements.
  2. Next, declare and initialize two variables to find sum as oddSum=0, evenSum=0
  3. Then, use the “while loop” to take the elements one by one from the array.
  4. The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by  “else statement”.
  5. The “else statement”  finds odd numbers and adds to oddSum.
  6. Finally, the sums of odd numbers and even numbers are displayed.

Program 2

This program allows the user to choose any value for array length and input values into the array.  The program will calculate the sum of odd and even numbers from the array using “while loop”.

#include <stdio.h> #include <stdlib.h> int main() { int oddSum=0,evenSum=0; int i,size; printf("Enter the size of the array: "); scanf("%d",&size); int arr[size]; printf("Enter the Array elements: "); i=0; while(i<size){ scanf("%d",&arr[i]); i++; } i=0; while(i<size){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of even numbers are: %d",evenSum); getch(); return 0; }

When the above code is executed, it produces the following results

Enter the size of the array: 6 Enter the array elemts: 10 11 12 13 14 15 The sum of the odd numbers are: 39 The sum of the even numbers are: 36

Method:

  1. To begin with , declares and initializes three variables: oddSum=0, evenSum=0 and size.
  2. Next, receives input from the user regarding the array length.
  3. Then, defines an array without including the elements.
  4. Afterwards, the elements for an array is received from the user.
  5. Then, using the “while loop”, the elements are added one by one to the array.
  6. Then, using the second “while loop”, elements are picked one by one from the array to determine oddness and evenness.
  7. Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by  “else statement”.
  8. After that, the “else statement” adds to oddSum.
  9. Finally, the sums of odd numbers and even numbers are displayed.

Similar post

C program to check whether a number is even or odd

C program to check a number is even or odd using function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

Suggested for you

for loop in C language

while loop in C language

if statements  in C language

Single dim array in C

Postingan terbaru

LIHAT SEMUA