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)
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 :
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;
}
In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...
write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...
write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...
*Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise
Write a Python program to prompt the user for an integer number. Assign this integer to a variable X. Then assign a variable Y to the X**3 (X to the power 3). Finally, assign the variable Z to the square root of Y and print the values of X, Y, and Z as shown in the example below. Use main() function to define and call the program. Example: Enter an integer number: 3 X = 3 if Y=X**3 then Y...
C++ Write a void function called getAge that has no formal parameter. The function should prompt the user for their age. An example of the call to the function could be as follows getAge();
C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr); Here is an example of its use, in conjunction with print_decimal. Assume that the user will type a non-negative hex number as input. Also, assume that the “digits” a-f are in lower case. Let’s say the user types 1b int x; scan_hex(&x); print_decimal(x); MODIFY THIS CODE // function inputs a non-negative integer and stores it into *xptr. // The format of the input is...
C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...
How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...
Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...