Categories
Programming

C++ Simple Math

Now that you’ve traced through using the while loop to count the digits in an integer, why don’t you try summing the digits?

#include <iostream>
using namespace std;

int main()
{
   int n = 853269; 
   int sum = 0; 
   int temp = n;
   while(temp > 0)
   {
      sum += temp % 10;
      temp = temp / 10;
   }
cout << "The sum of the digits in " << n << " is " << sum << endl;
}

Suppose we add $100 to a checking account in year 1, $200 in year 2, $300 in year 3, and so on. The account earns no interest. After how many years will the balance reach a given target? Modify the program below to produce the answer.

#include <iostream>

using namespace std;

int main()
{  
   double balance = 0;
   int year = 0;

   cout << "Target: " << endl;
   double target; 
   cin >> target;

   // Add $100 in year 1, $200 in year 2, ..., until the 
   // target has been reached

   while (balance < target)
   {  
      year++;
      double amount = 100 * year;
      balance = balance + amount;
   }
   
   cout << "Year: " << year << endl;
   cout << "Balance: " << balance << endl;
   
   return 0;
}

The sum of the reciprocals 1 + 1/2 + 1/3 + 1/4 + … is infinite. Write a program that reads in a target and finds the first n such that 1 + 1/2 + 1/3 + … + 1/n > target.

#include <iostream>

using namespace std;

int main()
{
   double sum = 0;
   int n = 0;

   cout << "Target: " << endl;
   double target; 
   cin >> target;

   while (sum <= target)
   {  
      sum += 1.0/++n;
   }

   cout << "n: " << n << endl;
   cout << "sum: " << sum << endl;

   return 0;
}

Write a while loop that prints all powers of 2 that are less than a given number n. For example, if n is 100, print 1 2 4 8 16 32 64.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   cout << "n: " << endl;
   int n; 
   cin >> n;
   int sum{0}, power{0};
   
   while (pow(2, power) < n)
   {
      
      sum = pow(2, power);
      cout << sum << " ";
      ++power;
   }
   cout << endl;
    
   return 0;
}

Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90.

#include <iostream>

using namespace std;

int main()
{
   cout << "n: " << endl;
   int n; 
   cin >> n;
   int count{1};
   
   while (count < n)
   {
      if(count % 10 == 0)
      {
         cout << count << " ";
      }
      ++count;
   }
   cout << endl;
    
   return 0;
}

Complete this program, prompting the user to to enter two positive numbers a and b so that a is less than b.

#include <iostream>
using namespace std;

int main()
{
   int a, b;

   // Keep prompting the user until the input is correct
   do
   {
      cout << "Enter two positive integers, the first smaller than the second." 
         << endl;
      cout << "First: " << endl;
      cin >> a;
      cout << "Second: " << endl;
      cin >> b;
   }while(b < a || a == 0);
   
   // Only print this when the input is correct
   cout << "You entered " << a << " and " << b << endl;
}

Write a do loop that reads integers and computes their sum. Stop when a zero is read or the when the same value is read twice in a row. For example, if the input is 1 2 3 4 4, then the sum is 14 and the loop stops.

#include <iostream>

using namespace std;

int main()
{
   int previous;
   int sum = 0;
   int input = 0;
   do
   {
      previous = input;
      cin >> input;
      sum += input;
   }
   while (input != previous && input != 0);
   cout << "Sum: " << sum << endl;
   return 0;
}