1 An Armstrong number of three digits is an integer such tha

1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1^3 + 5^3 + 3^3 = 153. Write a function named is_armstrong that takes one positive integer number as argument and returns a boolean variable which is true if the number is an Armstrong number and return false otherwise. Use this function is_armstrong in your main( ) function to print all Armstrong number in the range of 100 and 999.

2. The Greatest Common Divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. Write a function named calc_gcd that takes two positive integer arguments and returns the greatest common divisor of those two integers. If the function is passed an argument that is not greater than Zero, then the function should return the value -1 to indicate that an error occurred. For example,

cout << calc_gcd (8,12) ; // will print 4

cout << calc_gcd (256,625) ; // will print 1

cout << calc_gcd (0,8) ; // will print -1

cout << calc_gcd (10,-2) ; // will print -1

3. Write a function named double_swap that takes two doubles as arguments and interchanges the values that are stored in those arguments. The function should return no value. For example, if the following code fragment is executed,

int main()

{

double x = 4.8, y = 0.7;

double_swap(x, y);

cout <<”X= ”<< x <<” Y= ”<< y;

}

The output will be:

X= 0.7 Y= 4.8

4. A composite number is an integer that has at least one divisor other than one or the number itself. Any integer greater than one that is not a prime number is a composite number. Write a function named is_composite that takes one positive integer number as argument and returns a boolean variable which is true if the number is a composite number and return false otherwise. Use this function is_composite in your main( ) function to print all composite numbers in the range of 5 and 155.

Solution

// C++ code
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

//1
bool is_armstrong(int n)
{
int number = n;
int digit1, digit2, digit3;
digit1 = n%10;
n = n/10;
digit2 = n%10;
n = n/10;
digit3 = n%10;

if (number == pow(digit1,3)+pow(digit2,3)+pow(digit3,3))
{
return true;
}

return false;
}

// 2
int calc_gcd(int a, int b)
{
if(a <=0 || b < 0)
return -1;
else if (b != 0)
return calc_gcd(b, a%b);
else
return a;
}

// 3
void double_swap(double *x, double *y)
{
*x = *x + *y; // x now becomes 15
*y = *x - *y; // y becomes 10
*x = *x - *y; // x becomes 5
}

//4
bool is_composite(int n)
{
for (int i = 2; i < n; ++i)
{
if(n%i == 0)
return true;
}

return false;
}

int main()
{
cout << \"Armstrong numbers between 100 and 999 are: \";

for (int i = 100; i <= 999; ++i)
{
if(is_armstrong(i) == true)
{
cout << i << \" \";
}
}
cout << endl;
// output:
// Armstrong numbers between 100 and 999 are: 153 370 371 407
  
cout << calc_gcd (8,12) << endl; // will print 4
cout << calc_gcd (256,625) << endl; // will print 1
cout << calc_gcd (0,8) << endl; // will print -1
cout << calc_gcd (10,-2) << endl; // will print -1
cout << endl;

double x = 4.8, y = 0.7;
double_swap(&x, &y);
cout <<\"X= \"<< x <<\" Y= \"<< y;
// output: X= 0.7 Y= 4.8
cout << endl << endl;

cout << \"Composite numbers in the range of 5 and 155: \";
for (int i = 5; i <= 155; ++i)
{
if(is_composite(i) == true)
cout << i << \" \";
}
cout << endl;
/*
output:
Composite numbers in the range of 5 and 155: 6 8 9 10 12 14 15 16
18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46
48 49 50 51 52 54 55 56 57 58 60 62 63 64 65 66 68 69 70 72 74 75
76 77 78 80 81 82 84 85 86 87 88 90 91 92 93 94 95 96 98 99 100 102
104 105 106 108 110 111 112 114 115 116 117 118 119 120 121 122 123
124 125 126 128 129 130 132 133 134 135 136 138 140 141 142 143 144
145 146 147 148 150 152 153 154 155
*/

return 0;
}

1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstron
1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstron
1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstron

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site