Question

In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...

In C++:
A. Write 3 functions that are called in the program.
 1. Function readInput prompt user to enter an integer to store to the parameter.
 2. Function isPerfectSquare take an integer parameter and checks whether it's a perfect square, that it's square root is an integer.
 3. Function min3 return the mainimum value of the parameter values, it shouldn’t use any if statement to compare the parameters but call min2 to do the comparison.
 Extra Credit: Function printPrimeFactorization print out parameter as a product of prime factors. 

B.
Write a function to print a diamond which calls following printChars function to print each characters.
void printChars(int x, char c)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answers below.

A.

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

void readInput(){
int number;
// Gettig user input
cout<<"Enter an integer value ";
cin>>number;
cout<<"You have entered "<<number<<endl;
}

void isPerfectSquare(int number){
int t=0;
for(int i=1 ; i<50 ; i++){
if (number == (i*i)){
cout<<"\nThe number "<<number<< " is a perfect square";
t++;
}
}
if (t==0){
cout<<"\nThe number "<<number<< " is not a perfect square";   
}
}
void min3 (int x, int y, int z){
int c = 0;
// In a loop, repeatedly subtract x, y and z by 1 and increment c.
// The number which becomes 0 first is the smallest.
// After the loop terminates, c will hold the minimum value of three numbers.
while (x && y && z) {
x--;
y--;
z--;
c++;
}
cout<<"\nThe minimum among three numbers is "<< c<<endl;
}
void printPrimeFactorization(int n)
{
cout<<"\nThe prime factors of "<<n<<" is "<<endl;
// Print the number of 2s that divide n
while (n % 2 == 0)
{
cout << 2 << " ";
n = n/2;
}
  
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
cout << i << " ";
n = n/i;
}
}
if (n > 2)
cout << n << " ";
}
  

int main()
{   
readInput();
isPerfectSquare(16);
isPerfectSquare(1000);
min3(3,8,2);
printPrimeFactorization(315);
  

return 0;
}

OUTPUT :

Enter an integer value 9 You have entered 9 The number 16 is a perfect square The number 1000 is not a perfect square The min

B.

Please find the program, output below.

#include <iostream>
using namespace std;

void printChars(int r, char c){
int i,j;
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<c;
cout<<endl;
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<c;
cout<<endl;;
}
}
  

int main()
{   
int value;
char ch;
cout<<"Enter the integer value : ";
cin>>value;
cout<<"Enter the character value : ";
cin>>ch;
printChars(value,ch);
return 0;
}

1 13 17 #include <iostream> 2 using namespace std; 3 4 void printChars(int r, char c){ 5 int i,j; 6 for(i=0;i<=r;i++) 7 { 8 f

Enter the integer value : 5 Enter the character value :

Enter the integer value : 5 Enter the character value : $ s $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $

Add a comment
Know the answer?
Add Answer to:
In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT