Skip to content

Commit a70229b

Browse files
Added : Basic Core Java and Functional Programming Practice Problem
0 parents  commit a70229b

12 files changed

+300
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class Distance {
6+
7+
//program Distance.java that takes two integer command-line arguments x
8+
//and y and prints the Euclidean distance from the point (x, y) to the origin (0, 0). The
9+
// formulae to calculate distance = sqrt(x*x + y*y). Use Math.power function
10+
11+
public static void main(String[] args) {
12+
Scanner sc =new Scanner(System.in); //scanner input
13+
System.out.println("Enter the value of x");
14+
int x = sc.nextInt();
15+
System.out.println("Enter the value of y");
16+
int y = sc.nextInt();
17+
18+
double distance = Math.sqrt( x * x + y * y ); //formula for distance
19+
System.out.println("Euclidean distance : " + distance);
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class EvenOddNumberr {
6+
7+
public static void main(String[] args) {
8+
//Java Program to Check Whether a Number is Even or Odd
9+
Scanner sc = new Scanner(System.in); // scanner iinput
10+
System.out.println("Enter the number : ");
11+
int number = sc.nextInt();
12+
if(number % 2 == 0) // if number is divisble by 2
13+
System.out.println("Even number");
14+
else
15+
System.out.println("odd number");
16+
17+
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class Factor {
6+
7+
public static void main(String[] args) {
8+
// Computes the prime factorization of N using brute force.
9+
// calculating the prime factors and printing the result
10+
Scanner sc = new Scanner(System.in); // taking user input using scanner class
11+
System.out.println("Enter any number : ");
12+
int num = sc.nextInt();
13+
System.out.println("Prime Factors of " + num + " is :");
14+
for (int i = 2; i * i <= num; i++) {
15+
if (num % i == 0)
16+
num = num / i;
17+
System.out.println(i);
18+
}
19+
}
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class FlipCoin {
6+
7+
public static void main(String[] args) {
8+
// Flip Coin and print percentage of Heads and Tails
9+
10+
// Random random = new Random(); // creating random object
11+
12+
int count = 0;
13+
int tail = 0;
14+
int head = 0;
15+
Scanner sc = new Scanner(System.in);
16+
System.out.println("number of time you want to flips coin is : ");
17+
int flip = sc.nextInt();
18+
while (count < flip) {
19+
double flips = Math.random();// random value will be 0 or 1 in decimal form
20+
count++; // increase the count value till while loop iterates
21+
22+
if (flips <= 0.5) {
23+
24+
System.out.println("Tail");
25+
tail++; // Increase the count value for tail
26+
27+
} else {
28+
29+
System.out.println("Head");
30+
head++; // increase the count value for head
31+
}
32+
System.out.println("Number of heads = " + head);
33+
System.out.println("Number of tails = " + tail);
34+
35+
double headpercentage = (double) head / flips * 100; // head percentage
36+
double tailpercentage = (double) tail / flips * 100; // tail percentage
37+
38+
System.out.println("Percentage of head = " + headpercentage);
39+
System.out.println("Percentage of tail = " + tailpercentage);
40+
41+
}
42+
}
43+
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class HarmonicNumber {
6+
//the Nth harmonic number: 1/1 + 1/2 + ... + 1/N
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in); //scanner input
10+
System.out.println("enter the value of n : ");
11+
double num = sc.nextDouble();
12+
double sum = 0.0;
13+
for(int i = 1; i <= num; i++) {
14+
sum += 1.0 / i;
15+
}
16+
System.out.println("Nth Harmonic Num : " + sum);
17+
18+
}
19+
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class LargestNumber {
6+
7+
//Java Program to Find the Largest Among Three Numbers
8+
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in); //scannerr input
11+
System.out.println("Enter first number : ");
12+
int number1 = sc.nextInt(); // storing 1st input
13+
System.out.println("Enter second number : ");
14+
int number2 = sc.nextInt(); //storing 2nd input
15+
System.out.println("Enter third number : ");
16+
int number3 = sc.nextInt(); //storingg 3rd input
17+
18+
if (number1 > number2 && number1 > number3) // comparing all 3 numbers
19+
System.out.println("Largest number is number one i.e :" + number1);
20+
else if (number2 > number1 && number2 > number3)
21+
System.out.println("Largest number is number two i.e :" + number2);
22+
else
23+
System.out.println("Largest number is number three i.e :" + number3);
24+
25+
}
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class LeapYear {
6+
7+
public static void main(String[] args) {
8+
/* leap year program
9+
*A year is a leap year if divisible by 4 except for century year (years ending with 00)
10+
*century year is leap year only if it perfectly divisible by 400
11+
*/
12+
13+
System.out.println("Enter any year");
14+
Scanner sc = new Scanner(System.in);
15+
int year = sc.nextInt();
16+
17+
if(year % 400 == 0 && year % 100 == 0 || year % 100 != 0 && year % 4 == 0)
18+
System.out.println("Leap Year");
19+
else
20+
System.out.println("Not a leap year");
21+
}
22+
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class PowerOfTwo {
6+
7+
public static void main(String[] args) {
8+
// This program takes a command-line argument N and prints a table of the
9+
//powers of 2 that are less than or equal to 2^N.
10+
Scanner sc = new Scanner(System.in);
11+
System.out.println("Enter the value for N : ");
12+
int number = sc.nextInt();
13+
14+
System.out.println("power of 2^" + number + " is :"+ (Math.pow(2, number)));
15+
16+
17+
}
18+
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class Quadratic {
6+
7+
/*
8+
* find the roots of the equation a*x*x + b*x + c. Since the equation is x*x,
9+
* hence there are 2 roots. The 2 roots of the equation can be found using a
10+
* formula (Note: Take a, b and c as input values) delta = b*b - 4*a*c Root 1 of
11+
* x = (-b + sqrt(delta))/(2*a) Root 2 of x = (-b - sqrt(delta))/(2*a)
12+
*/
13+
public static void main(String[] args) {
14+
15+
Scanner sc = new Scanner(System.in); // scanner input
16+
//taking value for a , b & c
17+
System.out.println("Enter value for a : ");
18+
int a = sc.nextInt();
19+
20+
System.out.println("Enter value for b : ");
21+
int b = sc.nextInt();
22+
23+
System.out.println("Enter value for c : ");
24+
int c = sc.nextInt();
25+
26+
sc.close(); // closing scanner
27+
28+
int delta = (b * b) - 4 * (a * c); // formula for delta
29+
int root1 = (-b + Math.abs(delta)) / (2 * a); // formula to find the root1 value
30+
int root2 = (-b - Math.abs(delta)) / (2 * a); // formula to find the root12 value
31+
32+
System.out.println("Root 1 of X1 :" + root1); // printing the value of root1
33+
System.out.println("Root 2 of X2 : " + root2); // printing the value of root1=2
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class QuotientAndRemainder {
6+
//Java Program to Compute Quotient and Remainder
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in); //scanner input
10+
//taking divident and divisor as user input using scanner class
11+
System.out.println("enter divident : ");
12+
int divident = sc.nextInt(); // storing dividentt value
13+
14+
System.out.println("enter divisor : ");
15+
int divisor = sc.nextInt(); // storing divisor value
16+
17+
int quotient = divident / divisor; // / this give value for quotient
18+
int remainder = divident % divisor; // % this will give value for reminder
19+
20+
System.out.println("Quotient is : " + quotient);
21+
System.out.println("Remainder is : " + remainder);
22+
23+
}
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class SwapingTwoNumber {
6+
7+
public static void main(String[] args) {
8+
//Java Program to Swap Two Numbers
9+
Scanner sc = new Scanner(System.in); //scanner input
10+
System.out.println("Enter first number : ");
11+
int firstNumber = sc.nextInt();
12+
13+
System.out.println("Enter second number : ");
14+
int secondNumber = sc.nextInt();
15+
16+
int temp = firstNumber;
17+
firstNumber = secondNumber;
18+
secondNumber = temp;
19+
20+
System.out.println("after swapping number will be : ");
21+
System.out.println("first number is : " + firstNumber);
22+
System.out.println("second number is : " + secondNumber);
23+
24+
}
25+
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bridgelabz.basiccore;
2+
3+
import java.util.Scanner;
4+
5+
public class VowelConsonant {
6+
7+
public static void main(String[] args) {
8+
// Java Program to Check Whether an Alphabet is Vowel or Consonant
9+
Scanner sc = new Scanner(System.in); // scanner input
10+
System.out.println("Enter any character : ");
11+
char ch = sc.next().charAt(0); // storing char value
12+
13+
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
14+
System.out.println("vowel : " + ch);
15+
else
16+
System.out.println("consonant : " + ch);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)