Write a program to find Power of a number

01 July 2022

Write a program to find Power of a number


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

 

Description

Get any number as the input from user for base and exponents, then calculate the power of the number.

E.g.  let base = 3 and exponent=2 then we have to find 3^2. The answer will be 9.

Input

5

2

Output

25

 

C Program

Method 1

#include <stdio.h>

int main()

{

    int base,exponent;

    long long result=1;

    printf("Enter a base value: ");

    scanf("%d",&base);

    printf("Enter an exponent value: ");

    scanf("%d",&exponent);

    while(exponent!=0)

    {

        result*=base;

        exponent--;

    }

    printf("Answer = %lld",result);

    return 0;

}

 

Method 2

#include <stdio.h>

#include<math.h>

int main()

{

    int base,exponent;

    long long result=1;

    printf("Enter a base value: ");

    scanf("%d",&base);

    printf("Enter an exponent value: ");

    scanf("%d",&exponent);

    result = pow(base,exponent);

    printf("Answer = %lld",result);

    return 0; }

Java

import java.util.Scanner;

public class Main

{

                public static void main(String[] args) {

                                Scanner sc=new Scanner(System.in);

                                System.out.print("Enter the base value: ");                                                        

                                int base=sc.nextInt();

                                System.out.print("Enter the exponent value: ");                                                              

                                int exponent=sc.nextInt();

                int result=1;

                while (exponent!=0)

                {

                    result*=base;

                    exponent--;

                }

                System.out.println("Answer = "+result);

                }

}

 

Python

base=int(input("Enter base value: "))

exponent=int(input("Enter exponent value: "))

result=1

for i in range(0,exponent):

    result=result*base

print(result)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !