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;
};
Related
I want to write a program which displays palindromes numbers between 1 and 10000, I wrote a script which displays if the number typed by the user is a palindrome or no but when I add the for loop it gives me false results
My code:
#include<iostream>
using namespace std;
int main()
{
int num, reverse = 0, remainder, temp;
for(num=0;num<1000;num++){
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
You need to make sure that reverse is always 0 at the start of loop iteration:
for(num=0;num<1000;num++){
reverse = 0;
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
Managing variables becomes much easier if you declare them where you need them instead of lumping everything together:
int main()
{
for(int num=0;num<1000;num++){
int reverse = 0;
int temp = num;
int remainder = 0;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
Now you are sure remainder is always initialized at the start of for loop iteration and it cannot live longer than one iteration (for example, it won't live to the next iteration).
I checked your program and noticed a few flaws:
1) All 1-digit numbers are Palindrome because their is reverse is same.
---Your program didn't display them as palindromes.
2) You don't have to display all the details like their remainder and reverse.
---Obviously the Palindromes reverse will be the same as the original. We don't need to display non-palindromes. In some cases reverse was displaying garbage values as well. I don't find a reason that will be of any help to the user.
Solution:-
#include<iostream>
using namespace std;
bool findPalindrome(const int);
int main()
{
for (int i = 0; i < 10000; i++) {
if (findPalindrome(i)) {
cout << "Number " << i << " is a Palindrome!" << endl;
}
}
return EXIT_SUCCESS; // Return EXIT_SUCCESS is my Specialty :D
}
bool findPalindrome(const int num) {
int temp = num;
int reve = 0;
while (temp != 0) {
reve = (reve * 10) + (temp % 10);
temp /= 10;
}
return (reve == num);
}
In addition to the excellent codes other people have posted (I would go with those), this is another (and more readable) way in which you can do the same thing..
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
bool isPalindrome(unsigned number) {
std::vector<unsigned> number_as_vec;
while (number) {
number_as_vec.push_back(number % 10);
number /= 10;
}
for (size_t i = 0, j = number_as_vec.size() - 1; i < j; i++, j--) {
if (number_as_vec.at(i) != number_as_vec.at(j)) {
return false;
}
}
return true;
}
int main(int argc) {
unsigned num = 1;
while (num != 10001) {
if (isPalindrome(num)) {
std::cout << num << "\n";
}
num++;
}
return 0;
}
Also, instead of using vectors, one can simply use a string here:
bool isPalindrome(unsigned number) {
std::string num_as_string(std::to_string(number));
for (size_t i = 0, j = number_as_string.size() - 1; i < j; i++, j--) {
if (number_as_string.at(i) != number_as_string.at(j)) {
return false;
}
}
return true;
}
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 was trying to figure out this task, but so far have been unsuccessful. I think I understand the logic behind it, I just don’t know how to nest loops so it works (if that makes sense). I would very much appreciate your help!
Task:
"Create an application in which a user enters full numbers until they enter number 0 (zero). The application should print out how many even numbers have been entered, how many odd numbers, sum of even numbers and sum of odd numbers, and total sum of numbers."
my code so far:
#include <iostream>
using namespace std;
void main() {
do {
int input1;
cout << "Type in a number";
cin >> input1;
} while (input1 != 0);
cout << "Type in a number";
cin >> input1;
if (input1 % 2 == 0)
{
int even = 0;
while (input1 % 2 == 0 )
cout << even;
even++;
}
else
{
int odd = 0;
while (odd != 0)
{
cout << odd;
odd++;
}
}
}
system("pause");
}
Note: I did not try to do the third part of the task, since the second one won't work :/ I figured out first part, and I did it with do while loop. Thanks again.
Try this:
int oddCount = 0;
int evenCount = 0;
int oddSum = 0;
int evenSum = 0;
int in;
do
{
std::cout << "Type in a number:";
std::cin >> in;
if (0 == in)
break;
if ( in % 2 == 0 )
{
evenCount++;
evenSum += in;
}
else
{
oddCount++;
oddSum += in;
}
} while ( true );
std::cout << "Odd count: " << oddCount << std::endl;
std::cout << "Even count: " << evenCount << std::endl;
std::cout << "Odd sum: " << oddSum << std::endl;
std::cout << "Even sum: " << evenSum << std::endl;
std::cout << "Total sum: " << oddSum + evenSum << std::endl;
Take a look at this piece of code:
#include <iostream>
using namespace std;
int main() {
int input;
int total_sum = 0, odd_sum = 0, even_sum = 0;
int odd_count = 0, even_count = 0;
do {
cout << "Type in a number: ";
cin >> input;
total_sum += input;
if (input % 2 == 0)
{
even_count++;
even_sum += input;
}
else
{
odd_count++;
odd_sum += input;
}
} while (input != 0);
cout << "Total Sum: " << total_sum << endl;
cout << "Even sum: " << even_sum << endl;
cout << "Odd sum: " << odd_sum << endl;
cout << "Even Count: " << even_count << endl;
cout << "Odd Count: " << odd_count << endl;
return 0;
}
See how input is declared outside of the loop. if it was inside it, then you essentially create it each time you enter the loop. that would be fine if you did not want to use its values outside of the loop (like in the loops condition).
Also, notice that the values you need to calculate can be updated within that same loop.
I am creating a program to make a frequency table range from <=0 to >=10 of integer numbers from a text file. However when I run the program, if it contains number that <= 0 or >= 10 all other numbers are added up but the negative numbers is not counted. I think my problem lies in my If-statement but I dont know how to correct it. This is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream abc("beef.txt");
int num;
int tem;
int N;
int noNum = 0;
cout << "Class" << " | " << "Frequency" << endl;
cout << "_________|___________" << endl;
while (abc >> tem) {
noNum++;
}
for (num = 0; num < 11; num++) {
N = 0;
while (abc >> tem) {
if (num == tem) {
N = N + 1;
}
if (tem < 0) {
N = N + 1;
}
if (tem > 10) {
N = N + 1;
}
}
abc.clear(); //clear the buffer
abc.seekg(0, ios::beg); //reset the reading position to beginning
if (num == 0) {
cout << "<=0" << " | " << N << endl;
}
else if (num == 10) {
cout << ">=10" << " | " << N << endl;
}
else {
cout << " " << num << " | " << N << endl;
}
}
cout << "The number of number is: " << noNum << endl;
}
For example if there is -5 in the text file the program would run like this
The problem is twofold. First, you forgot two clear and reset the buffer after counting the number of elements. Second, you always count the numbers lower than zero and greater than 10. You should only do this when num is 0 or 10 respectively.
The correct code should look like this:
ifstream abc("beef.txt");
int num;
int tem;
int N;
int noNum = 0;
cout << "Class" << " | " << "Frequency" << endl;
cout << "_________|___________" << endl;
while (abc >> tem) {
noNum++;
}
for (num = 0; num < 11; num++) {
abc.clear(); //clear the buffer
abc.seekg(0, ios::beg); //reset the reading position to beginning
N = 0;
while (abc >> tem) {
if (num == tem) {
N = N + 1;
}
if (tem < 0 && num == 0) {
N = N + 1;
}
if (tem > 10 && num == 10) {
N = N + 1;
}
}
if (num == 0) {
cout << "<=0" << " | " << N << endl;
}
else if (num == 10) {
cout << ">=10" << " | " << N << endl;
}
else {
cout << " " << num << " | " << N << endl;
}
}
cout << "The number of number is: " << noNum << endl;
This program outputs terms in a Jugglers Series based on user input of first term, number of terms to calculate and terms per line
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//prototype
int ValidateInput (string Prompt);
int main()
{
//local variables
long long int firstTerm;
int termsToCalc;
int termsPerLine;
int count;
//tells user what program does
cout << "Program will determine the terms in a Juggler Series" << endl << endl;
//calls user function to read in the frist term
firstTerm = ValidateInput ("Enter the first term: ");
cout << endl;
//calls user function to read in the number of terms to calculate (after the first)
termsToCalc = ValidateInput ("Enter the number of terms to calculate (after the first): ");
cout << endl;
//calls user function to read in the number of terms to display per line
termsPerLine = ValidateInput ("Enter the terms to display per line: ");
cout << endl;
cout << "First " << termsToCalc << " terms of Juggler series starting with " << firstTerm << endl << endl;
count = 1;
do
{
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
//the term is even take it to the power of 1/2 and increase the count 1
if (firstTerm % 2 == 0 )
{
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm << endl;
count++;
}
//the term is odd take it to the power of 3/2, and increase the count 1
else
{
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm << endl;
count++;
}
}
//continue looping until the terms to calculate is no longer less than or
// equal to the count
while (count <= termsToCalc);
return 0;
}
int ValidateInput (string Prompt)
{
//local variable
int number;
//prompts user for first term, and reads in number
cout << Prompt;
cin >> number;
//user input must be positive, a while loop will check user input and
//continue to check until the term is positive.
while (number <=0)
{
cout << "Error - Enter a positive number" << endl;
cin >> number;
}
//returns number to main function
return number;
}
This is the current printout
This is how I would like it to look
I can not figure out how to edit the output statement to make this display correctly
After removing the endls and moving the new line statement to the end I now get the correct print out but with an extra term
First of all, remove the endls from these:
cout << setw(16) << firstTerm << endl; // -> cout << setw(16) << firstTerm;
And perhaps move this part:
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
to the end of the loop. So it becomes:
count = 0;
do {
if (firstTerm % 2 == 0 ) {
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm;
} else {
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm;
}
if ( (count + 1) % termsPerLine == 0) {
cout << "\n";
}
count++;
} while (count < termsToCalc);