C++ questions

The following code segment is supposed to write out the even numbers between 1 and 15


Int n;
N= 2;
While (n!= 15)
{
N=n+2;
Cout <<n<< endl;
}



What is the output of it?
And also if it's wrong what's my mistake so it works as is supposed to be ?
C++ is case sensitive.
Hmm sorry I was in hurry..
Besides case sensitive ?
N will never equal 15, so the loop will never end. It should be N <= 15.
Anything else besides that problem?
I think there are two mistakes..
What is the value in total after this loop is executed?

Total =0;
For (i=1; i<= 5; i++)
{ total =total +i;
}
What is the output of this code?

For (i=1; i<=10; i=i+2)
Cout<<i;
Is the last one output suppose to be
13579?
closed account (18hRX9L8)
I know this might be a stupid question, but 1,3,5,7,9 are odd. Don't you want your program to print even numbers (2,4,6,8,10,12,14)?

Sources:
http://www.aaamath.com/g25a2-evenodd.html


Code:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

main()
{
      for(int i=2;i<15;i=i+2)
      {
           std::cout<<i<<std::endl;
      }
   
getchar();
}
Last edited on
I know this might be a stupid question, but 1,3,5,7,9 are odd. Don't you want your program to print even numbers (2,4,6,8,10,12,14)?


That's why he started with N=2 and added +2 each time
closed account (18hRX9L8)
I knew it was a stupid question.
Last edited on
Topic archived. No new replies allowed.