Description

In this program, we will take two inputs from the usrs, one will be the base and the another one is exponent.

For example: 32

3 is the base number.

2 is the exponent.

power is equal to 3 * 3.

Write a c program to calculate the power of given number without using the pow() function.

#include<stdio.h>

int main(){
    int base, exp, pow = 1;
    printf("Enter the base number : ");
    scanf("%d", &base);

    printf("Enter the exponent : ");
    scanf("%d", &exp);
    
    for(int i = 1; i <= exp; i++){
        pow = pow * base;
    }
    
    printf("%d ^ %d is : %d", base, exp, pow);

    
return 0;
}

Write a c++ program to calculate the power of given number without using the pow() function.

#include<iostream>
using namespace std;

int main(){

    int base, exp, pow = 1;
    cout<<"Enter the base number : ";
    cin>>base;

    cout<<"Enter the exponent : ";
    cin>>exp;

    for(int i = 1; i<=exp; i++){
        pow = pow * base;
    }

    cout<<base<<"^"<<exp<<" = "<<pow;
    return 0;
}

Algorithm to calculate power given number.

Step 1: START

Step 2: READ base and exp

Step 3: SET pow = 1

Step 4: START for loop, when condition (i<=exp) is true then go to STEP 5.

Step 5: CALCULATE pow = pow * base.

Step 6: EVERY Iteration increment i by 1.

Step 7: PRINT pow

Step 8: EXIT

Working of calculate of power.

Initial i = 1, i <= 3 condition true then. 

pow = 1 * 2 = 2.

pow = 2.

After this increment iteration i by 1.

Now i = 2

pow = 2 * 2 = 4

After this increment iteration i by 1.

Now i = 3

pow 4 * 2 = 8 

After this increment iteration i by 1.

Now i = 4.

Loop is Terminate Because the condition is false.

Calculate Power with pow() function in c programming.

#include<stdio.h>
#include<math.h>
int main(){
    int base, exp;
    printf("Enter the base number : ");
    scanf("%d", &base);

    printf("Enter the exponent : ");
    scanf("%d", &exp);
    int power = pow(base, exp);
    printf("%d", power);
    return 0;
}

Calculate power with pow() function in c++ programming.

#include<iostream>
#include<math.h>
using namespace std;

int main(){
    int base, exp;
    cout<<"Enter the base number : ";
    cin>>base;

    cout<<"Enter the exponent : ";
    cin>>exp;
    int power = pow(base, exp);
    cout<<power;
    return 0;
}

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Write a program to count the digit in a Number

To count the digits of a given number, divided that number by 10 until that number is greater than 0.

Insertion and Deletion of all operation at Singly Linked List in C Programming Language

Insertion and Deletion in Singly(singular) linked list in C programming langauge Data Structure. How to Implement insertion and deletion.

Simple Macro Substitution(#define) in C and C++ programming language

Program to Explain Simple Macro Substitution ( #define ) c and c++ programming language

Print numbers from 1 to 100 using while loop c and cpp program

C program to print numbers from 1 to 100 using while loop and also c++ program.

How to make swastik in c and cpp programming language

Write a c and c++ program to print the Swastik. How to make Swastik in c and cpp.

User Login System in C programming language

We create a login System program which takes username and password from the user.