I have completed code that works for the majority of cases for outputting odd numbers between two integers in C++. However it doesn't work for negative numbers and/or if the two values are less than 3 in difference from one another (e.g. it works if the two numbers are 2 & 5, but does not work if the two numbers are 2 & 4).
I know that it is a cause of my code which adds 2 every time the while loop iterates, I'm just not sure how to rectify it.
while (secondOddNum - firstOddNum > 2)
{
if (firstOddNum % 2 > 0) //positive numbers
{
firstOddNum += 2;
sumOdd += pow(firstOddNum,2);
cout << firstOddNum << endl;
} else // even numbers
{
firstOddNum += 1;
sumOdd += pow(firstOddNum,2);
cout << firstOddNum << endl;
}
Thanks
I think your logic is overcomplicating the problem a little bit. Did you mean to do something like this?
void OutputOdds(int min, int max)
{
for(int i = min; i <= max; i++)
{
if(i % 2 == 1 || i % 2 == -1)
cout << i << " ";
}
}
Tests:
OutputOdds(-25, 6);
cout << endl << endl;
OutputOdds(1, 3);
cout << endl << endl;
OutputOdds(2, 4);
prints
-25 -23 -21 -19 -17 -15 -13 -11 -9 -7 -5 -3 -1 1 3 5
1 3
3
You can try something like this:
void printOddsBetween(int min, int max) {
int t = min + (min % 2 == 0);
while (t <= max) {
cout << t << endl;
t += 2;
}
}
It starts at the closest odd value to min. Then just prints every odd value up to max.
int min = 1;
int max = 11;
int counter = 0;
for (int i = min; i <= max; i++) {
if (i % 2 != 0) {
System.out.println(i);
counter += 1;
}
}
System.out.println("counter" + counter);
int xx[] = new int[counter];
int ii = 0;
for (int i = min; i <= max; i++) {
if (i % 2 != 0) {
xx[ii] = i;
ii += 1;
}
}
int firstNum{}, secondNum{};
cout << "Enter two numbers, the first smaller than the second."
<< endl;
cout << "Enter first integer: \t";
cin >> firstNum;
cout << endl;
while (firstNum < secondNum);
{
cout << "Enter second integer: \t";
cin >> secondNum;
cout << endl;
cout << "\nOdd numbers from " << firstNum << " to " << secondNum << " are \n";
for (int i = firstNum; i <= secondNum; i++)
{
if (i % 2 != 0)
{
cout << i << " ";
}
}
}
Prints: 2 & 19 ARE MY USER INPUTS
Enter two numbers, the first smaller than the second.
Enter first integer: 2
Enter second integer: 19
Odd numbers from 2 to 19 are
3 5 7 9 11 13 15 17 19
Related
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
// Declarations
int firstNum = 0, secondNum = 0;
cout << "This program will ask you for two numbers, and then output the even numbers between those two numbers" << endl;
cout << "Please enter the lowest number: " << endl;
cin >> firstNum;
cout << "Please enter the highest number: " << endl;
cin >> secondNum;
if (firstNum % 2 != 0 && secondNum % 2 != 0)
{
while (firstNum <= secondNum)
{
if (firstNum % 2 != 0)
{
cout << firstNum << " ";
}
firstNum++;
}
}
else if (firstNum % 2 == 0 && secondNum % 2 == 0)
{
while (firstNum <= secondNum)
{
if (firstNum % 2 == 0)
{
cout << firstNum << " ";
}
firstNum++;
}
}
return 0;
}
I'm doing a coding exercise for c++, if the user inputs two odd numbers the program would then output all of the odd numbers between the first and second number (first number has to be smaller than the second). If both numbers are even then the program will out put the sum of the even numbers between the first and second (again the first has to be the smallest). My problem is that I do not know how to add the numbers together, I only know how to print either the even or odd numbers between them.
An example:
first number = 4
second number = 36
output = 340
how do you add the even numbers between them?
You could have a variable that stores the result. For example
else if (firstNum % 2 == 0 && secondNum % 2 == 0)
{
int sum = 0; // store the sum of all even numbers
while (firstNum <= secondNum)
{
sum += firstNum; // adding the number to the sum
firstNum += 2; // increment by 2 would skip all the odd numbers
}
cout << sum << endl;
}
Hopefully this helps!
My problem is that I do not know how to add the numbers together - If that is the exact question and assuming start and end are the start and end numbers you can do this to get the sum of all numbers between start and end
int sum = 0;
for ( int i = start; i <= end; i++ )
{
sum += i;
}
std::cout << "Sum of numbers between " << start << " and end " << end << " is " << sum ;
to get the sum of all even numbers in that range
int sum = 0;
for ( int i = start; i <= end; i++ )
{
if ( i%2 == 0 ) // even
sum += i;
}
std::cout << "Sum of even numbers between " << start << " and end " << end << " is " << sum ;
I see you wanted it using while loop in which case
int sum = 0;
while ( start <= end )
{
sum += start;
++start;
}
I'm attempting to list a set of prime numbers from a lower bound to an upper bound limiting the number of prime numbers in a row to 8. Though I have done the first part, I can't get them to list in rows with only 8 prime numbers per row.
#include <iostream>
enter code here
int main()
{
int low, high, i, flag, j;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
while (low < high)
{
flag = 0;
for (i = 2, j = 1; i <=low/2; +ii, ++j)
{
if (j == 8)
{
cout << "\n";
j = j - 7;
}
else if (low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << low << " ";
++low;
}
return 0;
}
It works for the first row, then everything else seems to start listing rather than being in a row.
Output: Enter two numbers(intervals): 1
200
Prime numbers between 1 and 200 are: 1 2 3 5 7 11 13 17
19
23
29
31 ...
It's a little late, but I said I'd do it. My suggestions:
#include <iostream>
//enter code here
using std::cin;
using std::cout;
int main()
{
int low, high, count, i;
bool flag; // bools are more suited to being flags
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
count = 1; // I replaced j with this for ease of reading
while (low < high)
{
flag = true;
// using break in loops is not recommended, and you already have a flag
for (i = 2; i <= low / 2 && flag; ++i)
{
if (low % i == 0)
{
flag = false;
}
}
if (flag)
{
cout << low;
if (count == 8)
{
cout << std::endl;
count = 1;
}
else
{
cout << " ";
++count;
}
}
++low;
}
return 0;
}
Your code divide not every 8 prime numbers, but every 8 attempts of dividing a number during search for prime number. So any prime that is 8 or more values away from previous will generate a line break. Consider following fix:
#include <iostream>
using namespace std;
int main()
{
int low, high, i, flag, j;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
j = 0;
while (low < high)
{
flag = 0;
for (i = 2; i <=low/2; ++i)
{
// Removed here
if (low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
++j; // Added here
cout << low << " ";
}
if (j == 8) // and here
{
cout << "\n";
j = j - 8;
}
++low;
}
return 0;
}
By the way, you should end a search when reaching square root of low, not low / 2. The loop will be much faster.
I'm trying to calculate simple MA by reading values from a file.
The values are stored like this:
11
12
13
14
15
16
17
I've did this so far:
for (int i = 0; (ifs); i++) {
ifs >> price;
//cout << "price:" << price;
prices_vec.push_back(price);
sum += prices_vec[i];
cnt++;
if (cnt >= 5) {
output_file << sum / 5 << endl;
cout << "Your SMA: " << (sum / 5) << endl;
sum -= prices_vec[cnt - 5];
}
}
This works, but at the end, it adds two additional numbers in the end. The output in file is:
13
14
15
15.8
0
Any idea why this might be happening? And, is there a more efficient way to calculate SMA?
I believe this will solve your problem:
int main()
{
int cnt = 0, sum = 0;
vector<float> prices_vec;
std::ifstream ifs ("Nos.txt", std::ifstream::in);
float price;
for (int i = 0; (ifs) >> price; i++) {
prices_vec.push_back(price);
sum += prices_vec[i];
cnt++;
if (cnt >= 5) {
cout << sum / 5 << endl;
cout << "Your SMA: " << (sum / 5) << endl;
sum -= prices_vec[cnt - 5];
}
}
return 0;
}
I am having troubles with some of the inputs for my addition/multiplication table, and am hoping to find some help towards fixing this. I will begin by posting what I have for the program.
The code is as follows :
#include <iostream>
using namespace std;
void die() {
cout << "BAD INPUT!" << endl;
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
cout << "Choose:\n";
cout << "1. Addition Table\n";
cout << "2. Times Table\n";
cin >> choice;
if (!cin) die();
if (choice != ADD and choice != MULTIPLY) die();
cout << "Please enter the smallest number on the table:\n";
cin >> min;
if (!cin) die();
cout << "Please enter the largest number on the table:\n";
cin >> max;
if (!cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
}
if (choice == MULTIPLY) {
for (int i = min; i <= max; i++) {
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
cout << '\n';
}
}
}
Now, here are the mistakes that I am getting from this code that I cannot seem to resolve. First, when doing the MUlTIPLY table with min = 1, max = 1, I am getting:
X 1
when I should be getting (I believe)
X 1
1 1
Secondly, while doing the MULTIPLY table with min = 1, max = 12, I am getting:
X 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
when I should be getting
X 1 2 3 4 ... 12
1 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
And finally, when using the ADD table with min = 21, max = 40, I cannot post all of the code since it is such a mess, but basically the columns/rows are as follows:
+ 21 22 23 24 25 ...
5
1
6
2
7
3
8
When obviously, the code should output the rows and columns to be 21 - 40 evenly. As you can see in the last example, my rows are outputting properly, but somehow my columns are a complete, garbled mess.
I have been sitting and staring at this code for awhile, and can't seem to fix these issues at hand. Can anyone help lead me in the right direction? I really appreciate any help and hints :)
Check this out. Might not be fully optimized, but works
if (choice == ADD) {
cout << '+';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
}
}
if (choice == MULTIPLY) {
cout << 'X';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
}
}
See output here.
#include <iostream>
#include <iomanip>
#include <cstdio>
void die()
{
std::cout << "BAD INPUT!" << "\n";
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
std::cout << "Choose:\n";
std::cout << "1. Addition Table\n";
std::cout << "2. Times Table\n";
std::cin >> choice;
if (!std::cin) die();
if (choice != ADD and choice != MULTIPLY) die();
std::cout << "Please enter the smallest number on the table:\n";
std::cin >> min;
if (!std::cin) die();
std::cout << "Please enter the largest number on the table:\n";
std::cin >> max;
if (!std::cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
printf(" +");
else
printf("%3d", i);
printf(" ");
for (int j = min; j <= max; j++) {
printf("%3d ", i + j);
}
printf("\n");
}
}
if (choice == MULTIPLY) {
/* for printing header of the multiplication table */
std::cout << "X\t";
for (int j = min; j <= max; j++) {
std::cout << min * j << "\t";
}
std::cout << "\n";
/* for printing rest of the table */
for (int i = min; i <= max; i++) {
std::cout << i << "\t";
for (int j = min; j <= max; j++) {
std::cout << i * j << '\t';
}
std::cout << '\n';
}
}
}
The crucial mistake in your code for multiplication was that, you were trying to print (max - min + 1) + 1 rows in total, the extra +1 for the header. While your code was printing the first row as header and then starting directly with the second row.
Your code for addition table was correct, but 21 to 40 with tab character in between was too taking too much space for a typical laptop screen, not to say the output won't be pretty.
On my system, the output of tput lines and tput cols was 38 and 144 resp.
which wasn't sufficient for your code.
you can format the output with printf using printf fixed width output.
Considering you are not much familiar with C++, I would like to state that
using the std namespace as default namespace will work for this program, but when you working with larger projects, you should always prefix it.
I haven't enough reputation to add comment
if I were, I was commenting these lines
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';
int main()
{
int n, i, sum;
cout << "Enter a value for n: ";
cin >> n;
sum = 0;
i = 1;
do
{
sum += i * i;
i++;
}while (i <= n);
cout << "The sum of the first " << n
<< " numbers is " << sum << endl;
return 0;
}
whenever I run this code and use 4 as an example the output comes up as 30. When the factorial of 4 is 24
sum+=i*i
--> sum for first n squares.
factorial is
prod=prod*i