Skip to content

Commit e38ec80

Browse files
authored
Add files via upload
1 parent 919131c commit e38ec80

26 files changed

+1946
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Scanner;
2+
3+
public class AlgebraicOperations {
4+
public static void main(String args[]) {
5+
6+
//Declaring variables
7+
double n1,n2,res = 0;
8+
9+
System.out.println("Algebraic Operations of two Numbers\n---");
10+
Scanner scanner = new Scanner(System.in);
11+
12+
System.out.println("Enter the number 1: ");
13+
n1 = scanner.nextDouble();
14+
15+
System.out.println("Enter the number 2: ");
16+
n2 = scanner.nextDouble();
17+
18+
System.out.println("---\nEnter 1 for addition,\n2 for subtraction\n3 for multiplication\n4 for division");
19+
int operator = scanner.nextInt();
20+
21+
//Solving by if else loops
22+
if(operator==1) {
23+
res =n1+n2;
24+
}
25+
else if(operator==2) {
26+
res =n1-n2;
27+
}
28+
29+
else if(operator==3) {
30+
res =n1*n2;
31+
}
32+
33+
else if(operator==4) {
34+
res =n1/n2;
35+
}
36+
37+
else{
38+
System.out.println("Error!");
39+
end();
40+
}
41+
42+
System.out.println(res);
43+
end();
44+
scanner.close();
45+
}
46+
47+
static void end() {
48+
System.out.println("Enter Y to continue or any key to exit!");
49+
50+
Scanner getEndOption= new Scanner(System.in);
51+
String option = getEndOption.next();
52+
if(option.equalsIgnoreCase("Y")) {
53+
main(null);
54+
}
55+
else {
56+
System.out.println("The Program has ended. Please run the program again.");
57+
}
58+
getEndOption.close();
59+
}
60+
61+
62+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.Scanner;
2+
3+
public class AlgrebraicOperationsSwitchCase {
4+
public static void main(String args[]) {
5+
6+
//Declaring variables
7+
double n1,n2,res = 0;
8+
9+
System.out.println("Algebraic Operations of two Numbers with Switch Case Loop\n---");
10+
Scanner scanner = new Scanner(System.in);
11+
12+
System.out.println("Enter the number 1: ");
13+
n1 = scanner.nextDouble();
14+
15+
System.out.println("Enter the number 2: ");
16+
n2 = scanner.nextDouble();
17+
18+
System.out.println("---\nEnter 1 for addition or\n2 for subtraction or\n3 for multiplication or\n4 for division");
19+
int operator = scanner.nextInt();
20+
21+
//Solving by switch case loop for simpler code.
22+
switch(operator) {
23+
case 1:
24+
res = n1+n2;
25+
break;
26+
case 2:
27+
res = n1-n2;
28+
break;
29+
case 3:
30+
res = n1*n2;
31+
break;
32+
case 4:
33+
res = n1/n2;
34+
break;
35+
default:
36+
System.out.println("You have chosen a wrong option.\nPlease choose from the above given options.");
37+
end();
38+
}
39+
System.out.println(res);
40+
end();
41+
scanner.close();
42+
}
43+
44+
static void end() {
45+
System.out.println("---\nEnter Y to continue or any key to exit!");
46+
47+
Scanner getEndOption= new Scanner(System.in);
48+
String option = getEndOption.next();
49+
if(option.equalsIgnoreCase("Y")) {
50+
main(null);
51+
}
52+
else {
53+
System.out.println("The Program has ended. Please run the program again.");
54+
}
55+
getEndOption.close();
56+
}
57+
58+
}
59+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
public class AsciiOfCharacter {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Program to get ASCII Value of Character.\n---");
8+
Scanner sm = new Scanner(System.in);
9+
10+
System.out.print("Enter the Character: ");
11+
char input = sm.next().charAt(0);
12+
13+
int output = (char)input;
14+
15+
System.out.println("The ASCII Value of '"+input+"' is: "+output);
16+
17+
sm.close();
18+
System.out.println("---\nThe Program has ended.");
19+
System.exit(0);
20+
21+
}
22+
23+
24+
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.math.BigInteger;
2+
import java.util.Scanner;
3+
4+
public class BigFactorial {
5+
6+
public static void main(String[] args) {
7+
8+
System.out.println("Program to find factorial of a number\n---");
9+
10+
int num, limit = 200; //recommended limit is 200 for RAM; can be upto 700
11+
Scanner scanner = new Scanner (System.in);
12+
13+
System.out.print("Enter the number: ");
14+
num = scanner.nextInt();
15+
16+
if (num>limit || num< -limit) {
17+
System.out.println("Cannot perform factorial for input greater than or lesser than "+limit+".");
18+
end();
19+
}
20+
scanner.close();
21+
22+
if (num<0) {
23+
num = -num;
24+
System.out.println("-"+num+"! = -"+factorial(num));
25+
end();
26+
//For numbers lesser than zero.
27+
}
28+
else {
29+
System.out.println(num+"! = "+factorial(num));
30+
end();
31+
//For numbers greater than or equal to zero.
32+
}
33+
34+
}
35+
36+
private static Object factorial(int num) {
37+
38+
long n = num;
39+
BigInteger output = new BigInteger("1");
40+
41+
while (n>0) {
42+
output = output.multiply(BigInteger.valueOf(n));
43+
n-=1;
44+
}
45+
return output;
46+
}
47+
48+
private static void end() {
49+
System.out.println("---\nThe Program has ended.");
50+
System.exit(0);
51+
}
52+
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Scanner;
2+
3+
public class CalculateAverageWithArray {
4+
5+
public static void main(String[] args) {
6+
7+
int num;
8+
double res,temp = 0;
9+
10+
System.out.println("Program to calculate the Average of the numbers entered in Array.\n---");
11+
System.out.println("Enter the number of elements:");
12+
Scanner sm = new Scanner(System.in);
13+
num = sm.nextInt();
14+
15+
int[] array = new int[num]; //declaring array
16+
17+
System.out.println("---\nEnter the elements: ");
18+
for(int i = 0; i<num; i++) {
19+
array[i]= sm.nextInt();
20+
}
21+
sm.close();
22+
for(int j=0; j<num; j++) {
23+
temp = temp + array[j];
24+
}
25+
res = temp/num;
26+
System.out.println("The average of the entered "+num+" numbers is: "+res);
27+
}
28+
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
3+
public class EvenOdd {
4+
5+
public static void main(String[] args) {
6+
7+
int num,flag;
8+
System.out.println("Program to check if the number is even or odd!\n---");
9+
System.out.println("Enter the number: ");
10+
Scanner scanner = new Scanner(System.in);
11+
num = scanner.nextInt();
12+
scanner.close();
13+
14+
flag = num % 2;
15+
if(flag == 0) {
16+
System.out.println("The number you entered is even.");
17+
}
18+
else {
19+
System.out.println("The number you entered is odd.");
20+
21+
}
22+
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.Scanner;
2+
3+
public class Factorial {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Program to find factorial of a number\n---");
8+
9+
int num;
10+
Scanner scanner = new Scanner (System.in);
11+
12+
System.out.print("Enter the number: ");
13+
num = scanner.nextInt();
14+
15+
if (num>20 || num< -20) {
16+
System.out.println("Cannot perform factorial for input greater than or lesser than 20.");
17+
end();
18+
}
19+
scanner.close();
20+
21+
if (num<0) {
22+
num = -num;
23+
System.out.println("-"+num+"! = -"+factorial(num));
24+
end();
25+
//For numbers lesser than zero.
26+
}
27+
else {
28+
System.out.println(num+"! = "+factorial(num));
29+
end();
30+
//For numbers greater than or equal to zero.
31+
}
32+
33+
}
34+
35+
private static long factorial(int num) {
36+
37+
long n = num, output = 1;
38+
39+
while (n>0) {
40+
output = output*n;
41+
n-=1;
42+
}
43+
return output;
44+
}
45+
46+
private static void end() {
47+
System.out.println("---\nThe Program has ended.");
48+
System.exit(0);
49+
}
50+
51+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class HelloWorld{
2+
public static void main(String[] args) {
3+
System.out.println("Hello World!!\nI'm learning Java.");
4+
}
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Scanner;
2+
3+
public class PositiveNegative {
4+
5+
public static void main(String[] args) {
6+
7+
int num; //Declaring Variables
8+
9+
System.out.println("Program to check if the entered number (int) is Positive or Negative.\n---");
10+
11+
Scanner sm = new Scanner(System.in);
12+
13+
System.out.print("Please enter the number: ");
14+
num = sm.nextInt();
15+
sm.close();
16+
17+
if (num>0) {
18+
System.out.println("The number you have entered is Positive.");
19+
}
20+
else if (num==0) {
21+
System.out.println("The number you have entered is ZERO.");
22+
}
23+
else {
24+
System.out.println("The number you have entered is Negative.");
25+
}
26+
27+
System.out.println("---\nThe Program has ended.");
28+
29+
}
30+
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Scanner;
2+
3+
public class PrimeNumberChecker {
4+
5+
public static void main(String[] args) {
6+
7+
long number,i;
8+
boolean flag = false;
9+
10+
System.out.println("A Program to check if the entered number is a Prime Number or not.\n---");
11+
Scanner sm = new Scanner(System.in);
12+
13+
System.out.print("Enter the Natural Number: ");
14+
number = sm.nextInt();
15+
16+
if (number==1) {
17+
System.out.println("1 is not a Prime Number."
18+
+ "\n---\nExplanation: "
19+
+ "A natural number (i.e. 1, 2, 3, 4, 5, 6, etc.) is called a prime number\n(or a prime) if it has exactly two positive factors, 1 and the number itself.\n1 has only one positive factor i.e. no. 1 only. Hence 1 is neither prime nor composite.");
20+
System.exit(0);
21+
}
22+
if (number<1) {
23+
System.out.println("Error: Input is negative or zero.\nTry Again!\n---");
24+
main(null);
25+
}
26+
27+
28+
for (i=2; i<number/2; i++) {
29+
if(number%i==0) {
30+
System.out.println(number+" is not a Prime Number. It is divisible by: "+i);
31+
flag=true;
32+
break;
33+
}
34+
if(flag) {
35+
break;
36+
}
37+
}
38+
if (flag==false) {
39+
System.out.println(number+" is a Prime Number.");
40+
}
41+
42+
sm.close();
43+
System.out.println("---\nThe Program has ended.");
44+
System.exit(0);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)