Write a program in c language to find the value of a raised to power b where a and b are natural numbers using while loop?
Answers
Answered by
4
Hi there!
Here's the answer:
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¶ While loop
• It is one of iterative(or) looping (or) Repetition statement.
• while statements will be executed repeatedly as long as the expression remains true.
• SYNTAX :
while (exp)
{
//Body of Loop
}
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¶¶¶ Program implementation:
¶ a^(b) = a × a × a × …………
- Here, b times a is multiplied with itself.
- Base is ' a ' & exponent is 'b'.
- [Let R be the result of a^(b) in the program i.e., a variable is needed to store the result.
*** One variable is required to control the while loop repetition.
[ Let it be ' i ' in the program ]
*** Here no. of times of repetition of loop depends on the no. of times the base 'a' is multiplied with its exponent 'b'.
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
/* C- program to find result of a^b using while loop */
#include< stdio.h>
#include<conio.h>
int main()
{ // main function begins
// Declare Variables
…int a, b, R, i;
// Read values a & b from User
…printf("\n Enter Value for base a :");
…scanf("%d",&a);
…printf("\n Enter Value for Exponent b :");
…scanf("%d",&b);
// initialise Result 'R' and while loop control variable 'i'
…R = 1;
…i = 1;
//loop begins
…while( i <= b )
……{
………R = R × a;
………i++;
……}
//loop ends
//print result
…printf("The value of a raised to power b is : %d", R);
…return 0;
}// main function ends.
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¢#£€®$
:)
Hope it helps
Here's the answer:
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¶ While loop
• It is one of iterative(or) looping (or) Repetition statement.
• while statements will be executed repeatedly as long as the expression remains true.
• SYNTAX :
while (exp)
{
//Body of Loop
}
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¶¶¶ Program implementation:
¶ a^(b) = a × a × a × …………
- Here, b times a is multiplied with itself.
- Base is ' a ' & exponent is 'b'.
- [Let R be the result of a^(b) in the program i.e., a variable is needed to store the result.
*** One variable is required to control the while loop repetition.
[ Let it be ' i ' in the program ]
*** Here no. of times of repetition of loop depends on the no. of times the base 'a' is multiplied with its exponent 'b'.
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
/* C- program to find result of a^b using while loop */
#include< stdio.h>
#include<conio.h>
int main()
{ // main function begins
// Declare Variables
…int a, b, R, i;
// Read values a & b from User
…printf("\n Enter Value for base a :");
…scanf("%d",&a);
…printf("\n Enter Value for Exponent b :");
…scanf("%d",&b);
// initialise Result 'R' and while loop control variable 'i'
…R = 1;
…i = 1;
//loop begins
…while( i <= b )
……{
………R = R × a;
………i++;
……}
//loop ends
//print result
…printf("The value of a raised to power b is : %d", R);
…return 0;
}// main function ends.
•°•°•°•°•°<><><<><>><><>•°•°•°•°•°
¢#£€®$
:)
Hope it helps
Similar questions