Java Programs1
Java Programs1
Java Programs1
}
}
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "Geeks for Geeks";
class GFG
{
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
4. Power of a number
if(a.equals(b)){
System.out.println("Both strings are equal.");
} else {
System.out.println("Both strings are not equal.");
}
if(a.equalsIgnoreCase(b)){
System.out.println("Both strings are equal.");
} else {
System.out.println("Both strings are not equal.");
}
}
}
6. import java.util.Date;
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else{
System.out.println("Date1 is equal to Date2");
}
}
}
7. To count number of words in a sentence
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] a=s.split("\\s+");
int l=a.length;
System.out.println(l);
}
}
Import java.util.scanner.*;
Public class Transpose
{
Public static void main(String args[])
{
Int I,j;
System.out.println(“Enter rows and columns”);
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}
Temp = Number;
while( Temp > 0)
{
Factorial = 1;
i = 1;
Reminder = Temp % 10;
while (i <= Reminder)
{
Factorial = Factorial * i;
i++;
}
System.out.println(" The Factorial of " + Reminder + " = " + Factorial);
Sum = Sum + Factorial;
Temp = Temp /10;
}
if ( Number == Sum )
{
System.out.println("\n " + Number + " is a Strong Number");
}
else
{
System.out.println("\n " + Number + " is Not a Strong Number");
}
}
}
14. Factorial
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
19. Read two String user input and check if first contains second in a java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First String:");
String s1 = scanner.nextLine();
System.out.println("Enter Second String:");
String s2 = scanner.nextLine();
scanner.close();
boolean result = stringContainsSubstring(s1, s2);
System.out.println(s1+" contains "+s2+" = "+result);
}
private static boolean stringContainsSubstring(String string, String substring) {
boolean result = false;
result = string.contains(substring);
return result;
}
}
21. write a program to print the odd and even places numbers in an array
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Odd numbers:");
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 != 0)
{
System.out.print(a[i]+" ");
}
}
System.out.println("");
System.out.print("Even numbers:");
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i]+" ");
}
}
}
}
import java.util.*;
class Main
{
static boolean checkSpy(int input)
{
int digit, sum = 0, product = 1;
while (input > 0)
{
digit = input % 10;
sum = sum + digit;
product = product * digit;
input = input / 10;
}
if (sum == product)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (checkSpy(input))
System.out.println("The number is "+ "a Spy number");
else
System.out.println("The number is "+ "NOT a Spy number");
}
}
29. Palindrome
int n=454;
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
if(temp==rev)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
Method 1:
Method 2:
else
System.out.println("No");
31.Prime number
int num=29;
boolean flag=false;
if( num%i == 0)
flag=true;
break;
}
}
if( !flag )
System.out.println(“prime number”);
else
else
if(i % j == 0)
{
flag = 0;
break;
Else
flag = 1;
if(flag == 1)
System.out.println(i);
For loop
if (number % i == 0) {
While loop:
i = 1;
if(Number % i == 0) {
i++;
35. How To Remove White Spaces From String In Java Using Built-In Methods?
}
}
36. Find All Pairs Of Elements In An Array Whose Sum Is Equal To A Given Number
do
{
t = squareRoot;
squareRoot = (t + (number / t)) / 2;
} while ((t - squareRoot) != 0);
return squareRoot;
}
first = 0;
last = n - 1;
middle = (first + last)/2;
int count=0;
if(arrayOne.length == arrayTwo.length)
{
for (int i = 0; i < arrayOne.length; i++)
{
if(arrayOne[i] != arrayTwo[i])
equalOrNot = false;
}
}
else
equalOrNot = false;
if (equalOrNot)
else
43. Write a program to swap two numbers without using third variable?
import java.util.*;
class SwapTwoNumbers{
public static void main(int args[]){
int a1,a2;
Scanner sc=new Scanner(System.in);
System.out.println(“ enter two numbers”);
a1=sc.nextInt();
a2=sc.nextInt();
System.out.println(“ before swapping values of a1 and a2 is:”+a1” ”+a2);
a1=a1+a2;
a2=a1-a2;
a1=a1-a2;
System.out.println(“ after swapping values of a1 and a2”+a1+” ”+a2);
}
}
44.Write a program to remove a specified character from a string?
Import java.util.ArrayList;
Import java.util.List;
class RemoveCharFromString{
public static void main(String args[]){
class Anagrams {
if (n1 != n2)
return false;
else{
Arrays.sort(str1);
Arrays.sort(str2);
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
else
return true;
}
}
}
48.Write a program for reverse of a string without using predefined functions?
import java.util.*;
class Reverse{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String s,rev=” ”;
s=sc.nextLine();
int n=s.length();
for(i=n;i>0;i--){
rev=rev+s.charAt(i);
}
System.out.println(“reverse of ”+s+” is”+rev);
}
}
System.out.println(Arrays.toString(result));
}
}
class BinarySearchExample1{
public static int binarySearch(int arr[], int first, int last, int key){
if (last>=first){
int mid = first + (last - first)/2;
if (arr[mid] == key){
return mid;
}
if (arr[mid] > key){
return binarySearch(arr, first, mid-1, key);//search in left subarray
}else{
return binarySearch(arr, mid+1, last, key);//search in right subarray
}
}
return -1;
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
int result = binarySearch(arr,0,last,key);
if (result == -1)
System.out.println("Element is not found!");
else
System.out.println("Element is found at index: "+result);
}
}
55. Program to perform operations on a tree set
TreeSet.add("A");
TreeSet.add("B");
TreeSet.add("C");
TreeSet.add("D");
TreeSet.add("E");
System.out.println(TreeSet);
TreeSet.remove("D");
while(itr.hasNext())
{
String value = itr.next();
class ExistorNot {
public static void main(String[] args)
{
if (ans)
System.out.println("The list contains 2");
else
System.out.println("The list does not contains 2");
}
}
class MaptoSet {
public static void main(String[] args)
{
58. Java program to find largest of three numbers using ternary operator?
Code:-
public class LargestNumber
{
public static void main(String[] args)
{
int num1, num2, num3, result, temp;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number:");
num1 = scanner.nextInt();
System.out.println("Enter Second Number:");
num2 = scanner.nextInt();
System.out.println("Enter Third Number:");
num3 = scanner.nextInt();
scanner.close();
temp = num1>num2 ? num1:num2;
result = num3>temp ? num3:temp;
System.out.println("Largest Number is:"+result);
}
}
59. Java program to check if given number is perfect square or not?
Code:-
import java.util.Scanner;
class PerfectSquare {
return i;
}
}
return -1;
}
public static void main(String[] args)
{
Int digit,number;
Scanner sc=new Scanner(System.in);
number=sc.nextInt();
digit=sc.nextInt();
System.out.println(getLLessThanN(number, digit));
}
}
68. Java program to check given number is niven/harshad number?
Code:-
public class NivenNumber {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();
int x = num, y, sum = 0;
while(x>0)
{
y = x%10;
sum = sum + y;
x = x/10;
}
if(num%sum == 0)
System.out.println(num+" is a Harshad Number.");
else
System.out.println(num+" is not a Harshad Number.");
}
}//353 is Harshad Number
* both will solve the problem, as long as both method are requesting lock
* in consistent order. */
synchronized (Integer.class) {
synchronized (String.class) {
synchronized (Integer.class) {
synchronized (String.class) {
#include<stdio.h>
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
void main ()
{
int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i;
mergeSort(a,0,9);
for(i=0;i<10;i++)
printf("\n%d\n",a[i]);
int mid;
if(beg<end)
mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
int temp[10];
{
if(a[i]<a[j])
temp[index] = a[i];
i = i+1;
else
temp[index] = a[j];
j = j+1;
index++;
if(i>mid)
while(j<=end)
temp[index] = a[j];
index++;
j++;
else
while(i<=mid)
temp[index] = a[i];
index++;
i++;
k = beg;
while(k<index)
a[k]=temp[k];
k++;
permutation("", input);
/*
*/
if (word.isEmpty()) {
System.err.println(perm + word);
} else {
+ word.substring(i + 1, word.length()));
Output:
123
132
213
231
312
321.
import java.util.regex.Pattern;
/**
*/
+ !pattern.matcher(input).matches());
+ digitPattern.matcher(number).matches());
Output:
class Node
int data;
data = item;
Node root;
/* Function to get the count of leaf nodes in a binary tree*/
int getLeafCount()
return getLeafCount(root);
if (node == null)
return 0;
return 1;
else
/* create a tree */
import java.util.Scanner;
/*
* input : [1, 2, 3, 4, 5]
* output: 3.0
*/
System.out
input[i] = scnr.nextInt();
scnr.close();
/**
* @param input
*/
}
Output
72. Write a program to find largest between three numbers using ternary operator?
import java.util.Scanner;
public class Largest_Ternary
{
public static void main(String[] args)
{
int a, b, c, d;
Scanner s = new Scanner(System.in);
System.out.println("Enter all three numbers:");
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
System.out.println("Largest Number:"+d);
}
}
1
24
369
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
*****
****
***
**
*
import java.util.Scanner;
public class Edureka
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
5555