Professional Documents
Culture Documents
com/category/java-examples/
1)to check Vowel or Consonant using Switch Case
public class CaseConsAndVoc {
//insert number
Scanner scan=new Scanner(System.in);
System.out.println("Insert number");
scan.hasNextInt();
scan.close();
if(IsPal(string))
System.out.println(string+" Is palindrom");
else
System.out.println(string + "Is not palindrom");
}
==============================
4)public class EvenAndOdd {
num=scan.nextInt();
if(num%2==0)
System.out.println(num + "It is a odd numebr");
else
System.out.println(num + "It is a n even
number");
}
============================================
5)How To Convert Char To String and a String to char in Java
class CharToStringDemo
public class StringToArray {
}
System.out.println("Numbers of Vowels: " + vcount);
System.out.println("Number of Consonants " + ccount);
}
============================
8)reverse words in a String
import java.util.Scanner;
System.out.println("Random no : ");
for(int i=0;i<5;i++)
System.out.println(rnum.nextInt(200));
}
==============================
10) print alternate prime numbers
public class PrimeNumbers {
// int i=1;
// while(i<=count)
// {
// System.out.println(num1 + "");
// int sumPrev=num1+num2;
// num1=num2;
// num2=sumPrev;
// i++;
// }
System.out.println("another aproach");
for(int i1=1;i1<=count;i1++)
{
System.out.println(num1 + " ");
int sumPrev=num1+num2;
num1=num2;
num2=sumPrev;
}
}
}
13)reverse the Array
public class array {
=========
public class reversetheArray {
/*
* This loop stores all the elements that we enter in an the
array number. First
* element is at number[0], second at number[1] and so on
*/
for (i = 0; i < counter; i++) {
System.out.print("Enter Array Element" + (i + 1) + ":
");
number[i] = scanner.nextInt();
}
/*
* Here we are writing the logic to swap first element with last
element, second
* last element with second element and so on. On the first
iteration of while
* loop i is the index of first element and j is the index of
last. On the
* second iteration i is the index of second and j is the index
of second last.
*/
j = i - 1;
i = 0;
scanner.close();
while (i < j) {
temp = number[i];
number[i] = number[j];
number[j] = temp;
i++;
j--;
}
int num1,num2,num3,result,temp;
/* Scanner is used for getting user input.
* The nextInt() method of scanner reads the
* integer entered by user.
*/
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);
}
=============
15)
class SortByteArray {
// Selective Sorting
/* public static void sort(byte[] a, int fromIndex,
* int toIndex): Sorts the specified range of the
* array into ascending order. The range to be sorted
* extends from the index fromIndex, inclusive, to the
* index toIndex, exclusive. If fromIndex == toIndex,
* the range to be sorted is empty.
*/
Arrays.sort(byteArray2, 2, 5);
class BubbleSortExample {
public static void main(String []args) {
int num, i, j, temp;
Scanner input = new Scanner(System.in);
19)Linear Search
* Program: Linear Search Example
* Written by: Chaitanya from beginnersbook.com
* Input: Number of elements, element's values, value to be searched
* Output:Position of the number input by user among other numbers*/
import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int counter, num, item, array[];
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
//Creating array to store the all the numbers
array = new int[num];
System.out.println("Enter " + num + " integers");
//Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
//Create a HashMap
Map<Character, Integer> map = new HashMap<Character, Integer>();
System.out.println("\nString: ChaitanyaSingh");
System.out.println("-------------------------");
obj.countDupChars("ChaitanyaSingh");
System.out.println("\nString: #@$@!#$%!!%@");
System.out.println("-------------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}
===================
22)FindChString
static void countEachChar(String str)
{
//ASCII values ranges upto 256
int counter[] = new int[256];
//String length
int len = str.length();
if (flag == 1)
System.out.println("Occurrence of char " + str.charAt(i)
+ " in the String is:" + counter[str.charAt(i)]);
}
}
public static void main(String[] args)
{
String str = "beginnersbook";
countEachChar(str);
}
}
==============================