Dividing a number in loop until it satisfies a condition = 0 - c++

I have to input a value in the program and keep dividing it by 4 until it reaches the number 0. But when I run it, it doesn't stop at 0, it keeps repeating 0 forever. What is wrong with the code?
#include <iostream>
using namespace std;
int main(){
double input;
cout << "Enter an Integer: ";
cin >> input;
cout << input << "/ 4 ";
do
{
input = input / 4;
if (input >= 0)
cout <<" = "<< input << endl;
cout <<input << " /4";
}
while ((input >= 0) || (input != 0));
return 0;
}

Here are my three cents.:)
#include <iostream>
int main()
{
const long long int DIVISOR = 4;
while ( true )
{
std::cout << "Enter an Integer (0 - Exit): ";
long long int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
std::cout << std::endl;
do
{
std::cout << n << " / " << DIVISOR;
n /= DIVISOR;
std::cout << " = " << n << std::endl;
} while ( n );
std::cout << std::endl;
}
return 0;
}
The program output might look like
Enter an Integer (0 - Exit): 1000
1000 / 4 = 250
250 / 4 = 62
62 / 4 = 15
15 / 4 = 3
3 / 4 = 0
Enter an Integer (0 - Exit): 0

Related

How should i solve counting problem in my divisor program C++

hi guys i recently started learning C++ and today i got a problem with counting divisors in this program
i want my program to count the number of divisors exactly but the output show weird numbers :/
#include <iostream>
using namespace std;
int main(void)
{
int Lowest;
int Highest;
int count = 0;
cout << "Enter The Lowest Number Of Series: ";
cin >> Lowest;
cout << "Enter The Highest Number Of Series: ";
cin >> Highest;
cout << "\n Number -> Divisors ((count)) \n";
for (int i = Lowest; i <= Highest; i++)
{
cout << " " << i << " ->";
for (int j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++; // WTF
cout << " " << j;
}
}
cout << " (( count:" << count << " )) " << endl;
}
return 0;
}
for example out put for Lower=1 to Highest=10 is like this:
Enter The Lowest Number Of Series: 1
Enter The Highest Number Of Series: 10
Number -> Divisors ((count))
1 -> 1 (( count:1 ))
2 -> 1 2 (( count:3 ))
3 -> 1 3 (( count:5 ))
4 -> 1 2 4 (( count:8 ))
5 -> 1 5 (( count:10 ))
6 -> 1 2 3 6 (( count:14 ))
7 -> 1 7 (( count:16 ))
8 -> 1 2 4 8 (( count:20 ))
9 -> 1 3 9 (( count:23 ))
10 -> 1 2 5 10 (( count:27 ))
Do you guys have any idea What is Wrong?
As noted by #cigien's comment, your count variable never gets reset to zero so it just keeps counting up. Reset it between each iteration of the outer loop. Better still, move the declaration to the closest place where it's used:
#include <iostream>
using std::cin;
using std::cout; // only using what you need
int main()
{
int Lowest;
int Highest;
cout << "Enter The Lowest Number Of Series: ";
cin >> Lowest;
cout << "Enter The Highest Number Of Series: ";
cin >> Highest;
cout << "\n Number -> Divisors ((count)) \n";
for (int i = Lowest; i <= Highest; i++)
{
int count = 0; // <-- initialize to zero on every pass
cout << " " << i << " ->";
for (int j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
cout << " " << j;
}
}
cout << " (( count:" << count << " )) \n";
}
return 0;
}

How do you add numbers in a while loop for c++?

#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;
}

Write a program that asks the user for up to 10 integers (it could be fewer); using arrays, and for loops

