Saturday, 19 September 2015

Factorial of Number

    package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Fact {
public static void main(String[] args) throws NumberFormatException, IOException {
 System.out.println("Enter a Number:");
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int n=Integer.parseInt(br.readLine());
 
 int f=fact(n);
 System.out.println(f);
}

public static int fact(int n){
 if(n==0)
  return 1;
 else
  return fact(n-1)*n;
}
}

  

Array-1

Enter a Length of array:
5
2
1
5
0
6
Array Element to Number:21506
Enter a value to add number:
4
Array value after adding:
[2][1][5][1][0]
===================
package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ElementToNumber {
 public static void main(String[] args) throws IOException {
  int array[];
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a Length of array:");
  int n=Integer.parseInt(br.readLine());
  array=new int[n];
  for(int i=0;i=0;i--){
   System.out.print("["+res[i]+"]");
  }
  
 }
}

Flyod's Algorithm


23 
456 

78910 
====================
  
  public static void main(String[] args) {
    int n=10,k=1;
    for(int i=1;i<=n;i++){
      for(int j=2;j<=i;j++){
        System.out.print(k);
        k++;
      }
      System.out.println(" ");
    }
  }

Sum of integers in the string

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SumOfNumberInString {
 public static void main(String[] args) throws IOException {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a String:");
  String s=br.readLine();
  removeChar(s);
}
 
 public static void removeChar(String s){
  StringBuffer res=new StringBuffer();
  for(int i=0;i='0'&&s.charAt(i)<='9')){
    res.append(s.charAt(i));
   }
  }
  
  int n=Integer.parseInt(res.toString()),r=0,sum=0;
  
  while(n!=0){
   r=n%10;
   sum+=r;
   n=n/10;
  }
  System.out.println("Sum of  numbers in the string:"+sum);
 }
}

Find String Length without using length()

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringLength {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String:");
String s=br.readLine();

int i=0;

try{
while(true){
s.charAt(i);
i++;
}
}catch(Exception e){
System.out.println(i);
}
}

}

Second Largest element in the array

Enter a Array Size:
6
Enter a Array Value:
2
5
3
8
2
6
Descending order:
8
6
5
3
2
Second Largest Element:6

======================


package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;

public class SecondLargest {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Array Size:");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
int newArray[]=new int[n];

System.out.println("Enter a Array Value:");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(br.readLine());
}
HashSet hs=new HashSet();
for(int i=0;i<n;i++){
hs.add(a[i]);
}
newArray=new int[hs.size()];
Iterator it=hs.iterator();
int k=0;
while(it.hasNext()){
newArray[k++]=(int) it.next();
}

for(int i=0;i<hs.size();i++){
for(int j=i+1;j<hs.size();j++)
if(newArray[i]<newArray[j]){
int t=newArray[i];
newArray[i]=newArray[j];
newArray[j]=t;
}
}
System.out.println("Descending order:");
for(int i=0;i<newArray.length;i++){
System.out.println(newArray[i]);
}
System.out.println("Second Largest Element:"+newArray[1]);
}
}

String-3

Enter a String:
Hi Iam Nataraja B
Reverse String:B ajarataN maI iH

Reversed word of string:B Nataraja Iam Hi

=======================================

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReverseWord {
public static void main(String[] args) throws IOException {
System.out.println("Enter a String:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
StringBuffer sb=new StringBuffer();
StringBuffer res=new StringBuffer();
s=reverseString(s);
System.out.println("Reverse String:"+s);
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '&& i!=s.length()){
sb.append(s.charAt(i));
}else{

res.append(reverseWord(sb).toString()+" ");
sb=new StringBuffer();

}
}
res.append(reverseWord(sb).toString());
System.out.println("Reversed word of string:"+res.toString());
}

public static String reverseWord(StringBuffer sbf){
String s="";
for(int i=sbf.length()-1;i>=0;i--){
s+=sbf.charAt(i);
}
return s;
}

public static String reverseString(String sbf){
StringBuffer rev=new StringBuffer(sbf);
rev.reverse();
return rev.toString();
}
}

Reverse String without in built method

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReverseString {

public static void main(String[] args) throws IOException {
System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
StringBuffer sb=new StringBuffer();
for(int i=s.length()-1;i>=0;i--){
sb.append(s.charAt(i));
}
System.out.println(sb);
}
}

