the sum

Aug 12, 2014 at 1:05pm
program that will accept a number n and display the sum of all even numbers and the sum of all odd numbers from 1 to n
Aug 12, 2014 at 1:07pm
what have you got so far?
Aug 12, 2014 at 1:12pm
nothing i don't even know on how to start it
Aug 12, 2014 at 1:23pm
Go through all the material that your course has had so far.
Then read http://www.cplusplus.com/doc/tutorial/
Aug 12, 2014 at 1:31pm
tn. I've done with that already and we are now in iterative statements

sorry for my first statement,, i'am just trying to get some help regarding with that question. hope you can help me through
Aug 12, 2014 at 2:05pm
An odd number divided by 2 always produces a remainder equal to 1.
An even number divided by 2 always produces a remainder equal to 0.

To get the remainder of a number we will use the modulo operator %

So I will write a function that will retrun the sum of even numbers.

1
2
3
4
5
6
7
8
9
10
11
int EvenSum(int n)
{
	int EvenSum=0;  // initializing the sum with 0
	for(int i=0; i<=n; i++)
		if (i%2==0)     // if i is even
		{
			EvenSum+=i;   //every time i is even we add i to the sum
		}
		return EvenSum;
}


In main we will call the function

1
2
3
4
5
6
7
8
9
10
int main()
{
	int n;
	cout<<"n="; 
        cin>>n;   
	cout<<EvenSum(n);  // calling the function

	_getch();
}


Using the same idea you can do a function for Odd Numbers.
If you have any questions feel free to answer.
Aug 12, 2014 at 2:31pm
tnx. it really helps me a lot..

i still have a question

.. A program that will compute for n! ( n factorial) which is the product of all numbers from 1 to n.

hope you can still help me with this
Aug 12, 2014 at 2:48pm
its similar to the previous problems

int factorial =1;

for(int i=1; i<=N; i++)
{
factorial = factorial*i;
}
Aug 13, 2014 at 5:37am
I got a bet confuse about the program,, imean how should i write it in
turbo c ?
Aug 13, 2014 at 7:14am
Aug 13, 2014 at 2:42pm
yes turbo c++,
Aug 13, 2014 at 3:16pm
This is a simple factorial program. It doesn't matter what compiler or IDE you're using. bubba89 already gave you the answer. Have you even tried to compile it or are you just waiting for someone to do ALL the work for you?
Topic archived. No new replies allowed.