### loop condition

Pages: 12
can you guyz help me with this?

"write a program that will display the sum of even numbers and the product of
odd numbers between 1-100"

so i am assigned to do that using the while or for loops

i am wondering is it posible to make the console print it like this:

2+4+6+8+10...+98+100 = (the total sum of all even numbers)

and

1*3*5*7*9...*96*99 = (the toal prod of all odd numbers)

assuming that the ... will still display the numbers that are supposed to be
there?
 ``1234567891011`` ``````int sum = 0; int product = 1; for ( int i = 1; i <= 100; i++ ) { if ( i & 1 ) product *= i; else sum += i; } std::cout << "sum = " << sum <, std:;endl; std::cout << "product = " << product << std::endl;``````
The product of all odd numbers between 1-100 is

2725392139750729502980713245400918633290796330545803413734328823443106201171875

and has 79 digits...

Also, the product of all even numbers between 1-100 is

34243224702511976248246432895208185975118675053719198827915654463488000000000000

and has 80 digits...
Last edited on
The product of all odd numbers is going to be gigantic and will overflow a 64 bit (or 128 bit) integer, so you will need a library that allows you to represent large integers, such as http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic or http://gmplib.org/ if you want to see your answer.
In fact this product is similar to factorial.The maximum number the factorial of which can be stored in an object of type int provided that the type int occupies 4 bytes is equal to 12.:)
Last edited on
i think i misunderstood you or you misunderstood me XD

anyways what am i expecting in the output will be like this

2
4
.
.
.
98
100
the total sum is XX

1
3
.
.
.
96
99
the total product is XX

am expecting to see this in console as an output..

i do have "this" idea on how am i will be able to put the numbers on output but i am wondering if how am i be able to do the arithmetic operation without writing sum = 2+4+6+....+98+100... do you guyz get my point? XD
 @xeimaster 1 3 . . . 96 99 the total product is XX am expecting to see this in console as an output..

I have understood nothing. Do you understand yourself what you are writing and do you read what others already wrote?
oh i think we have a misunderstanding here XD anyways my real problem would be like this

i know how will i be able to display those odd and even numbers

the problem is creating the arithmetic operation for the sum and product

i mean the typical way would be i writing it like this

SUM = 2+4+6+8... till +98+100 right?

is there such a way to avoid me writing it that way? like a shortcut or somewhat?
 `i know how will i be able to display those odd and even numbers`

OK - how do you do it?

 the problem is creating the arithmetic operation for the sum and product

If you have an int A, and you want to add 2 to it, how do you do it?

 ``12345`` ``````int A = 3; // what goes here? cout << A << "\n";``````

 `5`

Then generalize it!

Andy
Last edited on
 @xeimaster the problem is creating the arithmetic operation for the sum and product i mean the typical way would be i writing it like this SUM = 2+4+6+8... till +98+100 right? is there such a way to avoid me writing it that way? like a shortcut or somewhat?

One more do you read what was already written in the thread? I showed how the operations are performed.
If you want to display the number stored in an array
I meant to say store it using a char array
I completely misunderstood your question I think I Understand what you're saying. There is a way to write it like that. It's called a for loop.

example

int sum =0;
for(int i = 0; i < 20; i++)
sum += i;

So you don't have to write sum = 1+2+3...
You don't need a computer to find the sum of all even numbers from 2 to n; it is equal to n * (n + 2) / 4.

Last edited on
You don't need a computer for a lot of things, it just sometimes saves a lot of time, plus even in cases like this, means you don't have to derive formulas.
Why the blank post??
I think i understand what you want and it is not very hard but you cod not communicate it correctly.
 ``123456789101112131415161718`` ``````int sum = 0; int product = 1; int i; for ( i = 1; i <= 100; i++ ) { if ( i & 1 ) product *= i; else sum += i; } for ( i=2; i<100; i=i+2) { cout<

I hope this is the code you want. I am sorry about the grammar , my keyboard is almost broken.
@fluture

Does it work this code? Especially the line 6?...
Last edited on
condor, line 6 checks if i is odd (bitwise and operator).
Pages: 12