THE BASIC JAVA PROGRAMS
THE BASIC JAVA PROGRAMS
import java.util.Scanner;
1
public class Calculator {
2
public static void main(String[] args) {
3 Scanner reader = new Scanner(System.in);
4 System.out.print("Enter two numbers: ");
15 case '-':
result = first - second;
16
break;
17
case '*':
18 result = first * second;
19 break;
20 case '/':
result = first / second;
21
break;
22
// operator doesn't match any case constant (+, -, *, /)
23
24
25 default:
System.out.printf("Error! operator is not correct");
26
return;
27
}
28 //printing the result of the operations
29 System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
30 }
}
31
32
33
34
35
36
37
38
When you execute the above program, the output looks like as shown below:
Factorial of a number is the product of all the positive numbers less than or equal
to the number. The factorial of a number n is denoted by n!
Now, let us write a program and find factorial of a number using recursion.
1 import java.util.Scanner;
public class Factorial {
2
public static void main(String args[]){
3
//Scanner object for capturing the user input
4 Scanner scanner = new Scanner(System.in);
5 System.out.println("Enter the number:");
6 //Stored the entered value in variable
int num = scanner.nextInt();
7
//Called the user defined function fact
8
int factorial = fact(num);
9 System.out.println("Factorial of entered number is: "+factorial);
10 }
11 static int fact(int n)
{
12
int output;
13
if(n==1){
14 return 1;
15 }
16 //Recursion: Function calling itself!!
output = fact(n-1)* n;
17
return output;
18
}
19 }
20
21
22
23
24
On executing the above program, you will get factorial of a number as shown
below:
It is a series in which the next term is the sum of the preceding two terms. For
Example: 0 1 1 2 3 5 8 13……. Let us write a Java program to calculate the
Fibonacci series.
1 Upto 100: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 +
A palindrome is a number, string or a sequence which will be the same even after
you reverse the order. For example, RACECAR, if spelled backward will be same
as RACECAR.
1 import java.util.Scanner;
2 public class Palindrome {
8 if(input.charAt(i) != input.charAt(length-i-1)) {
9 res = false;
break;
10
}
11
}
12 System.out.println(input + " is palindrome = "+res);
13 }
14 public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
15
System.out.print("Enter your Statement: ");
16
String str = sc.nextLine();
17 //function call
18 checkPalindrome(str);
19 }
}
20
21
22
23
24
When you run the code, it will check whether the given string is a palindrome or not
as shown below:
1 import java.util.Scanner;
2 public class nprandncr {
1 Enter Value of n : 5
2 Enter Value of r : 3
3 NCR = 10
NPR = 60
4
1
2
import java.util.Scanner;
3
public class PatternA {
4
// Java program to print alphabet A pattern
5 void display(int n)
6 {
7 // Outer for loop for number of lines
for (int i = 0; i<=n; i++) {
8
// Inner for loop for logic execution
9
for (int j = 0; j<= n / 2; j++) {
10 // prints two column lines
11 if ((j == 0 || j == n / 2) && i != 0 ||
12 // print first line of alphabet
13 i == 0 && j != n / 2 ||
// prints middle line
14
i == n / 2)
15
System.out.print("*");
16 else
17 System.out.print(" ");
18 }
System.out.println();
19
}
20
}
21 public static void main(String[] args)
22 {
23 Scanner sc = new Scanner(System.in);
PatternA a = new PatternA();
24
a.display(7);
25
}
26 }
27
28
29
30
Output:
3 {
public static void main(String args[])
4
{
5
int n, i, j, space = 1;
6 System.out.print("Enter the number of rows: ");
7 Scanner s = new Scanner(System.in);
8 n = s.nextInt();
space = n - 1;
9
for (j = 1; j<= n; j++)
10
{
11 for (i = 1; i<= space; i++)
12 {
13 System.out.print(" ");
}
14
space--;
15
for (i = 1; i <= 2 * j - 1; i++)
16 {
17 System.out.print("*");
18 }
System.out.println("");
19
}
20
space = 1;
21 for (j = 1; j<= n - 1; j++)
22 {
1
2
public class stringreverse {
3
public static void main(String[] args) {
4 // TODO Auto-generated method stub
5 String str = "Welcome To Edureka";
6 String[] strArray = str.split(" ");
for (String temp: strArray){
7
System.out.println(temp);
8
}
9 for(int i=0; i<3; i++){ char[] s1 = strArray[i].toCharArray(); for (int j = s1.length-1; j>=0; j--)
10 {System.out.print(s1[j]);}
11 System.out.print(" ");
}
12
}
13
}
14
15
The output of the above program will be as shown below:
1 Welcome
2 To
3 Edureka
emocleW oT akerudE
4
An array is called mirror–inverse if its inverse is equal to itself. Let’s now write
a program and check whether the given array is mirror inverse or not.
//Java implementation of the approach
1
public class MirrorInverse {
2
// Function that returns true if
3 // the array is mirror-inverse
static boolean isMirrorInverse(int arr[])
4
{
5
for (int i = 0; i<arr.length; i++) {
6 // If condition fails for any element
7 if (arr[arr[i]] != i)
8 return false;
}
9
// Given array is mirror-inverse
10
return true;
11 }
12
13 public static void main(String[] args)
14 {
int arr[] = { 1, 2, 3, 0 };
15
if (isMirrorInverse(arr))
16
System.out.println("Yes");
17 else
18 System.out.println("No");
19 }
}
20
21
22
23
24
25
Output: No
// If the given array was {3,4,2,0,1} then it would have printed yes as the output
because the array is mirror inverse.