Ques:-27) Write a program to input ten numbers from keyboard and find the sum of all even numbers and odd numbers

Here’s a C program that inputs ten numbers from the keyboard and finds the sum of all even and odd numbers:

				
					#include"stdio.h"
#include"conio.h"
void main()
{
  int arr[100],i,ev_sum=0,od_sum=0;
  clrscr();
  printf("enter 10 integers:");
  for(i=1;i<=10;i++)
    scanf("%d",&arr[i]);
  for(i=1;i<=10;i++)
  {
    if(arr[i]%2==0)
      ev_sum+=arr[i];
    else
      od_sum+=arr[i];
  }
  printf("sum of even no is %d and odd no is %d",ev_sum,od_sum);
  getch();
}

				
			

Out Put

				
					enter 10 integers: 12 3 45 6 7 88 9 0 23 21 
sum of even no is 106 andodd no is 108
				
			

Another Example

				
					#include <stdio.h>

int main() {
    int numbers[10];
    int even_sum = 0, odd_sum = 0;

    // Input 10 numbers from the user
    printf("Enter 10 numbers: \n");
    for (int i = 0; i < 10; i++) {
        printf("Number %d: ", i + 1);
        scanf("%d", &numbers[i]);
    }

    // Calculate the sum of even and odd numbers
    for (int i = 0; i < 10; i++) {
        if (numbers[i] % 2 == 0) {
            even_sum += numbers[i];
        } else {
            odd_sum += numbers[i];
        }
    }

    // Output the results
    printf("Sum of even numbers: %d\n", even_sum);
    printf("Sum of odd numbers: %d\n", odd_sum);

    return 0;
}

				
			

Explanation:

  1. Array Declaration: An array numbers of size 10 is declared to store the user inputs.
  2. Input Loop: A for loop runs 10 times to take input from the user and store it in the array.
  3. Sum Calculation Loop: Another for loop iterates through the array to calculate the sums of even and odd numbers.
  4. Condition Check: Inside the second loop, each number is checked to see if it is even or odd using the modulus operator (%). If numbers[i] % 2 == 0, the number is even; otherwise, it is odd.
  5. Sum Update: Based on the condition check, the number is added to even_sum or odd_sum.
  6. Output: Finally, the sums of even and odd numbers are printed.

Sample Output:

				
					Enter 10 numbers: 
Number 1: 12
Number 2: 7
Number 3: 5
Number 4: 10
Number 5: 2
Number 6: 3
Number 7: 8
Number 8: 15
Number 9: 6
Number 10: 1
Sum of even numbers: 38
Sum of odd numbers: 31

				
			

You can compile and run this program using a C compiler like gcc. Here’s how you can do it:

  1. Save the program in a file, e.g., sum_even_odd.c.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using gcc:
				
					gcc -o sum_even_odd sum_even_odd.c

				
			

Run the compiled program:

				
					./sum_even_odd

				
			

This will prompt you to enter ten numbers and then display the sum of even and odd numbers.

Scroll to Top