Java Mathematical Solutions

  1. Factorisation example in Java
  2. Differentiation example in Java
  3. Integration example in Java
  4. Linear Equation example in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Factorisation example in Java


Factorisation_Ex1.java

import java.util.Scanner;
 
public class Factorisation_Ex1 {
	
	public static void main(String srga[]) {
		
		Scanner scan = new Scanner(System.in);
		
		double a = 0, b = 0, c = 0, x1, x2;
		
		System.out.println("Your equation is just like below :\nax^2+bx+c\n");
		
		try {
			
			System.out.println("Enter the Co-efficient of x^2 (a) : ");
			a = Double.parseDouble(scan.nextLine());
			
			System.out.println("Enter the Co-efficient of x (b) : ");
			b = Double.parseDouble(scan.nextLine());
			
			System.out.println("Enter the constant value (c) : ");
			c = Double.parseDouble(scan.nextLine());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		x1 = (-b + (Math.sqrt( (b*b) - (4*a*c)))) / (2*a);
		x2 = (-b - (Math.sqrt( (b*b) - (4*a*c)))) / (2*a);
		
		System.out.println(x1);
		System.out.println(x2);
	}
}

Sample Output

Your equation is just like below :
ax^2+bx+c

Enter the Co-efficient of x^2 (a) : 
1
Enter the Co-efficient of x (b) : 
-5
Enter the constant value (c) : 
6
x1 : 3.0
x2 : 2.0




B. Differentiation example in Java


Differentiation_Ex1.java

import java.util.Scanner;
 
public class Differentiation_Ex1 {
	
public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Let us consider our equation is like below :" +
				"\nax^n + bx^n-1 + ...... + z");
		
		System.out.println("\nEnter n value :");
		int n = Integer.parseInt(scan.nextLine());
		int temp = n;
		float num[] = new float[n+1];
		
		for(int i=0; i<=n; i++) {
			
			System.out.println("Enter the Co-efficient of x^" + (temp-i) + " :");
			num[i] = Float.parseFloat(scan.nextLine());
		}
		
		for(int i=0; i<n; i++) {
			
			String str = (temp-i) * num[i] + " x^" + (temp-i-1);
			System.out.println(str);
		}
	}
}

Sample Output

Let us consider our equation is like below :
ax^n + bx^n-1 + ...... + z

Enter n value :
3
Enter the Co-efficient of x^3 :
6
Enter the Co-efficient of x^2 :
12
Enter the Co-efficient of x^1 :
20
Enter the Co-efficient of x^0 :
8
18.0 x^2
24.0 x^1
20.0 x^0




C. Integration example in Java


Integration_Ex1.java

import java.util.Scanner;
 
public class Integration_Ex1 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Let us consider oru equation is like below :" +
				"\nIntegration of (ax^n + bx^n-1+ .... + z)");
		
		System.out.println("\nEnter n value :");
		int n = Integer.parseInt(scan.nextLine());
		int temp = n;
		float num[] = new float[n+1];
		
		for(int i=0; i<=n; i++) {
			
			System.out.println("Enter the Co-efficient of x^" + (temp-i) + " :");
			num[i] = Float.parseFloat(scan.nextLine());
		}
		
		for(int i=0; i<=n; i++) {
			
			String str = (num[i] / ((temp-i)+1)) + " x^" + ((temp-i)+1) ;
			System.out.println(str);
		}
	}
}

Sample Output

Let us consider oru equation is like below :
Integration of (ax^n + bx^n-1+ .... + z)

Enter n value :
2
Enter the Co-efficient of x^2 :
12
Enter the Co-efficient of x^1 :
9
Enter the Co-efficient of x^0 :
10
4.0 x^3
4.5 x^2
10.0 x^1






D. Linear Equation example in Java


Linear_Ex1.java

import java.util.Scanner;
 
public class Linear_Ex1 {
	
	float a1, a2, b1, b2, c1, c2;
	float x, y;
	Scanner scan;
	
	void getValue() {
		
		System.out.println("Consider our equation is like below :" +
				"\na1x + b1y = c1\na2x + b2y = c2\n");
		
		scan = new Scanner(System.in);
		
		System.out.print("Enter value of a1 : ");
		a1 = Float.parseFloat(scan.nextLine());
		
		System.out.print("Enter value of b1 : ");
		b1 = Float.parseFloat(scan.nextLine());
		
		System.out.print("Enter value of c1 : ");
		c1 = Float.parseFloat(scan.nextLine());
		
		System.out.print("\nEnter value of a2 : ");
		a2 = Float.parseFloat(scan.nextLine());
			
		System.out.print("Enter value of b2 : ");
		b2 = Float.parseFloat(scan.nextLine());
		
		System.out.print("Enter value of c2 : ");
		c2 = Float.parseFloat(scan.nextLine());
	}
	
	void calculate() {
		
		float da1, da2, db1, db2, dc1, dc2;
		
		da1 = a1; da2 = a2;
		db1 = b1; db2 = b2;
		dc1 = c1; dc2 = c2;
		
		da1 = a1 * b2;
		db1 = b1 * b2;
		dc1 = c1 * b2;
		
		da2 = a2 * b1;
		db2 = b2 * b1;
		dc2 = c2 * b1;
		
		if(db1 == db2) {
			
			da2 = da2 * -1;
			db2 = db2 * -1;
			dc2 = dc2 * -1;
		}
		
		float sum_a = da1 + da2;
		float sum_c = dc1 + dc2;
		
		x = sum_c / sum_a;
		y = (c1 - (a1 * x)) / b1;
		
		String Str_X = "x value : " + x ;
		String Str_Y = "y value : " + y ;
		
		System.out.println("\n" + Str_X);
		System.out.println(Str_Y);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Linear_Ex1 obj = new Linear_Ex1();
		
		obj.getValue();
		obj.calculate();
	}
}

Sample Output

Consider our equation is like below :
a1x + b1y = c1
a2x + b2y = c2

Enter value of a1 : 2
Enter value of b1 : 5
Enter value of c1 : 52

Enter value of a2 : 3
Enter value of b2 : 6
Enter value of c2 : 65

x value : 4.3333335
y value : 8.666666




SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com