-
February 12th, 2012, 02:37 AM
#1
While loops to print even numbers
Hi
Good Morning Follows
_________________
am trying to write a program to get a number from user and then print even numbers as well as 0,2,4 and so on
what changes i should do ? any tips or hints will be useful
PHP Code:
# include <iostream>
using namespace std;
int main () {
int n,i;
cout << " Enter number ";
cin >>n;
i = 0;
while ( i%2 == n) {
cout <<i<<" ";
i = i+1;
}
system ("PAUSE");
return 0;
}
-
February 12th, 2012, 03:57 AM
#2
Re: While loops to print even numbers
If you use your debugger and single step every line you will quite easily find what's wrong.
-
February 12th, 2012, 04:17 AM
#3
Re: While loops to print even numbers
the funny thing that am not that smart in math
i know that if the number i put % 2 ==0 i will get even right ?
i will try and find out what wrongs thanks =)
-
February 12th, 2012, 04:22 AM
#4
Re: While loops to print even numbers
is this correct little or i make it worse ?!
PHP Code:
# include <iostream>
using namespace std;
int main () {
int n,i;
cout << " Enter number ";
cin >>n;
i = 0;
while ( n % 2 == i ) {
cout <<i<<" ";
i = i+2;
}
system ("pause");
return 0;
}
-
February 12th, 2012, 04:26 AM
#5
Re: While loops to print even numbers
As I posted before, use the debugger. You will never learn how to program if your method is like:
1. Throw a number of statements in the compiler/linkers face.
2. If the compiler/linker accepts the statements ask why it's not working in a forum.
-
February 12th, 2012, 04:52 AM
#6
Re: While loops to print even numbers
i use debugger there was no error ,, i know the mistake must be in the while loop
am trying to understand it but i cant figue it out ,, fix my while loop statement !
-
February 12th, 2012, 05:29 AM
#7
Re: While loops to print even numbers
You use the debugger to check that every single line of code do what you expect it to do. If it doesn't you have an error.
Check your while condition again. What results will you get from n % 2? How do those values compare to i? For what values of i will the while loop be executed? How does that conform with your intent?
-
February 12th, 2012, 05:30 AM
#8
Re: While loops to print even numbers
-
February 12th, 2012, 06:43 AM
#9
Re: While loops to print even numbers
is that correct ?
PHP Code:
#include <iostream>
using namespace std;
int main()
{
int x = 0;
while (x < 10)
{
cout << x << endl;
x = x + 2;
}
system("pause");
return 0;
}
-
February 12th, 2012, 06:50 AM
#10
Re: While loops to print even numbers
Does it seem to work for you?
(By the way, please indent your code properly. It will help you in the future.)
-
February 12th, 2012, 06:53 AM
#11
Re: While loops to print even numbers
That depends on what you seek.
That code will print out
0
2
4
6
8
so if that is what you want it is correct.
-
February 24th, 2012, 06:40 AM
#12
Re: While loops to print even numbers
As S_M_A said - if that's what you need, than it's correct.
But, you know, if you want to write code that works, it's a good idea to know what the code you're writing actually does. Maybe I can help you a bit with that...
Code:
int n, i; // declares n and i as integer variables
cout << " Enter number "; // guess you know what this does
cin >>n; // puts user input in n
i = 0; // initializes i to 0
while (condition) { // repeats thing_that_needs_be_done as long as condition is TRUE
thing_that_needs_be_done;
}
Additional tips:
- x % y - returns x mod y
- x == y - returns TRUE or FALSE, indicating if x equals y or not, respectively
- x = y - assigns the value of y to x. As you may have noticed, '=' is not the same as '=='...
- x % y == z - returns returns TRUE or FALSE, indicating if x mod y equals z, or not.
-
February 24th, 2012, 07:09 AM
#13
Re: While loops to print even numbers
Originally Posted by jacksparrow
Good Morning Follows
I'm still waiting for the good morning.
Do it in two steps. First write a while loop that loops all integers between 0 and the value the user entered. Then within this loop apply the even-test on every integer and print if applicable, like,
First,
Code:
i = 0;
while (i <= n) { // i will assume all values between 0 and n.
// print i for testing purposes
i = i+1;
}
Then,
Code:
i = 0;
while ( i <= n) {
if (i%2 == 0) { // check whether i is even (evenly divisible by 2)
// print i
}
i = i+1;
}
-
February 25th, 2012, 01:30 PM
#14
Re: While loops to print even numbers
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|