String Example-1

Enter a String:
Nataraja123b? {b"
String without number & special character:Natarajabb
String without number:Natarajab? {b"

String without character:123

===========================================

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RemoveNumber {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String:");
String s=br.readLine();
removeNumberSpecialChar(s);
removeNumber(s);
removeChar(s);
}

public static void removeNumberSpecialChar(String s){
StringBuffer res=new StringBuffer();
for(int i=0;i<s.length();i++){
if(s.charAt(i)>='a'&&s.charAt(i)<='z'||s.charAt(i)>='A'&&s.charAt(i)<='Z'){
res.append(s.charAt(i));
}
}
System.out.println("String without number & special character:"+res.toString());
}

public static void removeNumber(String s){
StringBuffer res=new StringBuffer();
for(int i=0;i<s.length();i++){
if(!(s.charAt(i)>='0'&&s.charAt(i)<='9')){
res.append(s.charAt(i));
}
}
System.out.println("String without number:"+res.toString());
}

public static void removeChar(String s){
StringBuffer res=new StringBuffer();
for(int i=0;i<s.length();i++){
if((s.charAt(i)>='0'&&s.charAt(i)<='9')){
res.append(s.charAt(i));
}
}
System.out.println("String without character:"+res.toString());
}
}

Find Reaminder of Number without using "%" operator



package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Remainder {

public static void main(String[] args) throws NumberFormatException, IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number:");
int n=Integer.parseInt(br.readLine());
System.out.println("Enter a devidend");
int m=Integer.parseInt(br.readLine());
System.out.println("Remainder:"+remainder(m,n));
}

public static int remainder(int m,int n){
int res=0; 
for(int i=1;i<=n/m;i++){
res=i*m;
}
return n-res;
}
}

Pattern-3

Write a Program to display below Pattern?

Enter a Number:
5
***** 
 **** 
  *** 
   ** 
    *

====================================

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pattern3 {
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j>=i)
System.out.print("*");
else
System.out.print("  ");
}
System.out.println(" ");
}
}
}


Remove Duplicate Elements from Array


Enter a Array Size:
5
Enter a Array Value:
2
1
3
2
3
Array without Duplicate Elements
1
2
3

=======================

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class ArrayDuplicate {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Array Size:");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];

System.out.println("Enter a Array Value:");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(br.readLine());
}
HashSet hs=new HashSet();
for(int i=0;i<a.length;i++){
hs.add(a[i]);
}

System.out.println("Array without Duplicate Elements");
Iterator<E> it=hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}

}

Count number of time Digit Repeats

Count the Number of  time the Digit Repeats

Example:


from 1 to 20 the digit 2 repeats 11 times..


==============================


package com.nataraja.b;


import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class CountDigitRepeat {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number:");
int n=Integer.parseInt(br.readLine());
System.out.println("Enter a Limit:");
int m=Integer.parseInt(br.readLine());
int c=findDigit(n,m);
System.out.println("Count:"+c);
}

public static int findDigit(int n,int m){
int c=0;
for(int i=1;i<=m;i++){
int t=i,r;
while(t!=0){
r=t%10;
t=t/10;
if(r==n){
c++;
t=0;
}
}
}
return c;
}
}

Pattern-2

Mostly asked Programming Interview Questions:

Write a Program to display below Pattern?


**********
**            **
*  *        *  *
*    *    *    *
*      **      *
*      **      *
*    *    *    *
*  *        *  *
**            **
**********

=======================
package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pattern2 {

public static void main(String[] args) throws  IOException {
System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int  n;
try{
n=Integer.parseInt(br.readLine());
int t=n;
int k=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==1||j==n||i==1||i==n||t==j||j==k){
System.out.print("*");
}else{
System.out.print("  ");
}
}
t--;
k++;
System.out.println(" ");
}
}catch(Exception e){
//e.printStackTrace();
}

}
}

Pattern-1




Write a Program to display below Pattern?

****

***
**
*

===================


