Below if my code that is supposed to ask for user for 2 numbers and output the sum of all even numbers between them. I am only having trouble using the count function as I don't believe I am setting it right and google has only helped me so far
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
while(num1 > num2) {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
cin >> num1 >> num2;
cout << endl;
}
if(num1 % 2 == 0)
count(== num1);
else
count(== num1 + 1);
while(count(<= num2)) {
sum = sum + count;
count = count + 2;
}
cout << "The sum of the even intergers between " << num1 << "and " << num2
<< " = " << sum << endl;
return 0;
}
There is no count() function in your code, nor is there a standard count() function in the standard C++ library that does what you want (though std::accumulate() comes close). Nor do you really need such a function anyway, a simple loop will suffice.
Try something more like this:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int num1, num2, sum = 0;
do {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
if (cin >> num1 >> num2) {
if (num1 < num2) break;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ā\nā);
}
cout << endl;
}
while (true);
for (int num = num1; num <= num2; ++num) {
if ((num % 2) == 0)
sum += num;
}
cout << "The sum of the even integers between " << num1 << " and " << num2 << " = " << sum << endl;
return 0;
}
If you want to omit num1 and num2 from the sum, simply change the loop accordingly:
for (int num = num1 + 1; num < num2; ++num) {
Related
I need to find out how to control the flow of the program so that the program does not crash when the user inserts huge numbers. The goal of the program is to calculate the Fibonacci series, by the user's input.
#include <iostream>
using namespace std;
int main() {
int n;
cout << " Please enter number of fibonacci number series you wanna see ";
cin >> n;
int num1=0;
int num2=1;
cout << num1 << " " << num2 << " ";
if(num1+num2<=32756)
for (int i=0; i<=n-1; ++i)
{
int num2Temp=num2;
cout << num1+num2 << " ";
num2=num1+num2;
num1=num2Temp;
}
else
cout << " TOO BIG " << endl;
return 0;
}
I do calculate the Fibonacci series, however, it does not follow the if statement and stops the program as it exceeds the 32756. I understand why it does it as the num1+num2 is not updated, so that program understand it 0+1, no the update versions for each loop. But I do not have a specific idea to fix the program.
You only test the sum once, before your loop starts.
You probably want to test inside the loop, and exit it if the sum exceeds 32756:
for (int i = 0; i < n; i++)
{
if (num1 + num2 <= 32756)
{
int num2Temp = num2;
num2 += num1;
num1 = num2Temp;
cout << num2 << " ";
}
else
{
cout << " TOO BIG " << endl;
break;
}
}
I am a beginner in c++. I wrote a program to separate the digits in an integer entered and display them and their sum. However, when the loop repeats, the program hangs even though it compiled perfectly. I tried a '... while' loop and a while loop but the problem persists. What should I do to have it repeat (ask user for next integer, calculate and display the results) without problems? Any help will be appreciated.
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num != 0 )
{
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
while (true )
{
//reset initial values every loop
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
//same exit condition
if (num == 0)
break;
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
//extraneous
/*while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;*/
}
return 0;
}
I think you should use vector to store the digits... also the logic inside should be a bit smaller (see note 1), (note that I didn't test for negatives, you may use abs from cmath)
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
std::vector<int> digits;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num) {
while (num) {
int temp = num % 10;
digits.push_back(temp);
sum += temp;
num /= 10;
}
std::reverse(digits.begin(), digits.end());
cout << sum << endl;
for(auto & a : digits)
{
cout << a << " ";
}
cout << endl;
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
note 1:
while (num) {
int temp = num % 10;
sum += temp;
num /= 10;
}
I am writing a program to simulate a dice roll with random function.
This is my code, but I am having infinite loop.
The program is supposed to ask the user how many times they would like to roll the dice.
Then I used a for loop to roll the dice from 1 to 6
and I put all that in a do-while in order to only allow the user to select between 1 and 6 , if the selection is outside of 1 to 6 it is supposed to say it is an invalid selection.
I am not allowed to use functions or arrays on this
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//i is the counter for the loop
int i = 0;
//store total count for landing number
int num1 =0;
int num2 =0;
int num3 =0;
int num4 =0;
int num5 =0;
int num6 =0;
int Num1 =0;
int Num2 =0;
int Num3 =0;
int Num4 =0;
int Num5 =0;
int Num6 =0;
int randNum;
int times;
//User selection
int selection;
/* initialize random seed: */
srand ((unsigned int)time(NULL));
/* generate random number: */
randNum = rand() % 6 + 1;
cout<<"How many times would you like to roll the dice? " << endl;
cin >> selection;
do{
for(i = 1; i <= selection; i++)
{
if(randNum==1)
{
num1++;
}
else if(randNum==2)
{
num2++;
}
else if(randNum==3)
{
num3++;
}
else if(randNum==4)
{
num4++;
}
else if(randNum==5)
{
num5++;
}
else if(randNum==6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled \t # Times \t % Times" << endl;
cout <<"----- \t ------- \t -------" << endl;
cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
}
while(i >= 1 || i <= 6);
{
cout << "This is an invalid number. \n"
<< "The number of rolls should be equal to or greater than 1.\n"
<< "Please enter again.\n";
}
}
Your conditional statement: i = 1; i <= selection; i++ should go in the final while you have.
Try:
do {
if (randNum==1) {
num1++;
} else if(randNum==2) {
num2++;
} else if(randNum==3) {
num3++;
} else if(randNum==4) {
num4++;
} else if(randNum==5) {
num5++;
} else if(randNum==6) {
num6++;
}
i += 1;
} while (i <= selection);
The infinite loop is the do - while...
I'm not sure what you were trying to do there, but you will always get a number greater than 1 or smaller than 6 in your test - you can't fail both...
I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
Here's my code :
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
const int NumsToCal = 5;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Edit
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
The program output should look like this:
Enter an even number: 23
The number is not a positive even number.
Enter an even number: -6
The number is not a positive even number.
Enter an even number: 4
20 20.25 20.50 20.75 21
The sum is 102.5
program doesn't run properly. the odd/ even numbers are identified, but the loop to increment the variables (20 + 1 / (even number entered)) does not work right.
#include <iostream>
int main(int argc, char *argv[])
{
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num % 2 != 0)
cout << "The number " << num << " is not a positive even number." << endl;
else
cout << num << " is even!" << endl << endl;
incr = 1 / num;
for (val = 20.0F; val <= 21.0; val += incr)
{
cout << val;
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}
If you divide one integer 1 between another num the result is an integer that as chris said is 0.
You should do:
incr = 1.0F / (float)num;
And for exiting for wrong introduced values you should return from main
#include <iostream>
int main() {
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num < 0 || num % 2 != 0){
cout << "The number " << num << " is not a positive even number." << endl;
return -1;
}
else {
cout << num << " is even!" << endl << endl;
}
incr = 1.0 / num;
for (val = 20.0F; val <= 21.0; val += incr) {
cout << val << " ";
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}