w3resource

Sum all odd values among 5 inputs


Sum all odd values among 5 inputs

Write a C program that read 5 numbers and sum of all odd values between them.

Pictorial Presentation:

C Programming: Read 5 numbers and sum of all odd values between them


C Code:

#include <stdio.h>
int main() {
    int j, numbers[5], total=0; // Declare array to store 5 numbers and variable for total
    printf("\nInput the first number: "); 
    scanf("%d", &numbers[0]);
    printf("\nInput the second number: "); 
    scanf("%d", &numbers[1]);
    printf("\nInput the third number: "); 
    scanf("%d", &numbers[2]);
    printf("\nInput the fourth number: "); 
    scanf("%d", &numbers[3]);
    printf("\nInput the fifth number: "); 
    scanf("%d", &numbers[4]);

    for(j = 0; j < 5; j++) {
        if((numbers[j]%2) != 0) // Check if the number is odd
        {
            total += numbers[j]; // Add odd number to total
        }   
    }

    printf("\nSum of all odd values: %d", total); // Print the sum of odd numbers
    return 0;
}

Sample Output:

Input the first number: 5                                                                            
                                                                                                     
Input the second number: 7                                                                           
                                                                                                     
Input the third number: 9                                                                            
                                                                                                     
Input the fourth number: 10                                                                          
                                                                                                     
Input the fifth number: 13                                                                           
                                                                                                     
Sum of all odd values: 34

Flowchart:

C Programming Flowchart: Read 5 numbers and sum of all odd values between them


For more Practice: Solve these Related Problems:

  • Write a C program to sum all even numbers among five inputs provided by the user.
  • Write a C program to continuously sum odd values from user inputs until a negative number is entered.
  • Write a C program to sum odd numbers that are greater than a specified minimum value from a set of inputs.
  • Write a C program to sum all odd numbers and count the number of odd inputs simultaneously, then display both results.

Go to:


PREV : Count positives and calculate their average from 5 inputs.
NEXT : Print squares of all even numbers up to a given value.

C Programming Code Editor:




Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.