r/programminghelp • u/Jarf710 • Nov 26 '20
C++ A little help with some homework
So, our homework is to create a program that lists every even number from 0 to 100 and then show the sum of every iteration (1+3+5+7+9+...+99).
this is how it's going:
#include <stdio.h>
int main()
{
int x,y,z;
x=1;
while(x<100)
{
printf("%i, ",x);
x=x+2;
}
y=x+x;
printf("%i",y);
}
I have no idea what to do for that last part of the program.
5
Upvotes
1
u/TreeBaron Nov 26 '20
It's clear from your approach you're probably missing some information so I will fill you in a bit.
You use a while loop but there is also something called a for loop which would be a better fit for this problem. What you have isn't wrong but it's a less-common approach to this problem.
Here is an example of a for loop that goes to 100.
Secondly I like that you iterate by 2 to print even numbers, however there is an operator (%) called a modulus operator. This is similar to division, but returns the remainder of a division instead. Thus if you check if (i % 2 == 0) you can see if a number is even.
The instructions for the last part seem a little unclear to me. They could be meaning several things there so I'd ask for clarification if you can.
I think they want a string showing all the even numbers from one to one hundred and then print the sum of all the numbers? In which case I think they want something like this...