944,106 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2683
  • C++ RSS
Oct 21st, 2007
0

Odd numbers in C++?

Expand Post »
Ja-ja I'm new to this c++, BUT egger to learn. I want to write a program that will cout all the odd numbers between 6 and a positive integer.It must also be bigger than 53. This is what I came up so far but it doesn't give any output :

cout<<"Please enter a number not less than 53\n";
int a, i; // Declare the number input
cin>>i;
while (i <= 53)
{ cout << "No, bigger than 53! Try again : ";
cin >> i;
for (a>6;a<=i;a++); //Execution of the series
if (a%2!=0); // calculating the odd numbers
cout << a <<endl;
cout <<"This is the odd numbers between 6 and "<<i<<"\n";
cout<<endl;

Can anybody help? I'm sure there must be an easier way of doing this.
<email snipped>
Last edited by Ancient Dragon; Oct 21st, 2007 at 5:49 pm. Reason: email snipped
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jobe007 is offline Offline
2 posts
since Oct 2007
Oct 21st, 2007
0

Re: Odd numbers in C++?

c++ Syntax (Toggle Plain Text)
  1.  
  2. for ( int c = 7; c < i; c += 2)
  3. cout << c << "\n";
Reputation Points: 343
Solved Threads: 24
Veteran Poster
Sturm is offline Offline
1,067 posts
since Jan 2007
Oct 21st, 2007
1

Re: Odd numbers in C++?

> for (a>6;a<=i;a++); //Execution of the series
> if (a%2!=0); // calculating the odd numbers
The trailing ; on both these lines is a huge problem.

Next time, use [code][/code] tags around your code.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 21st, 2007
0

Re: Odd numbers in C++?

Here is the improoved version
Quote ...
#include<iostream.h>
void main(){
int n;
do
{
cout<<"\Enter a Value bigger than 53: ";
cin>>n;
}while(n>53);

for(int i=7,j=0;i<n;i+=2,j++);
cout<<"\nThere are ";
cout<<j;
cout<<" odd number between 6 and";
cout<<n;
}
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Oct 22nd, 2007
0

Re: Odd numbers in C++?

void main is not an improvement, and nor is posting code without using [code][/code] tags.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 22nd, 2007
0

Re: Odd numbers in C++?

Nice for loop

I believe the proper format is
for(int a = x ; a < y; a++)
Reputation Points: 10
Solved Threads: 0
Light Poster
pacman326@gmail is offline Offline
43 posts
since Sep 2007
Oct 23rd, 2007
0

Re: Odd numbers in C++?

Salem's request is to repost your code with formatting and Code Tags... When we can read the code, we can help.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Connect a SQL database to a C++ application
Next Thread in C++ Forum Timeline: I'm looking for a good C++ programer (need help with COMports)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC