Hello
In my program, the function largest_prime_factor is not being called/working? Can you tell me what may be wrong?
The function largest_prime_factor, should return the largest prime number of the variable n5 (n5 = n3 + n4).
Plus if you see any other errors or maybe a better way of doing any part of my coding I would be very greatful :D ; especially with a code example
#include <iostream>
#include <cmath>
using namespace std;
// The precondition statement indicates what
// must be true before the function is called.
// The postcondition statement indicates what
// will be true when the function finishes its work.
bool largest_prime_factor(int b);
// precondition: a > 0
// postcondition: the function returns the largest prime factor of a
int squared_digit_length(int a);
// precondition: a > 0
// postcondition: the function returns the squared digit length starting from a
// you can define other functions if necessary
int main()
{
char ch1, ch2;
int n1, n2, n3, n4, n5; // used for the 5 steps of the task
cout << "Enter your first name and surname: " << flush;
cin >> ch1; // read the first letter of the first name
cin.ignore(100, ' '); // ignore the rest characters in the first name
cin >> ch2; // read the first letter of the surname
n1 = (int)ch1;
n2 = (int)ch2;
n3 = squared_digit_length(n1);
n4 = squared_digit_length(n2);
n5 = n4 + n5;
cout << n1 << n2 << endl;
squared_digit_length(n1);
squared_digit_length(n2);
largest_prime_factor(n5);
return 0;
}
int squared_digit_length(int a)
{
int ones,tens, hund;
double dones, dtens, dhund;
int co = 0;
while (a != 4)
{
if (a >= 100)
tens= (a/10)%10;
else tens= a/10;
ones= a%10;
hund= a/100;
dhund = pow((double)hund,2);
dtens = pow((double)tens,2);
dones = pow((double)ones,2);
co++;
a= (dhund + dones + dtens);
cout << "Hundreds= " << hund << " Tens= " << tens;
cout << " Ones= " << ones;
cout << " Count= " << co << " n1= "<< a << endl;
}
cout << "n3 =" << co << endl;
}
bool largest_prime_factor(int b)
{
bool p = true;
if (b == 1)
return false;
else if (b == 2)
return true;
else if (b % 2 == 0)
return false;
for (int i = 2; i * i <= b; i++)
{
if (b % i == 0) p = false;
}
return p;
}