Java Program to Compute an-bn when n is a Natural Number

In the previous article we have discussed Java Program to Compute 2(ab+bc+ca) where Value of a, b and c are Given

In this program we are going to see how to compute an-bn   where value of a and b are given and when n is a natural number by using Java programming language.

Java Program to Compute an-bn when n is a Natural Number

The formula of an-bn is given below.

an-bn  = (a−b)(an−1+an−2b+an−3b2+…+abn−2+bn−1)

Example:

Suppose a=4, b=2, and n=3

Then

an-bn  =(a−b)(an−1+an−2b+an−3b2+…+abn−2+bn−1)

=(4-2)*(43-1*23-3 + 43-2*23-2 + 43-3*23-1)

=2*(16+8+4)

=2*28

=56

Now let’s see different ways to compute an-bn  

Method-1: Java Program to Compute an-bn when n is a Natural Number By Using pow() Function and Static Input Value

Approach:

  • Declare and initialize two integer variables say a and b.
  • Declare another integer variable say n, which is denoted as power value of the formula.
  • Take the value of a, b, and n as user input by using Scanner class.
  • By using the formula compute an-bn .
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //Declare the first input from the user.
        int a= 10;
        System.out.println("a= "+a);
        // Declare the second input from the user.
        int b= 8;
        System.out.println("b= "+b);
        // Declare the third input from the user.
        int n= 12;
        System.out.println("n= "+n);
        //declare another integer variable and assigned the  (a-b) value to it.
        int c= a-b;
        long sum=0;
        //Appling for loop for looping the formula.
        for(int i=1;i<=n;i++)
        {
            int e=n-(n-i+1);
            double d=Math.pow(a,n-i)*Math.pow(b,e);
            sum=sum+(long)d;
        }
        //print the result		
        System.out.println("an-bn= "+sum*c);
        }
}
Output:

a= 10
b= 8
n= 12
an-bn= 931280523264

Method-2: Java Program to Compute an-bn when n is a Natural Number By Using pow() Function and User Input Value

Approach:

  • Declare and initialize two integer variables say a and b.
  • Declare another integer variable say n, which is denoted as power value of the formula.
  • Take the value of a, b, and n as user input by using Scanner class.
  • By using the formula compute an-bn .
  • Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main{
    public static void main(String[] args){
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("a= ");
        //Take the first input from the user.
        int a= sc.nextInt();
        System.out.print("b= ");
        //Take the second input from the user.
        int b= sc.nextInt();
        System.out.print("n= ");
        //Take the third input from the user.
        int n= sc.nextInt();
        //declare another integer variable and assigned the  (a-b) value to it.
        int c= a-b;
        long sum=0;
        //Appling for loop for looping the formula.
        for(int i=1;i<=n;i++){
            int e=n-(n-i+1);
            double d=Math.pow(a,n-i)*Math.pow(b,e);
            sum=sum+(long)d;
        }
        //print the result		
        System.out.println("an-bn= "+sum*c);
        }
}
Output:

a= 10
b= 8
n= 12
an-bn= 931280523264

Method-3: Java Program to Compute an-bn when n is a Natural Number By Using User Defined Method

Approach:

  • Declare two integer variables say a and b.
  • Declare another integer variable say n, which is denoted as power value of the formula.
  • Take the value of a, b, and n as user input by using Scanner class.
  • Then call a user defined method say computeValue() and pass a and b as parameter.
  • Then inside method by using the formula compute an-bn .
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("a= ");
        //Take the first input from the user.
        int a= sc.nextInt();
        System.out.print("b= ");
        //Take the second input from the user.
        int b= sc.nextInt();
        System.out.print("n= ");
        //Take the third input from the user.
        int n= sc.nextInt();
        //call the user defined method
        computeValue(a,b,n);
    }
    
    //define the method
    public static void computeValue(int a, int b, int n)
    {
        //declare another integer variable and assigned the  (a-b) value to it.
        int c= a-b;
        long sum=0;
        //Appling for loop for looping the formula.
        for(int i=1;i<=n;i++)
        {
            int e=n-(n-i+1);
            double d=Math.pow(a,n-i)*Math.pow(b,e);
            sum=sum+(long)d;
        }
        //print the result		
        System.out.println("an-bn= "+sum*c);
}
}
Output:

a= 3
b= 2
n= 4
an-bn= 65

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: