×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
CO Prime Java Program to Exchange Odd Position Element with Even Position Element
calculate the power of the given number in Java - Java
3584    Arnab De    17/12/2018

Write a Java program to calculate the power of the given number. If we send two parameter (a,n) then it return an, but if we send one parameter (a) then it return a2.

Here, we use the function overloading procedure in java. Here, we create two methods called Power. First method with two parameters and another method with one parameter. But the name of the two methods are same.

import java.util.*;
public class JavaPower
{
    public int Power(int a)
    {
            return a*a;
    }
    public int Power(int a,int n)
    {
        int m=1;
        for(int i=0;i<n;i++)
        {
            m=m*a;
        }
        return m;
    }
    public static void main(String []args)
    {
        int a,n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter A Number : ");
        a=sc.nextInt();
        System.out.print("Enter A Power : ");
        n=sc.nextInt();
        JavaPower jp=new JavaPower();
        System.out.println( a + " Square = " + jp.Power(a));
        System.out.print(a + " To The Power = " + n + “ : ” + jp.Power(a,n));
    }
} 

CO Prime Java Program to Exchange Odd Position Element with Even Position Element
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Education is not the amount of information that is put into your brain and runs riot there, undigest
Swami Vivekananda
1061
79.79
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     53897
25/06/2018     44964
01/01/2018     43545
28/06/2017     41094
02/08/2017     40074
01/08/2017     34126
06/07/2017     33951
15/05/2017     33195
11/09/2018     30035
14/07/2017     29671
softetechnologies