Tuesday, October 6, 2015

Reverse number program in java

Reverse number Program is program which reverse the number given by user and if both the numbers are same reverse and given reverse .For example user enters 123 the reverse number is 321.and if user enters number 121 and if its reverse number is same 121, then the number is called palidrona.This program have static  reverse() method which invoke and return reverse intiger.

Example:-

Program

import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas*/
public class reverse // name of class
{

public static boolean Ispalidrona(int rev)// its method of boolean type
{
if (rev==reverse.reverse(rev))
{
System.out.println("number is palidrona ");
return true;
}
else

System.out.println("number is not  palidrona");
return false;


}
public static int reverse(int rev) /* method reverse of integer type with intiger argument and return integer value*/
       {
int result=0;
while(rev>0)
{
int num=rev%10;
rev=rev/10;

result= result*10+num;
}
return result;
}

public static void main(String[] args)
{
int a;// variable a takes user input in program
Scanner sc = new Scanner(System.in); // sc is object to the Scanner class
System.out.println("Enter the number you wanted to reverse ");
a=sc.nextInt();// nextInt() method is used accept intiger value from user

reverse(a); // passing a variable in reverse() method
Ispalidrona(reverse(a));// this method takes return value from reverse function

System.out.println(" reverse string is"+reverse(a));

}

}

Monday, October 5, 2015

linear search program in java

Linear search in java program is used to find a particular element in array with n number of values

Example 

Write a program to find a particular position of element in given dynamic array

Program 

import java.util.Scanner; / this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/public class linearsearch // name of class
{

public static void main(String[] args) {
int value,sizea,searchele, list[]; // list variable to array
Scanner sc = new Scanner(System.in); // sc is object to the Scanner class
System.out.println("enter number of elements you need to enter");
value=sc.nextInt(); // nextInt() method is used accept intiger value from user
list=new int[value];// array initilization
System.out.println("enter "+value+" elements ");

for(sizea=0;sizea
list[sizea]=sc.nextInt(); // adding value by user in array of size sizea-1
}
if(sizea==value)
System.out.println("enter the element you wanted to find");
searchele=sc.nextInt();// user enter element to be searched

for(sizea=0;sizea if(list[sizea]==searchele)// comparing  values in array with user entered element 
     {
System.out.println("this" +searchele+" is at position"+(sizea+1));
break;
}
}
if(sizea==value)
       {
System.out.println(searchele+" not in this array");
}
}

}

Output

java linear search output not found
linear search search

switch case in java

How to use switch case statement in java with example, switch case statement is used when there are multiple condition in java

Syntax

switch(Variable){
 case value:
     code statement;
  break;
 case value:
     code statement;
  break;
default:
     delault value not in above cases;
}

Example 
Write a program which allows user to enter a integer from todays day, and user get to know which day of week it will be after certain days (consider today as sunday,0 for sunday )Today is day 0.

Program

import java.util.Scanner;// this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class weekdays // name of class
{

public static void main(String args[])
{
int a,b;// a is variable to store summation of given numbers

System.out.println("Enter the number of days from today and will tell you which day will be after thpse days");
System.out.println("(consider today as sunday)");
Scanner sc = new Scanner(System.in);// sc is object to the Scanner class
a=sc.nextInt();// nextInt() method is used accept intiger value from user
b=a%7; // b stores value by dividing a by 7 to sort day

   switch(b)
     {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;

   }
 }
}

Output:

java switch case
Switch case java



do while in java

Do-while loop in jave is used to run a specific code statement for first time when you run the program and then next time will ask for condition.
syntex

do{
statement(s);
-----
}while(expression);

Example 
Write a program to get summation of values entered by programmer untill user enter's 0.(0 will terminate program)

import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class conditional // name of class
 {

public static void main(String[] args) 
{
float number,sum=0;// sum is variable to store summation of given numbers
System.out.println("enter the numbers you wan to sum");
System.out.println("enter 0 to terminate");
Scanner sc = new Scanner(System.in);
do {

number= sc.nextFloat();//number stores value from user
 sum =number+sum; // sum add numbers and return summation of all given numbers
}while(number!=0); // when user enters 0 , program termiates loop
 
System.out.println("Summation is "+sum);
}


}

Output
do while in java image not found
do while in java


Sunday, October 4, 2015

ASCII code of Alphabets in java

Write a java  Program that display ASCII code of  entered Character by user.

Program

import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class asciicode //asciicode is name of class
{

   public static void main(String args[])
     {

int x; // x is variable which stores value entered by user
Scanner sc= new Scanner(System.in);// sc is object to the Scanner class
System.out.println("Enter value you wan to see in ascii");
x=sc.nextInt();// nextInt() method is used accept intiger value from user
char c= (char) x ; // this conver character to ascii
System.out.println("ASCII code is "+c);


    }
}

Output:-
java ascii value of char image not displayed
Java Output

Saturday, October 3, 2015

Java Hello program.

              While writting a java program make sure you file name should be same as your Class name , and file name should be saved with extension .java
Java hello program is a basic program to represent to print Simple output in Java

Example
File name : Simpleprint.java
public class Simpleprint
{ //Simpleprint is name of class

public static void main(String args[])
{

 System.out.println("Hello Java");// this display output written in " "
                 System.out.println("Jai Shree krishna");

}



}

      To run Java file  open command prompt and first go to path of yor file then run .java file with command Javac after the type java and  class name in .java file and java code will excute
Output:
Java Hello image not found
output