Hello im really strugling to make this simple program work.
write a program that shows on the screen the result of the * of the even numbers from 10 to 100.I must use the for loop in order for the program to work but i can't seem to make it work,what do i do wrong?Sry for the bad english =]
#include<iostream>
usingnamespace std;
int main()
{
int x=10;
if(x%2==0)
{ int s;
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x;x<=100;x++)
{ s=10*x; cout<<"s="<<s<<endl; }
}
else
{cout<<"The numbers are not even"<<endl;}
system("pause");
return 0;
}
From what I see here you int x as 10...but when you call it in your for statement you tell S to be 10*x...which would be 10 times 10 = 100...that's just to start with. I messed around with a little and let me tell you I am NO expert by far...and I'm sure others have a far easier or better solution. But here is what I wrote and works for all even numbers 10-100.
#include<iostream>
usingnamespace std;
int main(){
int x=2;
int s;
if(x%2==0){
cout<<"The * of the even numbers from 10 to 100"<<endl;
for(x;x<=100;x++){
s = x * 2;
if(s<10 || s>100){
}else{
cout << "s="<< s << endl;
}
}
}else{
cout<<"The numbers are not even"<<endl;
}
}
Your program shows all even numbers from 10 to 100 but does not multiply them.I am absolute beginner and i'm just struggling to understand how your program is showing all the even numbers.It's absolutely correct but i don't get it why x=2?And why s=x*2?This for loop seems so confusing....... :|
I wanted to use as much as your code as possible. If I would have done int x or int x = 10 it just would have generated the numbers slightly different. If you look at your for loop, x is basically your loop counter with your conditions but you also wanted to use it in your equation.
So if you int = 4 the first number it would generate would be 8 because have s = x * 2...now on the flip side you can say well use 5 cause that would equal 10...problem with that was that 5 is not an even number, so right off the bat in your if statement you say only run this statement IF x&2 equals 0...well it doesn't so it would skip the entire IF statement and move to ELSE and give "The numbers are not even". So I had to start with an even number that had a product of less than or equal to 10...so only 2 and 4 were usable.
Also if you look at your for statement again, you are simply telling it to run IF X is less than 100. So it basically ran 100 times, that's why your final number was 1000 (10x100 = 1000) but again it was really only showing the even numbers it calculated (10x10, 10x11, 10x12 etc etc etc). So I lowered your multiplier of X to 2 (2x2, 2x3, 2x4, etc etc).
Initially when I ran it I was getting everything to print out on the screen...which you did not want, so I had to add in another IF statement. That if statement basically tells the program NOT to print numbers less than 10 or greater than 100, if it has them it just does nothing, else it prints them out.
I explained the best I could in how and why I chose the code I did...but I will say that I have a really huge feeling that it is NOT the best way to go about it and I'm sure there is a much easier/simpler way of doing it. But I saw this as a problem I actually might be able to solve and decided to throw it in. :)
So you want 10 * 12 * 14 * 16 ..... * 98 * 100 - right?
This can be done with a very simple for loop - about 4 lines I reckon.
First, test the starting number is even - which you have done.
Now think about how a for loop works - this is psuedo code:
1 2 3 4 5 6
for (start value ;end condition ; increment expression) {
//calculate the sum here -use the *= operator
//optionally print some intermediate value here
}
//Print the answer here
See...told you there was an easier way...lol I made the code MUCH smaller and probably even easier to understand.
1 2 3 4 5 6 7 8 9 10 11
#include<iostream>
usingnamespace std;
int main(){
int x;
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=10;x<=100;x+=2){
cout << x << endl;
}
}
Though I will say, there is no multiplication in this code...
Thanks to both of you i finally really understand how the for loop work. :D
I thought i understand it the first time i learned it but i was wrong.Now i got it.
I think the code for the * of the numbers should be
Good work helios - good to see someone is on the ball!!
Hopefully the OP can figure how to do it from 10 to 20 say.
@Edwards
Good to see you have understood some what I said earlier. Although you haven't multiplied them.
The other thing is to avoid using magic numbers, I would have the upper limit as a const at the start of the program, then refer to this in the for loop.
I would have a return 0; before the end of main.
I would also either have using std::cout; instead of usingnamespace std; or use std::cout every time you want to use cout. then you won't need the usingnamespace std;
Ok, I think this is the best I got in me atm lol...for loop works, put in multiplication, and is 10-100 even numbers only...
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
usingnamespace std;
int main(){
int x;
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=5;x<=50;x++){
cout << x*2 << endl;
}
return 0;
}
I was also under the impression that "return 0;" was not a needed part of code anymore because if it is not available C++ understands that and automatically assumes that you want to return 0. Am I wrong on this?
Edit: Also, duh, just realized I can remove a line of code. int x can be added in the for loop for(int x=5; x<=50; x++)
#include<iostream>
usingnamespace std;
int main(){
int x;
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=5;x<=50;x++){
cout << x*2 << endl;
}
return 0;
}
But can i do it this way too?Or it's not correct like that?Thats the code i came upon myself after the help from TheIdeasMan.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
usingnamespace std;
int main(){
int x;
int s;
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=10;x<=50;x+=2){
cout << s=x*=(x+2) << endl;
}
return 0;
}
I'm truing to multiply the even numbers from 10 to 50.For example 10*12*14*16........*50.I know it wont work beacause the cpu can't multiply sum this big but it's just a program i have for training.So i don't know if this is right but i'm pretty sure it should be fine.
1 2 3 4
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=10;x<=50;x+=2){
cout << s=x*(x+=2) << endl;
}
s is just an integer it does not mean sum.My idea is that s=10*14*16.....50,and in the end the program will display s,but i guess it can be done without s like that
1 2 3 4
cout<<"The * of the even numbers from 10 do 100"<<endl;
for(x=10;x<=50;x+=2){
cout << x*(x+=2) << endl;
}