The part i am not able to solve is - i am checking the number digits weather they are other than 1 or 0 using the checknumber(). Now, if the digits are other than 0 or 1 then ask the user to enter the num again till the digits are 0 and 1 only. After this, convert into binary and then again check the number is greater than -1 and the number is in negative then only exit. the main function:
int main()
{
int num = 0, flag;
while (num > -1) {
do {
cout << "Enter Number: ";
cin >> num;
} while (checknumber(num, flag) == 0);
cout << "Result in Decimal = ";
cout << binaryToDecimal(num) << endl;
}
}
This is my checknumber():
int checknumber(int number, int flag)
{
while (number != 0) {
int val = number % 10;
if ((val != 1) && (val != 0)) {
flag = 0;
break;
}
else {
flag = 1;
}
return flag;
}
}
the output i am getting when entering the number other than 0 or 1:
In your checknumber function, you're never dividing number, and thus you're stuck in an infinite loop. You're also returning on the first iteration of the loop. I do not understand the usage of flag either.
A corrected checknumber would be:
int checknumber(int number)
{
while(number != 0)
{
int val = number % 10;
if((val != 1) && (val != 0))
{
return 0;
}
number /= 10;
}
return 1;
}
Related
Recently started learning C++ and i wrote a simple program that defines is the given number prime or not, and when the input is zero, it finishes. But i accidentally noticed that it creates an infinite loop when the input is somewhere between 2100000000 and 2200000000. I don't know why this happens, can you please explain to me?
#include <iostream>
using namespace std;
int number, i, k;
int main()
{
do
{
cin >> number;
if (number == 0)
break;
for (i = 2; i < round(sqrt(number)) + 1; i++)
if (number % i == 0)
k++;
if ((number == 1) || (k > 0))
cout << "This number is not prime\n";
else
cout << "This number is prime\n";
k = 0;
i = 0;
} while (!(number == 0));
}
The value overflows its type (int) which maximum value is 2147483647 and it results in Undefined Behaviour. If you need to accept large numbers as input you may have to use a long long. Additionally, why check if number equals 0 twice?
#include <iostream>
#include <math.h>
int main()
{
while(true)
{
long long number;
bool isPrime = true;
std::cin >> number;
if(number == 0)
break;
for (int i = 2; i < round(sqrt(number)) + 1; i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
std::cout << "This number is prime\n"; //<== print this is this case
else
std::cout << "This number is not prime\n";
}
return 0;
}
I am trying to solve a problem where I have to take the input integers of users until users enter 0, then I have to reverse those words using a function.
I have written a code that works partially but it also gives the output 0 at the end, but the problem doesn't ask for 0 to be printed.
I have tried to fix it, but cannot find a way out.
#include <iostream>
using namespace std;
void reverse(int num) {
int rem = 0;
while (num != 0) {
rem = rem * 10 + num % 10;
num = num / 10;
}
cout << rem << endl;
}
int main() {
int num;
while (num != 0) {
cin >> num;
reverse(num);
if (num == 0)
break;
}
return 0;
}
int main() {
int num = -1;
while (num != 0) {
if (cin >> num) {
if (num == 0) {
break;
}
reverse(num);
}
}
return 0;
}
Hope this will help you and you learn something.
Take input first to avoid garbage values, then proceed to reverse if and only if the number is not equal to zero and a variable with valid type for cin is entered. (i.e. for an integer type variable, cin would expect an integer. Otherwise it will trigger its failbit)
int main()
{
int num;
do { cin>>num;
if(n==0 || !cin) // or !cin.good()
break;
reverse(num);
} while(!num=0)
return 0;
}
Examples:
Input:
314
51
hello
Output:
413
15
Input:
23
0
Output:
32
I need to know if a number has a repeating digit using recursion and return 'yes' or 'no'. I am not allowed to use loops or arrays. This is what I've done untill now with 10 global variables and it works, but I think there is a better way.
#include <iostream>
using namespace std;
int counter0 = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
bool check(int k) {
int p = k % 10;;
if (k < 10) {
return false;
} else {
if (p == 0) {
counter0++;
} else if (p == 1) {
counter1++;
} else if (p == 2) {
counter2++;
} else if (p == 3) {
counter3++;
} else if (p == 4) {
counter4++;
} else if (p == 5) {
counter5++;
} else if (p == 6) {
counter6++;
} else if (p == 7) {
counter7++;
} else if (p == 8) {
counter8++;
} else if (p == 9) {
counter9++;
}
if(counter1>1 || counter2>1 || counter3>1 || counter4>1 || counter5>1 || counter6>1 || counter7>1 || counter8>1 || counter9>1)
{
return true;
}
k=k/10;
check(k);
}
}
int main() {
//cout << "Hello, World!" << std::endl;
int n;
cin >> n;
cout << (check(n) ? "yes" : "no") << endl;
//cout << n/10;
return 0;
}
#include <iostream>
using namespace std;
bool hasRepeatingDigit(int n, int mask)
{
// base case: if we have checked all the digits and didn't find any duplicates, return false
if (n == 0)
return false;
/*
p is the place of the last digit in n (n%10).
A digit can range from 0 to 9.
The place of 0 will be 1 << 0 which is 1.
The place of 1 will be 1 << 1 which is 2.
The place of 2 will be 1 << 2 which is 4.
...
...
The place of 9 will be 1 << 9 which is 512.
*/
int p = 1 << (n % 10);
// if place of p has already been marked then it's a duplicate
if (mask&p)
return true;
// otherwise scrap the last digit (n/10), mark place p and recurse on the remaining digits
return hasRepeatingDigit(n / 10, mask|p);
}
int main()
{
int n;
cin >> n;
cout << hasRepeatingDigit(n, 0) << endl;
}
Recursion problems always have a base case and an recursive case.
The base case is simple: k<11 has no repeated digits.
For the recursive case, k has repeated digits if either:
the lower two digits of k are equal, or
k/10 has repeated digits.
So:
bool check(int k) {
if (k < 11)
return false;
int digit = k % 10;
int next = k / 10;
int digit2 = next % 10;
if (digit == digit2)
return true;
else
return check(next);
// Or in one expression:
// return (digit == digit2) || check(next);
}
first, the code is incorrect...
if you enter n=11 it says 'no' but 1 repeated twice. you can fix it by changing the if statement from if(k < 10) to if(k == 0)
you can get down to the bits level but I can't see how much is useful...
in conclusion, this is the best you can do without arrays...
BUT: if you need to find if a digit repeated twice or more in a row the other answer is perfect
I have this code:
#include <iostream>
#include <cmath>
using namespace std;
int n, liczba;
int main()
{
cin >> n;
for (int i = 0; i < n; i++) {
cin >> liczba;
if (liczba < 2) {
cout << "NIE" << endl;
} else if (liczba == 2) {
cout << "TAK" << endl;
}
for (int i = 2; i < liczba; i++) {
if (liczba % i == 0) {
cout << "NIE" << endl;
break;
} else if (liczba % i != 0) {
cout << "TAK" << endl;
break;
}
}
}
return 0;
}
This program is supposed to write yes "TAK" or no "NIE" whether the number you input is prime or isn't. Variable n is the number of numbers you want to input into program, and liczba is the number you want to check if it's prime or not. It seems to work fine expect one significant thing. If I input number 9 it says yes "TAK" instead of no "NIE".. I discovered that this happens to numbers: 9,27,45,63,81 and so on.. if I add 18 starting from 9 it will happen every time.
What's wrong with my code?
You're breaking on BOTH sides of your if() test. Effectly you'll only ever test one divisor:
e.g. liczba = 9
1. if (liczba % 2 == 0) -> if (9 % 2 == 0) -> if (1 == 0) -> false
2. ...jump to else
3. if (liczba % 2 != 0) -> if (9 % 2 != 0) -> if (1 != 0) -> TRUE
4. spit out 'tak' and break out of the loop
You cannot break out of the loop "early" if you get a remainder. That means the divisor you tested is NOT a factor of the number. You can only break early if you DO get a remainder of 0, which means the number's not prime - it's composite.
Hint:
All prime numbers (except 2 and 3) can be expressed in the form 6k+1
or 6k-1, where k is a positive whole number.
Therefore, This should work:
bool IsPrime( int number )
{
if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
return (false);
for( int k = 1; 36*k*k-12*k < number;++k)
if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
return (false);
return true;
}
Taken from Determining if a number is prime.
You have not used a flag and thus it says NIE after first check. Also, you are missing an else statement.
Try this instead:
#include <iostream>
#include <cmath>
using namespace std;
int n,liczba;
int main()
{
cin >> n; //
for(int i=0;i<n;i++)
{
cin>>liczba;
if (liczba < 2)
{
cout << "NIE" << endl;
}
else if (liczba == 2)
{
cout << "TAK" << endl;
}
else
{
bool isPrime = true;
for (int i=2;i<liczba;i++)
{
if (liczba % i == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
cout<<"TAK";
else
cout<<"NIE";
}
}
return 0;
}
Currently, if you input 7, It will check if 7 % 2 == 0. Since it is not, it will print "NIE" and break out of the loop.
I am not sure what the issue may be. I added "cout << count;" to the if statements to see why there is no return value 'val'.
But now I see there is an infinite loop occurring.
I'm essentially trying to count how many loops it takes for num = 0.
At first I thought it was num = count being in the loop.
Any suggestions? Thanks in advance.
#include <iostream>
using namespace std;
// hailstone function prototype
int hailstone(int &num);
int main()
{
int val;
cout << "Enter integer" << endl;
cin >> val;
hailstone(val);
cout << val << endl;
return 0;
}
// Pass values by reference to hailstone function header
int hailstone(int &num)
{
do
{
int count = 0;
// If num is even
// Divide by two
if(num % 2 == 0)
{
num = (num / 2);
count++;
cout << count;
}
// If num is odd
// Multiply by 3 and add 1
else if(num % 2 != 0)
{
num = (num * 3) + 1;
count++;
cout << count;
}
// Assign the number of steps to num
num = count;
} while(num > 0);
// Return the number of steps
return num;
}
There are a few issues here, but the most pertinent is this line:
// Assign the number of steps to num
num = count;
You're essentially resetting num after you update it based on the rules of the hailstone sequence. Because 'count' is initialized at the start of each loop and then iterated once in either the 'if' or 'else if' block, it is always one. Therefore, at the end of each loop, num is always 1.
Whether you are doing num = num/2; (for even numbers) or num = (num * 3) + 1 (for odd numbers), the number will never become 0 or less. That's why your code runs for infinite time. So you need to change the line while (num > 0); to while(num > 1); inside the function hailstone (maybe it is line number 54). As like as follow:
Old code:
int hailstone(int &num)
{
do
{
int count = 0;
// If num is even
// Divide by two
if(num % 2 == 0)
{
num = (num / 2);
count++;
cout << count;
}
// If num is odd
// Multiply by 3 and add 1
else if(num % 2 != 0)
{
num = (num * 3) + 1;
count++;
cout << count;
}
// Assign the number of steps to num
num = count;
} while(num > 0); //You need to change this line
// Return the number of steps
return num;
}
New Code:
int hailstone(int &num)
{
do
{
int count = 0;
// If num is even
// Divide by two
if(num % 2 == 0)
{
num = (num / 2);
count++;
cout << count;
}
// If num is odd
// Multiply by 3 and add 1
else if(num % 2 != 0)
{
num = (num * 3) + 1;
count++;
cout << count;
}
// Assign the number of steps to num
num = count;
} while(num > 1); //Here is the change
// Return the number of steps
return num;
}
Another issue: You declares num = count (in line 52), which will not return the actual value (if your code is for counting number of loops). Example: If the input is 12 than the loop will run for 10 times but your code will print 11, I don't think this line is required. Return the value of count variable after your do-while loop breaks.
Thanks
clearly either 'if' or 'else if' condition is going to be satisfied that will increment count to 1 , and assigning num =count makes num =1 ,so(num>0) is always true.so loop continues forever
do
{
int count = 0;
if (num % 2 == 0)
{
num = (num / 2);
count++;
cout << count;
}
else if(num % 2 != 0)
{
num = (num * 3) + 1;
count++;
cout << count;
}
num = count;
} while (num > 0);