Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca96f60

Browse files
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‎

Lines changed: 19 additions & 0 deletions
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‎

Lines changed: 28 additions & 0 deletions
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‎

Lines changed: 36 additions & 0 deletions
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‎

Lines changed: 32 additions & 0 deletions
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‎

Lines changed: 17 additions & 0 deletions
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‎

Lines changed: 34 additions & 0 deletions
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‎

Lines changed: 31 additions & 0 deletions
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‎

Lines changed: 37 additions & 0 deletions
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‎

Lines changed: 48 additions & 0 deletions
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‎

Lines changed: 25 additions & 0 deletions
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /