Subscribe this Blog

Wikipedia

Search results

Notifications and New Posts!

Sunday, April 26, 2020

Audio Post - C program to find power of a given number using call by value type of function

This is the audio-texual post. To load audio mp3 file, it may take 10 to 15 seconds not more than that.
Please wait:


Above is the mp3 file which you can directly play from this page. Triangular shaped button can be pressed and audio gets started. For better experience of listening please use headphones. While listening above mp3 audio file, you can obsever following program. Listen and observe following program 2 to 3 times carefully, you will understand. 

/*A C Program to find power of a given number using call by value type of
function. Take number and power from user Example 2^3=8 */

#include<stdio.h>
#include<conio.h>
void power(int,int); //declaration of user defined function

void main()
{
int no,p;
clrscr();
printf("\n Please enter number=");
scanf("%d",&no);
printf("\n Please enter power=");
scanf("%d",&p);
power(no,p);    //calling of call by value type of function
getch();
}

void power(int no, int p)
{
int i,ans=1;
for(i=1;i<=p;i++)
{
ans=ans*no;
}
printf("\n %d^%d=%d",no,p,ans);
}
/*
 Please enter number=2
 Please enter power=3
 2^3=8
*/

Let's understand this program step by step 
Step 1: 
First of all letters include all preprocessor statements as stdio.h and conio.h.

Step 2:
Declaration of user defined function, here I have used power() as user defined function and this is call by value type of function. Here we are using two inputs number (no) and power(p). So number and power, we will be passing from main function to user defined function power.

Step 3:
main function- without main function program will not be executed. To execute entire program main function is required which is defined in step number 3.

Step 4: 
Inside the main function, first step is declaration of variables. no and p these two variables are used. Where no is is number and p is power. These two variables are integer type of variables.

Step 5:
Use of clrscr function - This function will help us to clear previous output screen.

Step 6:
To give the input to the program, here I have used two print functions and two scanf functions.
First printf function is used to display a message on the output screen as "please enter number". Then scanf function is used to accept number through the keyboard. Similarly next printf function is used to enter appropriate message on the output screen as as "please enter power". With the help of scanf function values of p, we can enter through keyboard. %d format specifier is used to accept integer values through keyboard.

Step 7: 
In this step, we are calling call by value type of function. To call this function no and p, these two variables, we are passing from main function to user defined function power().  Therefore no and p, these two variables are parameters here which we need to write in the round brackets.

Step 8:
getch function, this function will help us to hold the output screen after execution of the program.

Step 9: 
Where power function needs to be defined. This power function, we are defining outside the main function. While defining function outside the main function two parameters are declared in the round brackets no and p. Values of these variables/parameters are coming from main function.
In the round brackets first of all, we are declaring two variables. i and ans variables are declared as integer type of variables. where ans is initialised as 1. i is looping variable in the for loop. Value of i we are varying from 1 to p. Because we want multiplication of no, p times.
Therefore logical condition is written as i<=p. We are multiplying ans with no each time in for loop. 
At the last, outside the for loop, in the power function printf function is used to display result.

In short program will be executed as follows:
Execution of program is started from main function.
Because of main function, computer will accept two integer values of no and p through keyboard.
Due to calling of power function and as this function is call by value type of function this power function will pass values of no and p from main function to user defined function power.
power function will calculate power of a number and result will be stored in the ans variable with the help of for loop.

Please listen the audio clip you will understan this program for sure. For any doubts you can write me in the comment box below.

#sdbhosale

No comments:

Post a Comment