package com.nataraja.b;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pattern1 {


public static void main(String[] args) throws NumberFormatException, IOException {

System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}

Thursday, 23 July 2015

PHP 5 Tutorial


PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.



What is PHP?

  • PHP is an acronym for "PHP: Hypertext Preprocessor"
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use

What is a PHP File?

  • PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have extension ".php"

Why PHP?

  • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP supports a wide range of databases
  • PHP is free. Download it from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side

Classes and Objects

Class

Class can be defined as a template/ blueprint that describe the behaviors /states of a particular entity

A class is declared using class keyword. A class contain both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods

Rules for java Class

  • A class can have only public or default(no modifier) access specifier.
  • It can be either abstract, final or concrete (normal class).
  • A class can have extend only one one class/abstract class.
  • It must have the class keyword, and class must be followed by a legal identifier(name).
  • It may optionally extend one parent class. By default, it will extend java.lang.Object.
  • It may optionally implement any number of comma-separated interfaces.
  • The class's variables and methods are declared within a set of curly braces {}.
  • Each .java source file may contain only one public class. A source file may contain any number of default visible classes.
  • Finally, the source file name must match the public class name and it must have a .java suffix.

A simple class Example
Suppose, Student is a class and student's name, roll number, age will be its property. Lets see this in Java syntax
class Student.
{
 String name;
 int rollno;
 int age;
}

Object

Object is an instance of class. You may also call it as physical existence of a logical template class.
or
what you see in naked eye is called Object.

When a reference is made to a particular student with its property then it becomes an object, physical existence of Student class.
Student std=new Student();
After the above statement std is instance/object of Student class. Here the new keyword creates an actual physical copy of the object and assign it to the std variable. It will have physical existence and get memory in heap area. The new operator dynamically allocates memory for an object


Tuesday, 21 July 2015

Data Types in Java

Data Types in Java
Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.
In java, data types are classified into two catagories :
1.     Primitive Data type
2.     Non-Primitive Data type

1) Primitive Data type
A primitive data type can be of eight types :
1)Byte
2)Short
3)Int
4)Long
5)Float
6)Double
7)Char
8)Boolean


Once a primitive data type has been declared its type can never change, although in most cases its value can change. These eight primitive type can be put into four groups
Integer
This group includes byteshortintlong
byte : It is 8 bit integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;
short : It is 16 bit integer data type. Value range from -32768 to 32767. Default value zero. example:short s=11;
int : It is 32 bit integer data type. Value range from -2147483648 to 2147483647. Default value zero. example:int i=10;
long : It is 64 bit integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

Floating-Point Number
This group includes floatdouble
float : It is 32 bit float data type. Default value 0.0f. example: float ff=10.3f;
double : It is 64 bit float data type. Default value 0.0d. example: double db=11.123;

Characters
This group represent char, which represent symbols in a character set, like letters and numbers.
char : It is 16 bit unsigned unicode character. Range 0 to 65,535. example: char c='a';

Boolean
This group represent boolean, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;

2) Non-Primitive(Reference) Data type
A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change. We will talk a lot more about reference data type later in Classes and Object lesson.

Identifiers in Java
All Java components require names. Name used for classes, methods, interfaces and variables are calledIdentifier. Identifier must follow some rules. Here are the rules:
·         All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.
·         After the first character, an identifier can have any combination of characters.
·         A Java keyword cannot be used as an identifier.
·         Identifiers in Java are case sensitive, foo and Foo are two different identifiers.


First Java Program

First Java Program

Let us look at a simple java program.
class Hello
{
  public static void main(String[] args) 
  {
     System.out.println ("Hello World program");
  }
}
class : class kyword is used to declare classes in Java
public : It is an access specifier. Public means this function is visible to all.
static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.
void : It is the return type, meaning this function will not return anything.
main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.
System.out.println : This is used to print anything on the console like printf in C language.

Steps to Compile and Run your first Java program

Step 1: Open a text editor and write the code as above.
Step 2: Save the file as Hello.java
Step 3: Open command prompt and go to the directory where you saved your first java program assuming it is saved in C:\
Step 4: Type javac Hello.java and press Return to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line.
Step 5: Now type java Hello on command prompt to run your program.
Step 6: You will be able to see Hello world program printed on your command prompt.

Now let us see What happens at Runtime

After writing your Java program, when you will try to compile it. Compiler will perform some compilation operation on your program.
Once it is compiled successfully byte code(.class file) is generated by the compiler.
class-file at runtime in Java
After compiling when you will try to run the byte code(.class file), the following steps are performed at runtime:-
  1. Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
  2. Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
  3. Interpreter reads the byte code stream and then executes the instructions, step by step.