Write a program that asks the user for up to 10 integers (it could be fewer); stop prompting when the user enters zero. Then list the numbers in reverse order. Use the following example run as a guide:
Enter a number (0 to stop): 11
Enter a number (0 to stop): 33
Enter a number (0 to stop): 55
Enter a number (0 to stop): 77
Enter a number (0 to stop): 99
Enter a number (0 to stop): 0
Your numbers in reverse order are:
99, 77, 55, 33, 11
below is my current code and can't seem to figure out what i'm doing wrong.
#include <iostream>
#include <string>
using namespace std;
int main( ) {
int max = 10;
int num = 1;
int userVal[max];
int i = 0;
while(num <= max) {
cout << "Enter a number (0 to stop): ";
cin >> userVal[i];
cout << userVal[i] << endl;
if(userVal[i] == 0) {
break;
}
++num;
}
cout << num << endl;
cout << "Your numbers in reverse order are: " << endl;
for(i = num; i >= 0; --i) {
cout << userVal[i];
if(i < num - 2) {
cout << ", ";
}
}
return 0;
}
below is the output i'm getting obviously as stated above i want to numbers to print in reverse
Enter a number (0 to stop): 11
Enter a number (0 to stop): 33
Enter a number (0 to stop): 55
Enter a number (0 to stop): 77
Enter a number (0 to stop): 99
Enter a number (0 to stop): 0
6
Your numbers in reverse order are:
6553532600-6638791760, 4197268, 0, 0,
First, in your while loop, you are using i to assign the values in the array, you need num instead. Second, num should be set to zero so that the first element of the array is filled. Try this code:
#include <iostream>
#include <string>
using namespace std;
int main( ) {
int max = 10;
int num = 0;
int userVal[max];
int i = 0;
while(num <= max) {
cout << "Enter a number (0 to stop): ";
int number;
cin >> number;
cout << number << endl;
if(number == 0) {
break;
}
userVal[num] = number;
++num;
}
cout << num << endl;
cout << "Your numbers in reverse order are: " << endl;
for(i = num; i >= 0; --i) {
cout << userVal[i];
if(i < num - 2) {
cout << ", ";
}
}
return 0;
}
First: You use two different variables - num and i - but you only increment the num variable. Better:
int num = 0; // start with 0 - this is the number read initially: nothing...
while(num < max) // need to use < instead
std::cin >> userVal[num]; // can use your counter now as index, too...
However, you have to adjust your outputting loop accordingly:
for(int i = num - 1; i >= 0; --i)
Second: You want to print a comma always - unless for the last index (0); so:
if(i > 0)
std::cout << ", ";
Finally: You should declare max const or constexpr. C++ does not support variable length arrays on the stack. Normally, your compiler should complain about int userVal[max]; (maybe it accepts it this time as it recognizes max never being changed - but that's not C++ standard).
Thanks for all y'alls help the below code is what i came up with its a bit of a combined bit of each of your suggestions.
#include <iostream>
#include <string>
using namespace std;
int main( ) {
int max = 10;
int num = 1;
int userVal[max];
int i = 0;
while(num < max) {
cout << "Enter a number (0 to stop): ";
int number;
cin >> number;
cout << number << endl;
if(number == 0) {
break;
}
userVal[num] = number;
++num;
}
cout << "Your numbers in reverse order are: " << endl;
for(i = num - 1; i > 0; --i) {
cout << userVal[i];
if(i > 1) {
cout << ", ";
}
}
cout << endl;
return 0;
}
i received the below output
Enter a number (0 to stop): 11
Enter a number (0 to stop): 33
Enter a number (0 to stop): 55
Enter a number (0 to stop): 77
Enter a number (0 to stop): 99
Enter a number (0 to stop): 0
Your numbers in reverse order are:
99, 77, 55, 33, 11
Check your indexing in the input loop.

Output odd numbers between 2 integers in C++

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

find the each number of 4 digit of user input and count it using recursion

I have with my code. This is about recursion. I have to create function digitAppear( int findDigit, int value) where value is the user input, and findDigit is single digit number ranging from 0 to 9. The function read user input and return each digit number from the user input and count how many times each digit number occurs in the user input. For example, if I type 1234 then the output say 1 appear 1 time, 2 appear 1 time and so on (I hope my explanation is clear) The problem is the only run once and can only return 1 value.
#include <iostream>
using namespace std;
int countOccurence(int, int);
int main()
{
int findDig;
int value;
int n = 0;
cout << "Please enter a positive number: " << endl;
cin >> value;
cout << "The value is " << value << endl;
while ((value < 0) || (value > 9999))
{
cout << "Invalid value. Please try again!" << endl;
cout << "Please enter a positive number: " << endl;
cin >> value; //you need this here, otherwise you're going to be stuck in an infinite loop after the first invalid entry
}
//process the value
for (findDig = 0; findDig < 9; findDig++)
{
cout << endl;
cout << cout << "the " << findDig << "appear in digit " << value << " is " << countOccurence(findDig, value) << " times" << endl;
}
//countOccurance(findDig, value);
//cout
}
int countOccurence(int findDig, int value)
{
int n = value;
while( n > 10 )
{
int a = n / 10; //eliminate the right most integer from the rest
int aa = n % 10; //separate the right most integer from the rest
int b = a / 10; //eliminate the second integer from the rest
int bb = a % 10; //separate the second integer from the rest
int c = b / 10; // eliminate the third integer from the rest
int cc = b % 10; //separate the third integer from the rest
for (findDig = 0; findDig < 9; findDig++)
{
int i = 0;
if (findDig == aa) // see if the findDigit value is equal to single digit of b;
{
i += 1;
} else
{
i += 0;
}
return i;
if (findDig == bb)
{
i += 1;
} else
{
i += 0;
}
return i;
if (findDig == cc)
{
i += 1;
} else
{
i += 0;
}
return il;
}
}
}
The problem is my function countOccurence() doesn't seems right. I wonder if there a way to do it. I have been stuck with this for days and I really appreciate your input, thank you.
To use recursion, you must think about the problem in a different way.
The easiest way of thinking about how you could incorporate recursion into the function is the process of 'peeling off' each number.
A very simple way of doing this is by looking at the first/last digit in the number, compute that, then call itself on the remainder of the number.
Hopefully you can figure out the code from there.
If you mean that function digitAppear itself has to be recursive then it can look the following way as it is shown in the demonstrative program below
#include <iostream>
#include <cstdlib>
size_t digitAppear( int findDigit, int value )
{
return ( std::abs( value ) % 10u == std::abs( findDigit ) ) +
( ( value /= 10 ) ? digitAppear( findDigit, value ) : 0 );
}
int main()
{
int i = 0;
for ( int x : { 0, 11111, 1234, 34343 } )
{
std::cout << "There are " << digitAppear( i, x )
<< " digit " << i
<< " in number " << x
<< std::endl;
++i;
}
return 0;
}
The program output is
There are 1 digit 0 in number 0
There are 5 digit 1 in number 11111
There are 1 digit 2 in number 1234
There are 3 digit 3 in number 34343
Of course you may rewrite function main as you like for example that it would count each digit in a number.