Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 20: 20 10 5 2 1
#include <iostream>
using namespace std;
int main()
{
int userNum = 0;
userNum = 20;
cout << userNum << " ";
while (userNum > 1)
{
userNum = userNum/2;
cout << userNum << " ";
}
cout << endl;
return 0;
}
It divides properly until I get to 0, where it gives me 0 instead of terminating. What am I doing wrong?
Thanks!
Your posted code behaves just like you expect it to.
I am going to suggest changing code a little bit as a matter of good practice.
Instead of:
cout << userNum << " ";
while (userNum > 0)
{
userNum = userNum/2;
cout << userNum << " ";
}
Use:
while (userNum > 1)
{
cout << userNum << " ";
userNum = userNum/2;
}
The general principle is:
while ( <conditional> )
{
// Use the data
// Change the data as the last operation in the loop.
}
A for loop provides natural placeholders for these.
for ( <initialize data>; <conditional>; <update data for next iteration> )
{
// Use the data
}
If you were to switch to using a for loop, which I recommend, your code would be:
for ( userNum = 20; userNum > 0; userNum /= 2 )
{
cout << userNum << " ";
}
I tried all of these options but none of them worked with the other tests therefore I made some modifications. This works perfect for zybooks.
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
userNum = 40;
cin>>userNum;
while(userNum>1)
{
userNum=userNum/2;
cout<<userNum<<" ";
}
cout << endl;
return 0;
}
http://ideone.com/SvU8z3
#include <iostream>
using namespace std;
int main()
{
int userNum = 20;
while (userNum > 0)
{
cout << userNum << " ";
userNum = userNum/2;
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int userNum;
cin >> userNum;
while (userNum >= 4) {
userNum /= 4;
cout<<userNum<<" ";
}
cout << endl;
return 0;
}
Related
Is there some way to write a condition within a while loop that creates output if the user guesses a number that is within 10 units (plus or minus) from a random number generated by the program (integers)?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand ( time(NULL) );
bool valid;
int randNum;
int sum = 0;
int userNum;
for (int x = 1; x < 11; x++)
{
randNum = rand() % (71) + 7;
cout << "Random number " << x << ": " << randNum << endl;
sum = sum + randNum;
}
cout << "\nThe total of all the random numbers is " << sum << "\n\n";
cout << "Guess a number between 70 and 770: ";
do
{
cin >> userNum;
while(userNum >= 70 && userNum <= 770)
{
while(userNum == sum)
{
cout << "You win";
break;
}
while(/* the number given by the user is within 10 units from the random number generated by the program*/)
{
cout << "You almost won";
break;
}
break;
}
while(userNum < 70 || userNum > 770)
{
cout << "Try again.";
valid = false;
break;
}
}
while (!valid);
return 0;
}
You will have to #include <stdlib.h> then for the condition in your while loop you write abs(userNum - randNum) <= 10. This will give the magnitude of the difference between the userNum and randNum, which you want to be less than or equal to 10.
This is my code, which chooses a random number from 0 to 10 for the user to guess.
//guess the number game
//my code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
unsigned int secretNumber;
int guess;
int maxNumber = 10;
int maxTries = 4;
int numTries = 1;
srand(static_cast<unsigned int>(time(0)));
secretNumber = (rand() % 10)+ 1;
cout << "GUESS A NUMBER FROM 0 TO 10!!\n";
do {
cout << "\nGuess: \n";
cin >> guess;
if (guess < secretNumber)
{
cout << "too low:(:(!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
} ***//Would it be better to add a bool in the condition?***
else if (guess > secretNumber && guess <= maxNumber)
{
cout << "Too high:D:D!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
}
else if (guess > maxNumber)
{
cout << "Do you know how to count to 10?\n";
cout << "Only from 0 TO 10!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
}
else {
cout << "WOW! you GUESSED IT?! AMAZING!!!!";
cout << "You're right! the number is " << guess;
cout << "\nYou got it right in " << numTries << " guesses!!!";
}
if (numTries == maxTries)
{
cout << "\n\nYou LOOSE :( LOL!";
}
} while (guess != secretNumber && maxTries != numTries);
return 0;
}
This is the teacher's code, which is simpler and includes a bool variable.
Should my previous code be simpler, just as this one?
// Example program
// Teacher's code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int secretNumber = 7;
int guess;
int numTries = 0;
int maxTries = 3;
bool outOfGuesses = false;
while (secretNumber != guess && !outOfGuesses)
{
if (numTries != maxTries)
{cout << "Guess a Number: ";
cin >> guess;
numTries++;}
else
{
outOfGuesses = true;
}
}
if (outOfGuesses)
{
cout << "You loose!";
}
else
{
cout << "You win!";
}
return 0;
}
//Is my code as efficient and simple as the teacher's code?
//Is there a simpler way to do what I intended to do in my code?
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10] = { 0,0,0,0,0,0,0,0,0,0 };
while (number != 0) {
digits = number % 10;
counter[digits] = counter[digits] + 1;
number = number / 10;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
system("pause");
}
I'm having an issue with my code that when I run it and enter a Number nothing really happens. It is supposed to run something like 1234556789 and the output should look like
1 : 9
2 : 8
3 : 7
4 : 6
5 : 5
I know sometimes if there isn't a system pause this happens where it runs part of the code and just ends, but I'm not sure whats wrong here.
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10]={0},a=0;
while (number != 0) {
digits = number % 10;
counter[a] = digits; //made changes to this line
number = number / 10;
++a;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
}
All you are doing right now is printing how many digits there are of each number 0-9 in the number. If you want to pair elements together, then you can use std::vector and iterators. The number of digits in your input can be either even or odd and you would have to account for both cases.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long int number;
cout << "Enter Number: ";
cin >> number;
vector<int> digits;
if (number == 0)
{
digits.push_back(number);
}
while (number != 0)
{
digits.push_back(number % 10);
number /= 10;
}
auto it_begin = digits.begin();
auto it_end = digits.end() - 1;
if (digits.size() % 2 == 1)
{
for (; it_end != it_begin; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
cout << *it_end << endl;
}
else
{
for (; it_begin < it_end; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
}
}
With number = 1234556789, the output is:
1: 9
2: 8
3: 7
4: 6
5: 5
If you want first 10 no. Only use this code
include using namespace std;int main() {long int number;cout << "Enter Number: ";cin >> number;for (int i = 1; i<=10; i++) {cout << i << ": "<
My program has to count how many numbers in a range are even and how many of them are odd but I can't seem to figure it out.It kinda works
but when I put numbers in it spouts out nonsense. I'm an extreme nooob when it comes to programing, I think that the problem has to be at line 21 for (i=n; i<=m; i++) { ?
But I'm not sure. I have a programing book but it does not help much,maybe someone can help?
#include <iostream>
using namespace std;
int main()
{
int n;
int m;
int i;
int a;
int b;
cout << "Enter a number that begins interval: ";
cin >> n;
cout << "Enter a number that ends interval: ";
cin >> m;
a=0;
b=0;
for (i=n; i<=m; i++) {
if (i%2 == 0){
a=a+i;
}
else {
b=b+i;
}
}
cout << " unequal numbers: " << a << endl;
cout << " equal numbers: " << b << endl;
Assuming you mean even and odd numbers your problem lies in this code:
for (i=n; i<=m; i++) {
if (i%2 == 0){
a=a+i; // increase number of even numbers by i
}
else {
b=b+i; // increase number of odd numbers by i
}
}
What you might want do to do is add 1 (instead of whatever i is):
for (i = n; i <= m; ++i) {
if (i % 2 == 0)
++a; // increase number of even numbers by one
else
++b; // increase number of odd numbers by one
}
Also I'd suggest using better variable names, for example even and odd instead of a and b and so on. It makes code easier to understand for everybody, even for you.
Just a little more tips. Assigning variables as soon as you declare them is good practice:
int m = 0;
You can declare variable inside of for loop, and in your case there is no need to declare it out of it:
for (int i = n; i <= m; ++i) { ... }
Example how it can change look and clarity of your code:
#include <iostream>
using namespace std;
int main() {
int from = 0,
to = 0,
even = 0,
odd = 0;
cout << "Enter a number that begins interval: ";
cin >> from;
cout << "Enter a number that ends interval: ";
cin >> to;
for (int i = from; i <= to; ++i) {
if (i % 2 == 0)
++even;
else
++odd;
}
cout << " even numbers: " << even << endl;
cout << " odd numbers: " << odd << endl;
return 0; // don't forget this! main is function returning int so it should return something
}
Ok, so as per the new clarification the following should work
#include <iostream>
using namespace std;
int main()
{
int n;
int m;
int i;
int a;
int b;
cout << "Enter a number that begins interval: ";
cin >> n;
cout << "Enter a number that ends interval: ";
cin >> m;
a=0;
b=0;
for (i=n; i<=m; i++) {
if (i%2 == 0){
a++;
}else {
b++;
}
}
cout << " unequal numbers: " << a << endl;
cout << " equal numbers: " << b << endl;
}
So the following changes were done:
The for loop was closed
a = a + i or b = b + i was wrong as you are adding the counter value to the count which should be a++ or b++. Changed that also
The last two lines where you are showing your result was out of the main method, brought them inside the main method
Hope you find this useful.
You don't need to use loop to count even and odd numbers in a range.
#include <iostream>
int main ()
{
int n,m,even,count;
std::cin >> n >> m;
count=m-n+1;
even=(count>>1)+(count&1 && !(n&1));
std::cout << "Even numbers: " << even << std::endl;
std::cout << "Odd numbers: " << count-even << std::endl;
}
#include <iostream>
using namespace std;
int main()
{
int n, i;
cin >> n;
cout << " even : ";
for (i = 1; i <= n * 2; i++)
{
if (i % 2 == 0)
cout << i << " ";
}
cout << " odd : ";
for (i = 1; i <= n * 2; i++)
{
if (i % 2 != 0)
cout << i << " ";
}
return 0;
}
//input n = 5
// output is even : 2 4 6 8 10
// odd : 1 3 5 7 9
#include <iostream>
using namespace std;
int main()
{
int n;
int m;
int i;
int a;
int b;
cout << "Enter a number that begins interval: ";
cin >> n;
cout << "Enter a number that ends interval: ";
cin >> m;
a = 0;
b = 0;
for (i = n; i < = m; i++) {
if (i%2 == 0){
a = a + 1;
} else {
b = b + 1;
}
}
cout << " unequal numbers: " << a << endl;
cout << " equal numbers: " << b << endl;
}
Not sure why you are looping through all the elements (half of them are going to be even and the other half odd). The only case where you have to consider when the interval length is not divisible by two.
using namespace std;
int main()
{
int n;
int m;
int x;
int odds;
int evens;
cout << "Enter a number that begins interval: ";
cin >> n;
cout << "Enter a number that ends interval: ";
cin >> m;
cout << n << " " << m << endl;
x = m - n + 1;
odds = x / 2;
evens = odds;
if (x % 2 != 0) {
if (n % 2 == 0) {
evens++;
} else {
odds++;
}
}
cout << " even numbers: " << evens << endl;
cout << " odd numbers: " << odds << endl;
}
This is a more readable version of #Lassie's answer
I want to write a program that only takes odd numbers, and if you input 0 it will output the addition and average, without taking any even number values to the average and the addition. I'm stuck with not letting it take the even values..
Heres my code so far:
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin >> num;
numberOfInputs++;
addition = addition + num;
if (num % 2 != 0) {
//my issue is with this part
cout << "ignored" << endl;
}
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
}
}
Solution of your code:
Your code doesn't working because of following reasons:
Issue 1: You adding inputs number without checking whether it's even or not
Issue 2: If would like skip even then your condition should be as follow inside of the loop:
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
Solving your issues, I have update your program as following :
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin>> num;
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
numberOfInputs++;
addition = addition + num;
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
break;
}
}
}
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int number;
int sum=0;
int average=0;
int inputArray[20]; // will take only 20 inputs at a time
int i,index = 0;
int size;
do{
cout<<"Enter number\n";
cin>>number;
if(number==0){
for(i=0;i<index;i++){
sum = sum + inputArray[i];
}
cout << sum;
average = sum / index;
cout << average;
} else if(number % 2 != 0){
inputArray[index++] = number;
} else
cout<<"skip";
}
while(number!=0);
return 0;
}
You can run and check this code here https://www.codechef.com/ide
by providing custom input