0

Write a program in c to enter 20 number in an array. Determine the sum of odd numbers and even numbers separately

12th Feb 2017, 2:04 PM
ankit pariyar
ankit pariyar - avatar
4 Antworten
+ 4
If you think of it a little bit, then you'll get it!
12th Feb 2017, 2:48 PM
Hsn
Hsn - avatar
+ 3
#include <stdio.h> main(){ int ar[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; int even=0,odd=0; for(int i=0;i<20;i++){ if(ar[i]%2==0) even+=ar[i]; else odd+=ar[i]; } printf("sum of even numbers=%d\n",even); printf("sum of odd numbers=%d",odd); }
12th Feb 2017, 2:57 PM
Hsn
Hsn - avatar
0
thanks
12th Feb 2017, 6:19 PM
ankit pariyar
ankit pariyar - avatar
0
This problem is similar to finding the sum of all odd and even numbers between 1 to using for loop. Here is the sample C program to find sum of all even numbers for(counter = 1; counter <= N; counter++) { /* Even numbers are divisible by 2 */ if(counter%2 == 0) { /* counter is even, add it to sum */ sum = sum + counter; } } Source : http://www.techcrashcourse.com/2015/11/c-program-to-find-sum-of-even-numbers-from-1-to-N.html Here is the sample C program to find sum of all odd numbers for(counter = 1; counter <= N; counter++) { /* Odd numbers are not divisible by 2 */ if(counter%2 == 1) { /* counter is odd, add it to sum */ sum = sum + counter; } } Source : http://www.techcrashcourse.com/2015/11/c-program-to-find-sum-of-odd-numbers-from-1-to-N.html
15th Apr 2017, 5:25 AM
Arun Kumar
Arun Kumar - avatar