How do i continue this while - c++

Im stuck with this program. What program does is to take a integer from user and display all number that are left after cutting the even numbers.
int main(){
long n, minder=0;
int cdonr, power=1;
cout<<"Give a positive number "<<endl;
cin>>n;
while (n>0){
cdonr=n%10;
if(cdonr % 2 != 0){
minder=minder+cdonr*power;
power=power*10;
}
n=n/10;
}
cout<<"The number that is left after all even number " << endl;
cout<<minder<<endl;
cout<<"Give a positive nr "<<endl;
cin>>n;
}
Can someone help me with this while because after it split the first numbers it gives no response at the second one.
Thanks to people on this community this is the answer
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}

Another answer as requested by user, exits upon == 0, <= 0, and not a number
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}

Try this, it is not infinite and you can do it as many times as you want by calling the function.
#include<iostream>
using namespace std;
void function(long n, long minder, int cdonr, int power) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
int main() {
long n, minder = 0;
int cdonr, power = 1;
//can add a while loop here or just use a for
// if you know how many times you want
cout << "Give a positive number " << endl;
cin >> n;
if(n > 0){
function(n, minder, cdonr, power);
}
//probably add an else here in case of == 0 or < 0
// or just loop back if while
}

If you really want to do something forever, loop on it.
i.e. put your "function" to be repeated in a loop.
(And consider pulling it out to be an actual function)
int main() {
using namespace std;
while (true)
{
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr*power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
}

Related

Return biggest number after writing out coalltz sequence of given number

So if I write out a collatz sequence of the given number long int n in my first function then in the next I wanna return the biggest number how do I do take for example if I call the first writeCollatzSekvens(7) it writes out
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 and I wanna return 52.
void writeCollatzSekvens(long int n){
cout << n << " ";
while(n !=1){
if ( n % 2 == 0){
n = n/2;
cout << n << " ";
}
else{
n = (n*3) + 1;
cout << n << " ";
}
}
}
long int collatzMax(long int n){
int max = 0;
if (n > max){
max = n;
}
return max;
}
You have the following problem in your "collatzMax"-function:
long int collatzMax(long int n){
int max = 0; // max has now the value 0
if (n > max){ // Therefore we do here if (n > 0) and that is always true. N is always greater then 0
max = n; // This assignment will always been done, because the if is always true
// max is now n
}
return max; // The function will always return n
}
Your function is stateless. It does not remember the "old" max value. This will not work. It is anyway not called in your other function. And there it is needed.
If you want to have the max collatz number, then you need to calculate max-values in your main loop.
And, if you have calculated your max number in the main loop, you can return it at the end of the function.
This could then for example look like that:
#include <iostream>
long int writeCollatzSekvens(long int n) { // Changed function prototype. It will return a value now
long int maxNumber = 0;
std::cout << n << " ";
while (n != 1) {
// Continously check for a new max number
if (n > maxNumber) maxNumber = n;
if (n % 2 == 0) {
n = n / 2;
std::cout << n << " ";
}
else {
n = (n * 3) + 1;
std::cout << n << " ";
}
}
return maxNumber; // Return the calculated max collatz number
}
int main() {
long int mn = writeCollatzSekvens(7);
std::cout << "\n\nMax: " << mn << '\n';
}
Of course there are many other potential solutions
EDIT
If the function should be void, then we can add an additional output parameter maxNumber. And pass this parameter by reference or by pointer.
Please see the example below. using pointer:
#include <iostream>
void writeCollatzSekvens(long int n, long int* maxNumber) { // Changed function prototype. Use output pointer for max
*maxNumber = 0;
std::cout << n << " ";
while (n != 1) {
// Continously check for a new max number
if (n > *maxNumber) *maxNumber = n;
if (n % 2 == 0) {
n = n / 2;
std::cout << n << " ";
}
else {
n = (n * 3) + 1;
std::cout << n << " ";
}
}
}
int main() {
long int mn = 0;
writeCollatzSekvens(7, &mn);
std::cout << "\n\nMax: " << mn << '\n';
}
Using reference
#include <iostream>
void writeCollatzSekvens(long int n, long int& maxNumber) { // Changed function prototype. Use output reference for max
maxNumber = 0;
std::cout << n << " ";
while (n != 1) {
// Continously check for a new max number
if (n > maxNumber) maxNumber = n;
if (n % 2 == 0) {
n = n / 2;
std::cout << n << " ";
}
else {
n = (n * 3) + 1;
std::cout << n << " ";
}
}
}
int main() {
long int mn = 0;
writeCollatzSekvens(7, mn);
std::cout << "\n\nMax: " << mn << '\n';
}

How do I limit the amount of numbers I have in a row?

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.

maximum power a number can be raised to with out exceeding y recursion

i have this program assignment and one part of it is trying to find the max power a number will go to(x) without exceeding a number the user inputs it not to exceed(y). we are using it in a function. this is the whole program and what i have for max power it just keeps returning 0. it is the int maxpower(int x, int y) function i am trying to figure out
#include <iostream>
#include <cmath>
using namespace std;
// meunue where you can get your options from
void menue() {
cout << "choose the following options:" << endl;
cout << "1) Power of x raised by y." << endl;
cout << "2) Find the max power a number can be raised to." << endl;
cout << "3) Print out a number with its digits in reversed order." << endl;
cout << "4) Sum of integers from 1 to n." << endl;
cout << "5) Product of integers from 1 to n." << endl;
cout << "6) Quit" << endl;
}
//functions for finding the power usign recursion
int Power(int a, int b) {
int x = 1, i;
for (i = 1; i <= b; i++) {
if (b == 0) {
return Power(a, b--);
}
else {
x = x * a;
}
}
return x;
}
int maxpower(int n, int max_value) {
int temp = temp * n;
if (temp > max_value)
return 0;
else return maxpower(n, max_value + 1);
}
int reverse(int number) {
int lastDigit, numberOfDigits, sign = 1;//sets the sign equal to one
// if number is less than 0 returns 0
if (number < 0) {
return 0;
}
else
//if a number is under 10 than it can not be switched so you times the number by 10 and switch it.
if (number < 10)
return number * sign;
lastDigit = number % 10;
number = number / 10;
numberOfDigits = log10(number) + 1;
//recursive statement that calls the function
return (lastDigit * pow(10, numberOfDigits) + reverse(number)) * sign;
}
//finding the sum
int sum(int n) {
if (n != 0) {
return n + sum(n - 1);//recursive statement
}
else {
return n;
}
}
//finding the product
int product(int n) {
int temp;
if (n <= 1) {
return 1;
}
else {
temp = n * product(n - 1);
// recursive statement setting temp == to recursive statement
return temp;//returning temp
}
}
int main() {
int a;
int x;
int y;
int length = 0;
int temp;
int results;
// calls menue and get prints all the options
do {
menue();
//inserts the choice
cin >> a;
cout << "you choose:" << a << endl;//prints the choice out.
//switch statement that will take account for the number you choose and prints the results
switch (a) {
case 1:
cout << "enter the number to raise" << endl;
cin >> x;
cout << " enter the power to raise to: " << endl;
cin >> y;
Power(x, y);
cout << "the result is:" << Power(x, y) << endl;
break;
case 2:
cout << "Enter the number to raise:" << endl;
cin >> x;
cout << "Enter the number not to exceed:" << endl;
cin >> y;
maxpower(x, y);
cout << "the result is:" << maxpower(x, y) << endl;
break;
case 3:
cout << " enter numbers to be reversed by: " << endl;
cin >> x;
temp = x;
while (temp != 0) {
length++;
temp = temp / 10;
}
reverse(x);
cout << "the result is:" << reverse(x) << endl;
break;
case 4:
cout << "enter the number to sum to: " << endl;
cin >> x;
sum(x);
cout << "the result is:" << sum(x) << endl;
break;
case 5:
cout << "enter the number to multiply to:" << endl;
cin >> y;
product(y);
cout << "the result is:" << product(y) << endl;
break;
case 6:
cout << "good bye!!" << endl;
break;
}
} while (a != 6);
return 0;
}
I don't think it's necessary to use recursion for this problem. Moreover, recursion is creating a lot of overhead while solving it with a loop works just fine. Do you have to use recursion? If so, then disregard this answer :p. But you'll find below a solution that will work.
Note the #include <math.h> bit - you need that to use pow(base, exponent).
Also, while(true) is definitely not the best practice, but as long as you have sufficient checks to get out of the loop properly then you're ok. Hence the max_iteration and the actual return statement that you're looking for.
Best of luck!
#include <iostream>
#include <math.h>
int maxpower(int n, int max_value) {
if ( n > max_value ) return 0;
int previous, current = 1;
int max_iteration = 0;
while (true) {
if (max_iteration >= 1000) return -1;
if (pow(n, current) > max_value) {
return previous;
}
previous = current;
current++;
max_iteration++;
}
}
int main() {
int x;
int y;
int result;
std::cout << "Enter the base: ";
std::cin >> x;
std::cout << "Enter the max number x^pow should not exceed: ";
std::cin >> y;
result = maxpower(x, y);
if (result == -1) {
std::cout << "Max iteration reached." << std::endl;
}
else {
std::cout << result << " is the maximum power such that " << x << "^" << result << " does not exceed " << y << std::endl;
}
return 0;
}
As an example of output:
If x = 2 and y = 32, the program will return 5 as the max power (i.e. 2^5 = 32 and is not greater than, but 2^6 > 32).
EDIT:
I realized after I posted that all of your functions are recursive, so perhaps that's a requirement for your assignment. Anyway, below is a recursive solution:
int maxpower_rec_helper(int n, int power, int max_value) {
if (pow(n, power) > max_value) return power - 1;
return maxpower_rec_helper(n, power + 1, max_value);
}
int maxpower_rec(int n, int max_value) {
if ( n > max_value ) return 0;
return maxpower_rec_helper(n, 1, max_value);
}
You'll need a helper function to give the initial power 1, and so as not to disturb your max_value.
return power - 1; is essentially the same thing as return previous; in the iterative example above.

Use while loop to print series of even and odd numbers

I know I'm missing something real simple but I can't seem to get the numbers to print out in rows of just odd or just even numbers using a while loop or loops. Also It keeps printing out "the even numbers are:"/ "the odd numbers are:" for every number.
#include<stdio.h>
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
while (n <= 100) //loop only if n equals 100 or less
{
for(number = n; number <= n; number++) //for loop to increment int value
{
if(number % 2 !=0) //determines if odd
{
cout << "The odd numbers are:" <<number << endl; //prints odd values
}
}
for(number = n;number <= n; number++) // for loop to increment int value
{
if(number % 2 ==0) //determines if even
{
cout <<"The even numbers are:" <<number <<endl; //prints even values
}
}
n++;
}
return 0; //end of program
}
You may want this:
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
// print odd values
cout << "The odd numbers are:";
for (number = n + 1 - (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
// print even values
cout << "The even numbers are:";
for (number = n + (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
return 0; //end of program
}

Classifying digits of an integer value

I spent a day on this code for count even and zero and odd numbers
From long datatype I used a function to send data. Here is the code
#include <iostream>
using namespace std;
void digitCount(long long int &num);
int main ()
{
long long int num;
cout <<"Enter any No. " <<endl;
cin >>num;
cout <<endl;
digitCount(num);
return 0;
}
void digitCount(long long int &num)
{
int e = 0, z = 0, o = 0, x = 0;
for (int i = 0; i <= num; i++)
{
x= num % 10;
if(x == 0)
{
++z;
num = num / 10;
}
else if(x%2==1)
{
++o;
num = num / 10;
}
else
{
++e;
num = num / 10;
}
}
cout << "No of zeros Digits = " << z<< endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
the problem is when I count odd numbers there is a number missed
for example when i input : 12345
the result is
no of even : 2
no of odd : 2 (should be 3)
no of zero : 0
and here the question :
Write a function that takes as parameter an integer (as a long value) and returns the number of odd, even, and zero digits. Also write a program to test your function. Use pass by reference method.
Instead of the for loop you should use:
while (num > 0)
You're constantly changing num and when it gets to 1 (in your 12345 example), i is at 3. I also modified your digitcount to demonstrate some decent formatting for readable code.
void digitCount(long long int &num) {
int e(0), z(0), o(0), x(0);
while (num > 0) {
x = num % 10;
if (x == 0) {
z++;
}
else if (x % 2 == 1) {
o++;
}
else {
e++;
}
num /= 10;
}
cout << "No of zeros Digits = " << z << endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
If you believe this solves your problem && is the best answer, please click the checkmark next to this answer. Thanks