| | |
odd even numbers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 17
Reputation:
Solved Threads: 0
Hi there!
I am just starting out in c++ and so far, its going ok. I have a small problem in a simple program that is frustrating me, so I am hoping someone will point me in the right direction of where I've gone wrong.
The program basically prompts the user to input two integer values, then outputs all the odd numbers between these two integer values (and their squares), and also outputs all the even numbers between the two inputted values, along with the sum of these even numbers.
I have the program working sufficiently, so long as the first value entered by the user is smaller than the second value entered. Thats the small problem I've encountered; it should not make a difference between the size of the values entered. I think i need to change the condition in the for loop, but I've tried various conditions without success
Here is the program:
I am just starting out in c++ and so far, its going ok. I have a small problem in a simple program that is frustrating me, so I am hoping someone will point me in the right direction of where I've gone wrong.
The program basically prompts the user to input two integer values, then outputs all the odd numbers between these two integer values (and their squares), and also outputs all the even numbers between the two inputted values, along with the sum of these even numbers.
I have the program working sufficiently, so long as the first value entered by the user is smaller than the second value entered. Thats the small problem I've encountered; it should not make a difference between the size of the values entered. I think i need to change the condition in the for loop, but I've tried various conditions without success
Here is the program:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int a,b,a1,b2,square; //declaring variables
int sum = 0;
cout << "Enter two integers: " << endl; //user inputs two integers
cin >> a >> b;
cout << "The numbers entered are: " << endl; //displays the integers listed
cout << "1. " << a << endl;
cout << "2. " << b << endl;
a1 = a;
b2 = b;
//for odd numbers
cout << "The odd numbers between " << a << " and " << b <<
", along with their squares are:" << endl;
for (a++;a < b;a++)
{
if ((a%2) !=0)
{
cout << setw(4) << a;
square = a * a; //formula for finding the square of an integer
cout << " = " << square;
} //end if
} //end for
cout << endl << endl;
//for even numbers
cout << "The even numbers between " << a1 << " and " << b2 <<
" are:" << endl;
for (a1++;a1 < b2;a1++)
{
if((a1%2) == 0)
{
cout << setw(4) << a1;
sum += a1; //formula for finding the sum of the even integers
} //end if
} //end for
cout << endl;
cout << "The total sum of all the even numbers is: " << sum;
cout << endl << endl;
getch();
return 0;
}//end main•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 1
In both your 'for' loops your condition needs 'a' to be smaller than 'b', so before your 'for' loop but after your call for user input 'cin', you need a condition which checks the values entered, and then ensures that a is smaller than b
for example
your code should now work fine even if a is greater than b
for example
//check to ensure that a is smaller than b
if(a > b)
{
int temp = a;
a = b;
b = temp;
}![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- making counter for odd numbers (C++)
- C++ How to allow only odd numbers to be inputted (C++)
- Odd numbers in C++? (C++)
- Adding the sum of odd numbers. Please help (Visual Basic 4 / 5 / 6)
- print odd and even numbers (Assembly)
Other Threads in the C++ Forum
- Previous Thread: C and C++
- Next Thread: I have a problem in my assignment for binary trees
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






