Q:

C Program to Calculate Power of a Number

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following way:

 

  • C Program to Calculate Power of a Number Using For Loop
  • C Program to Calculate Power of a Number Using While Loop
  • C Program to Calculate Power of a Number Using Do While Loop
  • C Program to Calculate Power of a Number Using Function
  • C Program to Calculate Power of a Number Using Recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program to Calculate Power of a Number Using For Loop

Algorithm

  • Program Start
  • Declare Variable
  • Input base number and Power Number
  • Calculate Power
  • Display Result
  • Program End

Program

//C Program to Calculate Power of a Number Using For Loop

#include <stdio.h>
void main()
{
  int base, n, i;
	int result = 1;

	printf("Enter the base number : ");
	scanf("%d", &base);

	printf("Enter power number : ");
	scanf("%d", &n);

  for(i=1; i<=n; i++)
    {
        result = result*base;
    }

	printf("Given number is %d and power is %d and result is : %d",base, n, result);

}

Output

Enter the base number : 5
Enter power number : 3
Given number is 5 and power is 3 and result is : 125

C Program to Calculate Power of a Number Using While Loop

Program

//C Program to Calculate Power of a Number Using While Loop

#include <stdio.h>
void main()
{
    int base, n, i = 1;
	int result = 1;

	printf("Enter the base number : ");
	scanf("%d", &base);

	printf("Enter power number : ");
	scanf("%d", &n);

    while(i<=n)
    {
        result = result*base;

        i++;
    }

	printf("Given number is %d and power is %d and result is : %d",base, n, result);

}

Output

Enter the base number : 7
Enter power number : 3
Given number is 7 and power is 3 and result is : 343

C Program to Calculate Power of a Number Using Do While Loop

Program

//C Program to Calculate Power of a Number Using Do While Loop

#include <stdio.h>
void main()
{
  int base, n, i = 1;
	int result = 1;

	printf("Enter the base number : ");
	scanf("%d", &base);

	printf("Enter power number : ");
	scanf("%d", &n);

    do
    {
        result = result*base;

        i++;
    }while(i<=n);

	printf("Given number is %d and power is %d and result is : %d",base, n, result);

}

Output

Enter the base number : 10
Enter power number : 3
Given number is 10 and power is 3 and result is : 1000

C Program to Calculate Power of a Number Using Function

Algorithm

  • Program Start
  • Declare Variable
  • Input base number and Power Number
  • Calling Function
  • Calculate Power
  • Display Resullt
  • Program End

Program

//C Program to Calculate Power of a Number Using For Loop

#include <stdio.h>
void power(int base, int n)
{  int i, result =1;
    for(i=1; i<=n; i++)
    {
        result = result*base;
    }

	printf("Given number is %d and power is %d and result is : %d",base, n, result);
}
void main()
{
  int base, n;
	printf("Enter the base number : ");
	scanf("%d", &base);

	printf("Enter power number : ");
	scanf("%d", &n);

     power(base,n);

}

 

C Program to Calculate Power of a Number Using Recursion

Program

//Prime Number Program In C Using Recursion

#include<stdio.h>
int recursion(int ,int );
void main()
{  	
  int base, n;
	int result;

	printf("Enter the base number : ");
	scanf("%d", &base);

	printf("Enter power number : ");
	scanf("%d", &n);

	result= recursion(base, n);
	printf("Given number is %d and power is %d and result is : %d",base, n, result);


}
int recursion(int base,int n)
{  if(a!=0)
   {
     base = base * recursion(base ,n-1);
	 return(base);
   }
   else
	 return 1;
}

Output

Enter the base number : 3
Enter power number : 2
Given number is 3 and power is 2 and result is : 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Print Odd Numbers Between 1 To 100... >>
<< C Program To Check Vowel or Consonant...