Asked By
Rabia
120 points
N/A
Posted on - 04/26/2011
Hi,
I need a program that will calculate the sum of even numbers in an array. My assignment is to develop a program that will show the output of the sum of even no’s in an array. Can anyone help me on this please?
Answered By
Rabia
120 points
N/A
#88385
Output needed for calculating sum of even arrays using C Programming
No Dear,
The program will take 10 inputs from the user that will be stored in an array. Here you are not using an array. Please give me a concept of an array to do this work. How can I take input from the user and how will it be saved in an array?
Answered By
Sahil
0 points
N/A
#88386
Output needed for calculating sum of even arrays using C Programming
You will take the input in the array as you take input in a single variable. For example cin>>a; is taking input from user and saving that input value in a. Here you will use for loop for taking input of 10 values from user and save it in array as follows:
For(i=0;i<=9;i++)
{
Cout<<”Enter “<<i<<” Value in array”<<endl;
Cin>>arr[i];
}
Output needed for calculating sum of even arrays using C Programming
Complete solution of this problem is as follows. Here I am not going to define you the details, you need to read your notes to clear the concepts. If you need more help then I am here for you dear.
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[i];
int I,sum=0;
clrscr();
For(i=0;i<=9;i++)
{
Cout<<”Enter “<<i<<” Value in array”<<endl;
Cin>>arr[i];
If(arr[i]%2==0)
Sum=sum+arr[i];
}
cout<<”Sum of even Numbers is = “<<sum<<endl;
getch();
}
Answered By
Rabia
120 points
N/A
#88388
Output needed for calculating sum of even arrays using C Programming
I am going to be a fan of your programming. Let me thank you especially for your help – no doubt you are a good programmer. One more thing, please: what will be the change in working if I want to calculate the sum of odd numbers instead of even numbers?
Once again, Thanks a lot sir.
Answered By
Sahil
0 points
N/A
#88389
Output needed for calculating sum of even arrays using C Programming
I must say thumbs up to you Mark Jason; Superb.
Output needed for calculating sum of even arrays using C Programming
There will only be one change; that is: If(arr[i]%2!=0) instead of If(arr[i]%2==0).
Output needed for calculating sum of even arrays using C Programming
Hey,
The above Mark's code won't even compile. Check this out
First and foremost, you cannot declare an array like this –
int arr[i];
prog.cpp:5: error: ‘i’ was not declared in this scope
You have to initialize i to declare it in that way.
Second,
Sum=sum+arr[i];
prog.cpp:15: error: ‘Sum’ was not declared in this scope
You cannot use Sum when you have initialized
int sum = 0;
It becomes a different variable, and you cannot use it.
These are some of the basic programming errors that any coder can identify.
There are few more errors, but I'll leave it to you.
Regards,
Sanders Danny