Can u Write the int main(void) function as a driver program and call the above t
ID: 3863503 • Letter: C
Question
Can u
Write the
int main(void)
function as a driver program and call the above
three
1) Program:
#include<stdio.h>
#include<math.h>
int power(int, int);
int main(void)
{
int x, n;
printf("Enter a number and powerto raise it to: ");
scanf("%d %d", &x, &n); // Here we are scanning user inputs
printf("Result: %d ", power(n, x)); // Here we are printing the Result
return 0;
}
int power(int x, int n)
{
int m;
if (n == 0) return 1;
if (n % 2 == 0) {
m = power(x, n / 2);
return m * m; // Here we are returning the power value to the main function
}
else return x * power(x, n - 1); // Here we are returning the power value to the main
}
Output:
Enter a number and powerto raise it to: 3 2
Result: 8
2) Program:
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter any two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Greatest Common Divisor of %d and %d is %d.", n1, n2, hcf(n1,n2));
return 0;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2); // Here we are returning the hcf value to the main function
else
return n1; // Here we are returning the n1 value to the main function
}
Output:
Enter any two positive integers: 3 2
Greatest Common Divisor of 3 and 2 is 1.
3) Program:
#include <stdio.h>
int main()
{
int array[100], minimum, size, c, location = 1; // Here we are mentioning maximum size of an array
printf("Enter the number of elements in array ");
scanf("%d",&size);
printf("Enter %d integers ", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is %d", minimum);
return 0;
}
Output:
Enter the number of elements in array 4
Enter 4 integers 0 -1 2 3
Minimum element is -1
Explanation / Answer
#include<stdio.h>
#include<math.h>
int power(int, int);
int hcf(int n1, int n2);
int miminum(int *); // These three are function definition
int main(void)
{
int x, n;
printf("Enter a number and powerto raise it to: ");
scanf("%d %d", &x, &n); // Here we are scanning user inputs
printf("Result: %d ", power(n, x)); // Here we are printing the Result
return 0;
}
int power(int x, int n)
{
int m;
if (n == 0) return 1;
if (n % 2 == 0) {
m = power(x, n / 2);
return m * m; // Here we are returning the power value to the main function
}
else return x * power(x, n - 1); // Here we are returning the power value to the main
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2); // Here we are returning the hcf value to the main function
else
return n1; // Here we are returning the n1 value to the main function
}
int minimum(int array[])
{
int array[100], minimum, size, c, location = 1; // Here we are mentioning maximum size of an array
printf("Enter the number of elements in array ");
scanf("%d",&size);
printf("Enter %d integers ", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
//printf("Minimum element is %d", minimum);
return minimum; //Minimium is returned here to the main function
}
You can call these function from main.
in main you need to do is
int m= power(5, 2); //You can provide variables as well as by taking input from the user
int o= hcf(5,36); // Just like above you can call using variable as well.
int p= minimum(a);// Where a is an array in which you want to find the minimum.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.