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;
}
Related
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) {
C++ code where a user enters 2 integers, then the program outputs how many numbers were multiples of 3 between those integers, including both numbers, and how many numbers were divisible by 5.
Here is my code. I think I am not calling the if statements correctly. Maybe I need a switch?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
sentinel = (abs(numb2-numb1)+1);
if(numb1 % 3 == 0 && counter <= sentinel) {
mult3++;
numb1++;
counter++;
}
else {
numb1++;
counter++;
}
cout << endl;
counter = 0;
if(numb1 % 5 == 0 && counter <= sentinel) {
mult5++;
numb1++;
}
else {
numb1++;
counter++;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
A short version for your code is given below. Checkout the while loop(not ran, just from top of my head).
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
while(numb1 <= numb2){
mult3 += (numb1%3)==0?1:0; // (numb1%3)?0:1
mult5 += (numb1%5)==0?1:0;
++counter;
++numb1;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
You're not looping it at all, so this will only execute once.
while(counter < sentinel)
{
//run your tests and increment the appropriate variables if the numbers are divisible by 3 or 5
counter++;
}
start by evaluating the lowest of the 2 entered numbers + counter % 3 == 0
I got it. I had to use 3 separate while loops. One for <, one for >, and one for =. When I just had a <= and >=, the first loop would feed into the second. I took out the sentinel and the counter thanks to Avezan's help. Thanks everyone.
The program asks the users for numbers until the total of the numbers is greater than 30. Also, the user has to count how many numbers are even and how many are odd.
I can get the first part of the problem but i am having trouble with the counting part.
i.e.
Total is 0
Please enter an integer: 20
Total is 20
You had 1 even numbers and 0 odd numbers.
#include <iostream>
#include <string>
using namespace std;
int main (){
int integer;
int total = 20;
int even_count = 0;
int odd_count = 0;
cout << "Total is 0" << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
while ( total <= 30){
cout << "Total is " << total << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
total = integer + total;
}
if (integer % 2 == 0) {
even_count = even_count + 1;
}
if (integer % 2 != 0){
odd_count = odd_count + 1;
}
cout << "You had " << even_count << " even numbers and ";
cout << odd_count << " odd numbers.";
cout << endl;
return 0;
}
This code should work:
#include <iostream>
#include <string>
using namespace std;
int main (){
int integer=0;
int total = 0;
int even_count = 0;
int odd_count = 0;
cout << "Total is " << total << endl;
while ( total <=30 && integer <=30){
cout << "Please enter an integer: ";
cin >> integer;
total = integer + total;
if(total <=30){
cout << "Total is " << total << endl;
}
if (integer % 2 == 0) {
even_count = even_count + 1;
}
if (integer % 2 == 1){
odd_count = odd_count + 1;
}
}
cout << "You had " << even_count << " even numbers and " << odd_count <<
" odd numbers." << endl;
}
Here is a sample output:
Total is 0
Please enter an integer: 25
Total is 25
Please enter an integer: 5
Total is 30
Please enter an integer: 1
You had 0 even numbers and 3 odd numbers.
I was wanting to get some tips on how to get my program to output a desired number of columns by the user. For example, if the user enters 2 for termsPerLine, then the program should print the values generated by the Juggler series in two columns, or if the user enters 3 for termsPerLine, 3 columns and so forth. The output variable is firstTerm. Any assistance would be great.
#include <string>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int ValidateInput(string Prompt);
int main()
{
int count;
double Odd;
double Even;
long long int firstTerm;
int noOfTerms;
int termsPerLine;
cout << "Program will determine the terms in a Juggler Series" << endl << endl;
firstTerm = ValidateInput("Enter the first term: ");
noOfTerms = ValidateInput("Enter the number of terms to calculate (after first): ");
termsPerLine = ValidateInput("Enter the terms to display per line: ");
cout << "First " << noOfTerms << " terms of JUGGLER SERIES starting with " << firstTerm << endl;
count = 1;
do
{
if (firstTerm % 2 == 0 )
{
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm << endl;
count++;
}
if (firstTerm % 2 != 0 )
{
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm << endl;
count++;
}
}
while (count <= noOfTerms);
cout << endl;
system("Pause");
return 0;
}
int ValidateInput( string Prompt)
{
int num;
cout << Prompt << endl;
cin >> num;
while ( num <= 0 )
{
cout << "Enter a positive number" << endl;
cin >> num;
}
return num;
}
Try this at the top of the loop:
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
or this at the bottom of the loop:
if ((count % termsPerLine) == termsPerLine)
{
cout << "\n";
}
just Fix your loop as:
for (count = 1; count <= numOfTerm; count++)
{
if(firstTerm % 2 == 0)
firstTerm = pow(firstTerm, 0.5);
else
firstTerm = pow(firstTerm, 1.5);
if(count % termPerLine != 0)
cout << setw(15) << firstTerm;
else
cout << setw(15) << firstTerm << endl;
};
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;
}