Skip to content

Commit ca96f60

Browse files
authored
Add files via upload
1 parent 940d639 commit ca96f60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1522
-0
lines changed

MathPrograms/BigFactorial.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package MathPrograms;
2+
3+
import java.math.BigInteger;
4+
import java.util.Scanner;
5+
6+
public class BigFactorial {
7+
8+
public static void main(String[] args) {
9+
Scanner sc =new Scanner(System.in);
10+
System.out.println("Enter the number: ");
11+
int n=sc.nextInt();
12+
BigInteger b=new BigInteger("1");
13+
for(int i=1;i<=n;i++) {
14+
b=b.multiply(BigInteger.valueOf(i));
15+
}
16+
System.out.println("Factorial of a Number: "+b);
17+
sc.close();
18+
}
19+
}

MathPrograms/Factorial.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class Factorial {
6+
7+
//Recursive Factorial
8+
public static int fact(int n) {
9+
if(n<1) return 1;
10+
return n*fact(n-1);
11+
}
12+
//Non-recursive Factorial
13+
public static int nonRfact(int n) {
14+
int f=1;
15+
while(n!=0)
16+
f=f*n--;
17+
return f;
18+
}
19+
public static void main(String[] args) {
20+
Scanner sc = new Scanner(System.in);
21+
System.out.println("Enter the number: ");
22+
int t = sc.nextInt();
23+
int result=fact(t);
24+
System.out.println("Factorial of number: "+result);
25+
sc.close();
26+
}
27+
28+
}

MathPrograms/GCD.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
// Greatest Common Divisor
6+
public class GCD {
7+
// minimum of two numbers
8+
public static int min(int a, int b) {
9+
return a < b ? a : b;
10+
}
11+
12+
// GCD calculation
13+
public static int gcdCalculate(int x, int y) {
14+
int m = min(x, y), gcd = 1; // because 1 is common divisor for any number
15+
for (int i = m; i > 0; --i) {
16+
if (x % i == 0 && y % i == 0) {
17+
gcd = i;
18+
break;
19+
}
20+
}
21+
return gcd;
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner sc = new Scanner(System.in);
26+
System.out.print("Enter the first number : ");
27+
int first = sc.nextInt();
28+
System.out.print("Enter the Second number : ");
29+
int second = sc.nextInt();
30+
long result = gcdCalculate(first, second);
31+
System.out.println("GCD of Number " + first + " And " + second + " = " + result);
32+
sc.close();
33+
34+
}
35+
36+
}

MathPrograms/GCDEuclid.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
//Euclid's Algorithm to calculate GCD of two numbers
6+
7+
public class GCDEuclid {
8+
9+
public static int gcd(int x, int y) {
10+
int max,min;
11+
if(x<y) {
12+
max=y;min=x;
13+
}else{
14+
min=y;max=x;
15+
}
16+
17+
return (min == 0)?max:gcd(min, max % min);
18+
}
19+
20+
public static void main(String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
System.out.print("Enter the first number : ");
23+
int first = sc.nextInt();
24+
System.out.print("Enter the Second number : ");
25+
int second = sc.nextInt();
26+
int result = gcd(first, second);
27+
System.out.println("GCD of Number " + first + " And " + second + " = " + result);
28+
sc.close();
29+
30+
}
31+
32+
}

MathPrograms/LCM.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package MathPrograms;
2+
3+
import MathPrograms.GCDEuclid;
4+
import trick.FastReader;
5+
6+
public class LCM {
7+
8+
public static void main(String[] args) {
9+
FastReader sc=new FastReader();
10+
System.out.println("Enter the number: ");
11+
int a=sc.nextInt();
12+
int b=sc.nextInt();
13+
int hcf=GCDEuclid.gcd(a, b);
14+
int lcm=(a*b)/hcf;
15+
System.out.println("LCM of ("+a+", "+b+") = "+lcm);
16+
}
17+
}

MathPrograms/MyBigFactorial.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class MyBigFactorial {
6+
7+
public static int[] fact(int n) {
8+
int[] r = new int[100];
9+
r[0] = 1;
10+
for (int i = 1; i <= n; ++i) {
11+
int carry = 0;
12+
for (int j = 0; j < r.length; j++) {
13+
int x = r[j] * i + carry;
14+
r[j] = x % 10;
15+
carry = x / 10;
16+
}
17+
}
18+
return r;
19+
}
20+
public static void main(String[] args) {
21+
Scanner sc =new Scanner(System.in);
22+
System.out.println("Enter the number: ");
23+
int n=sc.nextInt();
24+
int[] result = fact(n);
25+
int i = result.length - 1;
26+
while (i > 0 && result[i] == 0)
27+
--i;
28+
while (i >= 0)
29+
System.out.print(result[i--]);
30+
System.out.println();
31+
sc.close();
32+
}
33+
34+
}

