EVALUATION OF THE FOLLOWING EQUATION AND SUM USING ‘GOTO’ AND ‘POW’ FUNCTION IN ‘C’ PROGRAMMING...
EQUATION FOR EVALUATION OF SUM IN 'C' |
SUM= (1+X+X2+X3+X4+..........+Xn)
Today in this we are going to discuss the above
equation and to evaluate the Sum by using ‘goto’ function and ‘pow’ function in
‘C’ programming.
So, let's start...............
Before starting let's see the programming code used here.......
PROGRAMMING CODE IN 'C' PROGRAMMING |
INITIALIZATION PART: here, we have taken ‘sum’ and ‘x’ as
float value.
Then we have taken
two variables ‘n’ and ‘i’ as an integer value. Lastly, a character variable has
been taken as ‘choice’.
This is the initialization part, we will see the use of the
above variables in the following parts.
float sum,x;
int n,i;
char choice;
Let's move to the argument making part.
ARGUMENT MAKING AND
MAIN CODING PART: In this part, we will
see the use of ‘goto’ and ‘pow’ function. Lets what is this thing and how they
work?
“GOTO” FUNCTION: The
function is already included in stdio.h header file. This function mainly
works to move to the control to a position of the programme where it is set to
move. That means it can move the control of the execution in any part of the
program code.
For example: if we write a program
int
n,x;
input:
printf(“Enter the value of ‘n’”);
scanf(“%d”,&n);
if(n%2==0)
{
printf( “The
number can be divided by 2”);
}
else
{
printf(“The number can’t be divided by 2”);
}
printf(“Do you want to
continue? press 0 or 1”);
if(x==1)
{
goto input;
}
else
printf(“Thank you........”);
By the above program, we can decide that the number given
by the user can be divided by 2 or not. Then we are taking a decision from the user
whether he wants to continue or repeat the program again or not. So, we need
here the ‘GOTO’ function. For this, we need to specify a location in the
program which tells the compiler to repeat the execution of the program
once more or again and again according to the input from the user. So, we have
marked the location as “input”. Remember after specifying the location and
taking the variable, there should be a colon after the declaration of the
location i.e (input:) and the code format will be “goto(space) (name as given
in the location)”.
In the given example if the user gives input ‘1’ then the
control goes to the “input” named location.
Thus, repeating the execution once more.
Now, What is “pow”?
‘POW’ denotes power. It is a math function included in “math.h”
header file. So, before using ‘pow’ function we must include the ‘math.h’
header file in the header section. The function mainly helps to make the power of a
number.
It is as the following format:
pow((number),(power))
for example:
pow(2,4)==24==16
or pow(3.5,2)==3.52==12.25
That how the ‘POW” function works.
let's move to our coding section.
we have used here a ‘for loop’ to evaluate the value of the sum.
sum=1;
for(i=1;i<=n;i++)
{
sum=sum+pow(x,i);
}
As seen, inside ‘for loop’ the argument is “sum=sum+pow(x, i)”
taken sum=1 and ‘for
loop’ starts with i=1 up to i<=n.
If user gives the input x=4 and n=3 then,
So, when i=1
sum=1+pow(4,1)==5
Now, the present value of ‘sum’=65
when i=2
sum=5+pow(4,2)==21
Now,the present value of ‘sum’=21
when i=3
sum=21+pow(4,3)=85
So, the evaluated ‘sum’ is equal to 85.
This way the argument works with ‘pow’ function by the
argument “sum=sum+pow(x,i)”
Now, the ‘for loop’ ends.
Next, we are moving towards the user’s decision making
section where he/she chooses whether he wants to repeat the program once more
or not.
printf("\n\nDo you want to continue ?\nPress y or n.....\n");
fflush(stdin); /*it is used to make the scanf function to get input from user without skipping*/
scanf("%c",&choice); /* or a space before %c can also do the same*/
if(choice=='y'||choice=='Y')
{
printf("\n\n");
goto input;
}
else
printf("\n\nThank You......");
Here, we are taking input from the user in the form of “Y/y
or N/n”.
NOTE: We have used a function “fflush(stdin)” to stay the
input from the user at the time of asking from the user. It can also be
achieved by giving a (space) before the formatting string “%c” i.e. “(space)%c”.
Without it, the input from the user can’t be taken, it will skip the respective
‘scanf’ part, thus not getting any input from the user. You can try it and see
what is happening without it.
Then, we have taken an ‘if’
function with the condition
if(choice==’Y’||choice==’y’)
As the input from the user is saved in the variable ‘choice’
, so we have given this condition. Here the sign ‘||’ stands for “OR” meaning
and remember the sign ‘&&’ stands for “AND” meaning. If the user enters
capital ‘Y’ or(||) small ‘y’ then, as
per ‘GOTO’ function control moves to ‘input’ location in the program.
Otherwise, the program ends with printing “Thank you......”
Hope, the given example is understandable to you. If any
doubt persists please comment below and if this helps you please share the
blog. Thank you so much.............
No comments:
Post a Comment