While Loops: simple division program outputting zero, don't understand why - c++

DIRECTIONS: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 = 40:
20 10 5 2 1
#include <iostream>
using namespace std;
int main() {
int userNum;
cin >> userNum;
while (userNum >= 1) {
userNum = userNum / 2;
cout << userNum << " ";
}
cout << endl;
return 0;
}
OUTPUT: (userNum = 40; 20 10 5 2 1 0)

In this part:
userNum = userNum / 2;
cout << userNum << " ";
When userNum = 1 (The final iteration of the loop) - 1/2 = 0.

You are doing an integer division .
Hence 1/2 = 0.5 after truncation it will be 0
By reading your question , it seems like you don't want the division of 1/2 to be done.
If so, check the condition in your while Loop
Make it userNum > 1 instead of userNum >= 1

Related

Sum of all integers that are both divisible by 4 and 6 between the first and second number

I need to write a program that will get 2 integers. The program will then display the sum of all integers that are both divisible by 4 and 6 between the two numbers
I tried doing the code down below:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1 + 1;i<num2;i++)
{
if ( num1 % 4 == 0 && num2 % 6 == 0)
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
system("pause");
return 0;
}
My expected result should be
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 12
"since 12 is the only number that is both divisible by 4 and 6"
But the actual results are
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 56
Here is the correct program:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1;i<=num2;i++) // if you want to include num1 and num2
//for(int i = num1+1;i<num2;i++) // if you do not want to include num1 and num2
{
if ( i % 4 == 0 && i % 6 == 0)
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
system("pause");
return 0;
}
The condition in if( num1 % 4 == 0 && num2 % 6 == 0 ) is wrong.
You need to change it to:-
(i % 4 == 0 && i % 6 == 0)
There are two issues:
At first, as 4 and 12 used as bounds shall yield 12, not 0, you need to include the bounds as well in your loop:
for(int i = num1; i <= num2; i++)
// ^ (!)
// ^ - 1 dropped
Then your condition to select the summands is not correct:
if(num1 % 4 == 0 && num2 % 6 == 0)
Be aware that this is always true for num1 == 4 and num2 == 12, so you'd sum up all numbers in between... What you actually want is to check the variable running in between these two borders, which is i:
if(i % 4 == 0 && i % 6 == 0) // i will be 4, 5, ... , 11, 12 (with above fixed loop)
Additionally, we can have it a bit shorter: a number is divisible by both 4 and 6, if it is divisible by 12. So your check might simply look like:
if(i % 12 == 0)
As stated, you only need check for divisibility by 12 and you need to use i as the check in the conditional. My system does not have pause(), so you can just use another cin >> call to create the pause. Of course, then you have to provide a letter/number input. A space is not sufficient.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
int wait_var;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1 + 1;i<num2;i++) // bounds are correct
{
if ( i % 12 == 0) // check for divisibility by 12 of i, not of the num1 and num2
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
cin >> wait_var;
return 0;
}

Displaying average number inputted by user greater than 50 and product of all numbers divisible by 3