MathPrograms/NegPowerOfN.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class NegPowerOfN {
6+
7+
public static double PowerOfX(int num, int pow) {
8+
double res = 1;
9+
if (pow > 0) {
10+
for (int i = 0; i < pow; i++)
11+
res = res * num;
12+
} else {
13+
for (int i = 0; i < (-pow); i++)
14+
res = res / num;
15+
}
16+
return res;
17+
}
18+
19+
public static void main(String[] args) {
20+
Scanner sc = new Scanner(System.in);
21+
System.out.print("Enter the value of x: ");
22+
int x = sc.nextInt();
23+
System.out.print("Enter the power of x: ");
24+
int n = sc.nextInt();
25+
double result = PowerOfX(x, n);
26+
System.out.println(x + " Power " + n + " = " + result);
27+
sc.close();
28+
29+
}
30+
31+
}

MathPrograms/PascalTriangle.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class PascalTriangle {
6+
7+
public static void pascal(int n) {
8+
9+
}
10+
11+
public static void main(String[] args){
12+
13+
Scanner sc =new Scanner(System.in);
14+
System.out.print("Enter the length of pascal triangle: ");
15+
int n=sc.nextInt();
16+
// pascal(len);
17+
for(int i=1;i<=n;i++) {
18+
int c=0;
19+
for(int spaces=1;spaces<=n-i+1;spaces++)
20+
System.out.println(" ");
21+
int k=i;
22+
for(int j=1;j>=1;) {
23+
System.out.println(j+" ");
24+
if(j==k) {
25+
j--; k=j;
26+
c=1;
27+
}
28+
if(c==0)
29+
j++;
30+
}
31+
System.out.print("\n");
32+
}
33+
sc.close();
34+
35+
}
36+
37+
}

MathPrograms/PowerOfX.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* if x pow(n) then x(2)(n/2)
7+
* if x pow(n) then x(2)(n-1/2)
8+
*
9+
* */
10+
public class PowerOfX {
11+
12+
public static long xPowN(int x, int n) {
13+
int result = 1;
14+
while (n > 0) {
15+
if (n % 2 == 1)
16+
result = result * x;
17+
x = x * x;
18+
n = n / 2;
19+
}
20+
return result;
21+
}
22+
23+
/* To compute power of multiple numbers...! 2 pow(10 pow(9))
24+
*
25+
* int modularExponentiation(int x,int n,int M)
26+
{
27+
int result=1;
28+
while(n>0)
29+
{
30+
if(n % 2 ==1)
31+
result=(result * x)%M;
32+
x=(x*x)%M;
33+
n=n/2;
34+
}
35+
return result;
36+
}*/
37+
38+
public static void main(String[] args) {
39+
Scanner sc = new Scanner(System.in);
40+
System.out.print("Enter the value of x: ");
41+
int x = sc.nextInt();
42+
System.out.print("Enter the power of x: ");
43+
int n = sc.nextInt();
44+
long result = xPowN(x, n);
45+
System.out.println(x + " Power " + n + " = " + result);
46+
sc.close();
47+
}
48+
}

MathPrograms/SeriesSum.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class SeriesSum {
6+
7+
public static int sum(int n) {
8+
return ((n*(n+1))/2);
9+
}
10+
public static int sumSquare(int n) {
11+
return ((n*(n+1)*(2*n+1))/6);
12+
}
13+
public static int sumCubes(int n) {
14+
return ((n*n*(n+1)*(n+1))/4);
15+
}
16+
public static void main(String[] args) {
17+
Scanner sc = new Scanner(System.in);
18+
System.out.println("Enter two numbers: ");
19+
int a=sc.nextInt();
20+
System.out.println("Sum of Series: "+sum(a));
21+
System.out.println("Sum of Square of the Series: "+sumSquare(a));
22+
System.out.println("Sum of Cubes of the Series: "+sumCubes(a));
23+
sc.close();
24+
}
25+
}

MathPrograms/factorOfN.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package MathPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class factorOfN {
6+
public static void factor(int n) {
7+
for(int i=1;i<=n/2;i++) {
8+
if(n%i==0)
9+
System.out.println(i+", ");
10+
}
11+
}
12+
13+
public static void main(String[] args) {
14+
Scanner sc = new Scanner(System.in);
15+
System.out.println("Enter the number: ");
16+
int t = sc.nextInt();
17+
factor(t);
18+
sc.close();
19+
}
20+
21+
}

MathPrograms/greatestPrimeCheck.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package MathPrograms;
2+
import trick.FastReader;
3+
4+
public class greatestPrimeCheck {
5+
6+
public static int greatestPrime(int num) {
7+
int calculatedPrimeNo = 0;
8+
if (num == 0 || num == 1) {
9+
return 2;
10+
}
11+
do {
12+
boolean isPrime = true;
13+
for (int i = 2; i < Math.sqrt(num); i++) {
14+
if (num % i == 0) {
15+
isPrime = false;
16+
break;
17+
}
18+
}
19+
if (isPrime) {
20+
calculatedPrimeNo = num;
21+
} else {
22+
num++;
23+
}
24+
} while (calculatedPrimeNo != num);
25+
return calculatedPrimeNo;
26+
}
27+
28+
public static void main(String[] args) {
29+
FastReader fc=new FastReader();
30+
System.out.println("Enter the number: ");
31+
int num = fc.nextInt();
32+
System.out.println(greatestPrime(num) - num);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)