28 Replies - 3592 Views - Last Post: 03 August 2011 - 09:10 PM
#1
Odd/Even integers
Posted 02 August 2011 - 01:57 AM
The program should contain two ways for printing integers, and automatically produce two outputs (which obviously must be identical); these two ways are in seperate functions.
a. The first way your code should contain the odd/even conditional statements before a while loop, and use a loop increment of two.
b. In the second way the odd/even conditional statement(s) should be inside the while loop, which should use a loop increment of one.
Replies To: Odd/Even integers
#2
Re: Odd/Even integers
Posted 02 August 2011 - 02:04 AM
Have you started making this?
Post your code if you need some clarification..
#3
Re: Odd/Even integers
Posted 02 August 2011 - 02:05 AM
#4
Re: Odd/Even integers
Posted 02 August 2011 - 02:17 AM
I have:
//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3
#include <iostream>
#include <conio.h>
using namespace std;
void printInteger( char );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int myInteger1;
int myInteger2;
cout << "Enter any 2 integers: " << endl << endl;
cin >> myInteger1;
cout << "... and the next one: " << endl << endl;
cin >> myInteger2;
_getch();
return 0;
}
even if you could help me towards what I need to do...
#5
Re: Odd/Even integers
Posted 02 August 2011 - 02:31 AM
you said that the user will select if the one to be printed is odd or even, so you need to let the user input..
Next.. finish your function..
void printInteger( char )
i'll ask you, in your college algebra, how do you identify if a number is odd or even?
If you will get that you will easily get the code..
#6
Re: Odd/Even integers
Posted 02 August 2011 - 02:35 AM
CreamDelight, on 02 August 2011 - 02:31 AM, said:
you said that the user will select if the one to be printed is odd or even, so you need to let the user input..
Next.. finish your function..
void printInteger( char )
i'll ask you, in your college algebra, how do you identify if a number is odd or even?
If you will get that you will easily get the code..
I never did algebra at college...! I know that it has to be divisible by 2. so 2n even 2n+1 odd
This post has been edited by rdhc1330: 02 August 2011 - 02:37 AM
#7
Re: Odd/Even integers
Posted 02 August 2011 - 02:46 AM
so...
if(n % 2 == 0)
i am an even
else
i am an odd
...You already got the idea
this snippet will print all even integers in a range of 0 - 10..
for(int x = 0; x <= 10; x++)
{
if(x % 2 == 0)
printf("%d\n", x);
}
This post has been edited by CreamDelight: 02 August 2011 - 02:47 AM
#8
Re: Odd/Even integers
Posted 02 August 2011 - 02:48 AM
CreamDelight, on 02 August 2011 - 02:46 AM, said:
so...
if(n % 2 == 0)
i am an even
else
i am an odd
...You already got the idea
this snippet will print all even integers in a range of 0 - 10..
for(int x = 0; x < 10; x++)
{
if(x % 2 == 0)
printf("%d\n", x);
}
yes - but the user should get to decide whether odd or even integers are to be printed? and also a while loop needs to be used, not a for loop.
This post has been edited by rdhc1330: 02 August 2011 - 02:54 AM
#9
Re: Odd/Even integers
Posted 02 August 2011 - 02:54 AM
scanf("%c", choice);
for(int x = 0; x <= 10; x++)
{
if(choice == 'o')
{
if(x % 2 != 0)
printf("%d\n", x);
}
else if(choice == 'e')
{
if(x % 2 == 0)
printf("%d\n", x);
}
}
oH.. i'm giving the whole code..
i think you can see everything here so any problem just refer to the code and your logic..
#10
Re: Odd/Even integers
Posted 02 August 2011 - 02:58 AM
CreamDelight, on 02 August 2011 - 02:54 AM, said:
scanf("%c", choice);
for(int x = 0; x <= 10; x++)
{
if(choice == 'o')
{
if(x % 2 != 0)
printf("%d\n", x);
}
else if(choice == 'e')
{
if(x % 2 == 0)
printf("%d\n", x);
}
}
oH.. i'm giving the whole code..
i think you can see everything here so any problem just refer to the code and your logic..
can you not give me the rest?
I'm confused now
#11
Re: Odd/Even integers
Posted 02 August 2011 - 03:03 AM
maybe do some and code and post here were your stuck..
#12
Re: Odd/Even integers
Posted 02 August 2011 - 03:08 AM
CreamDelight, on 02 August 2011 - 03:03 AM, said:
maybe do some and code and post here were your stuck..
I have this:
//-----------------------------------------------------------------------------
#include <iostream>
#include <conio.h>
using namespace std;
void printInt1( char number );
void printInt2( char );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int myInteger1;
int myInteger2;
cout << "Enter any 2 integers: " << endl << endl;
cin >> myInteger1;
cout << endl << "... and the next one: " << endl << endl;
cin >> myInteger2;
printInt1( myInteger1 );
_getch();
return 0;
}
//-----------------------------------------------------------------------------
void printInt1( char number );
{
scanf("%c", choice);
for(int x = 0; x <= 10; x++)
{
if(choice == 'o')
{
if(x % 2 != 0)
cout << ("%d\n", x);
}
else if(choice == 'e')
{
if(x % 2 == 0)
cout << ("%d\n", x);
}
}
}
#13
Re: Odd/Even integers
Posted 02 August 2011 - 03:18 AM
Like this..
void printInt1( char choice, int int1, int int2 )
char choice is for the decision of the user if he will display it odd or even..
int1 is for the first integer and int2 for the second..
having these three parameters we can now start the loop..
void printInt1( char choice, int int1, int int2 )
{
for(; int1 <= int2; int1++)
{
if(choice == 'o' || choice == 'O')
{
if(int1 % 2 != 0)
cout << int1 << endl;
}
else if(choice == 'e' || choice == 'E')
{
if(int1 % 2 == 0)
cout << int1 << endl;
}
}
}
That's all i can offer to you..
You should be the one who will make your main function...
#14
Re: Odd/Even integers
Posted 02 August 2011 - 03:23 AM
CreamDelight, on 02 August 2011 - 03:18 AM, said:
Like this..
void printInt1( char choice, int int1, int int2 )
char choice is for the decision of the user if he will display it odd or even..
int1 is for the first integer and int2 for the second..
having these three parameters we can now start the loop..
void printInt1( char choice, int int1, int int2 )
{
for(; int1 <= int2; int1++)
{
if(choice == 'o' || choice == 'O')
{
if(int1 % 2 != 0)
cout << int1 << endl;
}
else if(choice == 'e' || choice == 'E')
{
if(int1 % 2 == 0)
cout << int1 << endl;
}
}
}
That's all i can offer to you..
You should be the one who will make your main function...
thankyou
#15
Re: Odd/Even integers
Posted 02 August 2011 - 04:02 AM
//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3
#include <iostream>
#include <conio.h>
using namespace std;
void printInt1( char choice );
void printInt2( char );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int myInteger1;
int myInteger2;
cout << "Enter any 2 integers: " << endl << endl;
cin >> myInteger1;
cout << endl << "... and the next one: " << endl << endl;
cin >> myInteger2;
printInt1;
_getch();
return 0;
}
//-----------------------------------------------------------------------------
void printInt1( char choice, int int1, int int2 )
{
for(; int1 <= int2; int1++)
{
if(choice == 'o' || choice == 'O')
{
if(int1 % 2 != 0)
cout << int1 << endl;
}
else if(choice == 'e' || choice == 'E')
{
if(int1 % 2 == 0)
cout << int1 << endl;
}
}
}
I don't know what to do next.
This post has been edited by rdhc1330: 02 August 2011 - 04:19 AM

New Topic/Question
Reply


MultiQuote


|