I need to write a program that will get a series of numbers until a 0 is entered. The program then
displays the average of all numbers greater than 50 and the product of all numbers
divisible by 3.
I can already get the average numbers but when I entered less than 50 the program breaks and also I was not able to get the product of number divisible by 3.
#include <iostream>
using namespace std;
int main()
{
int total=0, result,num, inputCount=0, product = 1;
double average;
do{
cout << "Input numbers : " << endl;
cin >> num;
if(num>50){
inputCount++;
total = total+num;
}
}
while(num!=0);
cout<<"Average of numbers greater than 50 is ";
cout<<total/inputCount;
if(num % 3 == 0)
{
num*=num;
cout<< endl << "Product of all numbers divisible by 3 is " << num <<endl;
}
system("pause");
return 0;
}
I expect that the result will be :
Input num : 90
Input num : 9
Input num : 0
Average of numbers greater than 50 is : 90
Product of all numbers divisible by 3 is : 810
Since the user inputted 90, 9, and 0 the program stops getting input when 0 when entered and then the average of numbers greater than 50 is 90 since 90 is the only number that is greater than 50 while 90 and 9 are divisible by 3 so 90*9 = 810. But the actual output I get was
Average of numbers greater than 50 is : 90
Product of all numbers divisible by 3 is : 0
I tried doing the following, but when I entered 0, it also multiplied in the loop. how do I prevent that from happening?
do{
cout << "Input numbers : " << endl; cin >> num;
if(num>50){ inputCount++; total = total+num; }
if(num % 3 == 0) { product = product * num; }
cout<< endl << "Product of all numbers divisible by 3 is " << product <<endl;
} while(num!=0);
You should move that if (num % 3 == 0 into the loop.
if (num > 50) {
inputCount++;
total = total + num;
}
if (num % 3 == 0 && num > 0)
{
product *= num;
}
Also note the && num > 0, because 0 is also divisible by 3, and you want to ignore that or else your product is going to be 0. Alternatively, you could change your loop to abort on 0 before doing those checks. Outside of the loop, you should only have the output:
std::cout << "Product of all numbers divisible by 3 is " << product << std::endl;
On as side note, I would recommend handling the case for when no number greater than 50 is entered:
if (inputCount > 0) {
std::cout << "Average of numbers greater than 50 is " << total / inputCount << std::endl;
}
else {
std::cout << "No number greater than 50 was entered." << std::endl;
}
Otherwise the program crashes when this happens.

C++ Program to Display Numbers Between Two Intervals Check Whether a Number can be Express as Sum of Two Prime Numbers

i wrote a Program to Display Numbers Between Two Intervals and Check Whether can be Express as Sum of Two Prime Numbers
This is my code
//============================================================================================
// Check whether and display a number between two intervals can be expressed as 2 prime number
//============================================================================================
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int checkPrime (int);
int main()
{
int n1 , n2 , i , j;
bool flag = false;
cout << "Enter two number to check:" << endl;
cin >> n1 >> n2 ;
for (i = n1 ; i <= n2 ; i++)
{
for (j = 2 ; j <= i/2 ; j++ )
{
if(checkPrime (j) && checkPrime (i-j))
{
cout << "Number " << i << " equal sum of two prime number " << j << " + " << i - j << endl;
flag = true;
}
}
if (flag == false)
{
cout << "Number " << i << " can't epress to sum of two prime number " << endl;
}
}
system ("pause");
return 0;
}
int checkPrime (int n)
{
bool flag = true;
for (int i = 2; i <= n/2; i++)
{
if (n % i == 0)
{
flag = false;
break;
}
}
return flag;
}
Out put
Enter two number to check:
1
12
Number 1 can't epress to sum of two prime number
Number 2 can't epress to sum of two prime number
Number 3 can't epress to sum of two prime number
Number 4 equal sum of two prime number 2 + 2
Number 5 equal sum of two prime number 2 + 3
Number 6 equal sum of two prime number 3 + 3
Number 7 equal sum of two prime number 2 + 5
Number 8 equal sum of two prime number 3 + 5
Number 9 equal sum of two prime number 2 + 7
Number 10 equal sum of two prime number 3 + 7
Number 10 equal sum of two prime number 5 + 5
Number 12 equal sum of two prime number 5 + 7
Number 11 is missing :-s and i dont know why :( pls help me to fix it
You have to reset your flag at each loop otherwise once it's set with true value, this condition will never be reached if (flag == false).
for (i = n1 ; i <= n2 ; i++)
{
flag = false; /* Reset the flag */
for (j = 2 ; j <= i/2 ; j++ )
{
if(checkPrime (j) && checkPrime (i-j))
{
cout << "Number " << i << " equal sum of two prime number " << j << " + " << i - j << endl;
flag = true;
}
}
if (flag == false)
{
cout << "Number " << i << " can't epress to sum of two prime number " << endl;
}
}

Dividing a number in loop until it satisfies a condition = 0

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

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