w3resource

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

C Basic Declarations and Expressions: Exercise-29 with Solution

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;
	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) 
		{
		   total += numbers[j];
		}	
    }
   	printf("\nSum of all odd values: %d", total);
	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

C Programming Code Editor:


Contribute your code and comments through Disqus.

Previous: Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values.
Next: Write a C program to find and print the square of each one of the even values from 1 to a specified value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

How to increment a pointer address and pointer's value?

First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.

Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.

Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.

Ref : https://bit.ly/3vMewPt