Wednesday, January 22, 2020

Basic Java Programs for Beginners

1. Java Program to find weather a given number is prime or not

Code:

import java.util.Scanner;
public class prime {
public static void main(String args[])
{
int i,count=0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
int num = sc.nextInt();
        System.out.println(num);
if(num == 1)
{
System.out.print(num+" is Not Prime");
return;
}
else
{
for(i=2;i<num;i++)
{
if(num%2==0)
{
count++;
}
}
if(count>1)
{
System.out.print(num+" is Not Prime");
}
else
{
System.out.print(num+" prime number");
}
}
}

}

2. Binary Search

Code:

import java.util.Scanner;

public class binarySearch 
{
  @SuppressWarnings("resource")
  public static void main(String args[]) 
  {
Scanner sc = new Scanner(System.in);
int num,size,mid;
System.out.println("Enter Array size ");
size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter Array elements in ascending order ");
for (int i = 0; i < size; i++) 
{
          arr[i]=sc.nextInt();
}
System.out.println("Enter Search key ");
mid=size/2;
num = sc.nextInt();
for (int i = 0; i < size; i++) 
{
  for (int j = size-1; j >=mid+1; j--) 
  {
   if (num == arr[mid]) 
   {
    mid=arr[mid];
   }
   else
   {
//you try this☺
   }
}
     }
    System.out.println("Element "+num+" found in index "+mid);
  }
}

3. Java Program to Reverse a given String

Code:

import java.util.Scanner;
@SuppressWarnings("resource")
public class reverseString 
{
  public static void  main(String args[]) {
     Scanner sc = new Scanner(System.in);
  System.out.println("Enter a String");
  String str = sc.next();
  String rev = " ";
  for (int i = str.length()-1; i>=0; i--) 
  {
rev = rev+str.charAt(i);
  }
  System.out.println("Original String "+str+" Reversed String is "+rev);
  if(str.equalsIgnoreCase(rev))
  {
System.out.println("Palindrome ");
  }
  else
  {
   System.out.println("Not Palindrome");
  }
 }
}

4. Java Program to find Largest in an Array

Code:

public class largeInArray 
{
 public static void main(String args[]) 
 {
int arr[]= {1,5,0,-1,10,15,7};
int large=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i] > large)
{
large = arr[i];
}
}
System.out.println(large);
  }
}

5. Armstrong Number

Code:

public class armstrongNum 
{
 public static void main(String[] args)  
 {  
    int c=0,a,temp;  
    int n=153;//It is the number to check Armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");   
   }  
}

6. Program on static operations

class A
{
static int x;
int y;
A(int a,int b)
{
x=a+b;
y=x+b;
}
}
public class StaticOperations 
{
@SuppressWarnings("static-access")
public static void main(String args[]) 
{
A a=new A(2,3);
A a1=new A(5,10);
System.out.println(a.x+" "+a.y);
}

}



PHP Session for Login Logout with example

PHP Login Logout examples with Session

Before going to example you need to learn what is PHP session()


In this login logout example in PHP we used 3 file:

  • login.php
  • index.php
  • logout.php

login_user.sql

CREATE TABLE `login_user` (
  `id` int(11) NOT NULL,
  `name` varchar(60) NOT NULL,
  `user_name` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

login.php



<?php
session_start();
$message="";
if(count($_POST)>0) 
{ 
$con = mysqli_connect('127.0.0.1:3306','root','','admin') or die('Unable To connect');
$result = mysqli_query($con,"SELECT * FROM login_user WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
$row  = mysqli_fetch_array($result);
if(is_array($row)) 
{
$_SESSION["id"] = $row['id'];
$_SESSION["name"] = $row['name'];

else 
{
$message = "Invalid Username or Password!";
}
}
if(isset($_SESSION["id"])) 
{
header("Location:index.php");
}
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form name="frmUser" method="post" action="" align="center"><div class="message">
<?php
 if($message!="")
 {
 echo $message;
 }
 ?>
</div>
<h3 align="center">Enter Login Details</h3>
 Username:<br> <input type="text"name="user_name">
 <br>
 Password:<br><input type="password" name="password">
<br>
<br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
</form>
</body>
</html>

index.php

<?php
session_start();
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<?php
if($_SESSION["name"]) {
?>
Welcome <?php echo $_SESSION["name"]; ?>. Click here to <a href="logout.php" tite="Logout">Logout.
<?php
}else echo "<h1>Please login first .</h1>";
?>
</body>

</html>

logout.php


<?php
session_start();
unset($_SESSION["id"]);
unset($_SESSION["name"]);
header("Location:login.php");
?>


Friday, January 17, 2020

Parsing Excel File Using SimpleXLSX Part - 2



Parsing Excel File Using SimpleXLSX 

        In last post I have explained briefly about what is the good library for parsing Excel file in PHP. In this post I'm going to explain the next part of that, i.e how to parse the excel and how to read cells.

You need to follow some steps:
  • Download the SimpleXLSX library from github → Clicke Here to download 
  • Extract the zip and copy the folder into your project folder.
  • Create new php file to read the excel
  • include SimpleXLSX library into file using include or require ↓
  • Initialize the Excel File Handler Parse ↓

  • Now try with a simple example

Books.xlsx


example1.php
Output
   

Reading rows 

read_rows.php
Output




Tuesday, January 14, 2020

Parsing Excel File Using SimpleXLSX Part - 1


Parsing Microsoft Excel Files in PHP Easily

When developing a website, you sometimes have to manipulate data in Microsoft Excel format. For example, your website could offer users the option of importing an Excel file that you would parse to integrate its data it into your database, for example. This seemingly trivial task can be complicated. Fortunately, I will present you with a simple and powerful open source solution that will allow you to easily read Excel XLS and XLSX files in PHP.

Powerful And Simple Solution With SimpleXLSX

In this tutorial, I suggest you to parse your files in Microsoft Excel format using the open source solution SimpleXLSX. Solution is a very big word here since SimpleXLSX is a single PHP class without any dependency. To use it, you only have to import it into your PHP scripts.
SimpleXLSX does not need external dependencies because the class does the unzipping and parsing work internally for the Open XML format on which the XLSX files are based.
SimpleXLSX is under MIT license and can be downloaded freely from GitHub: https://github.com/shuchkin/simplexlsx
For parsing XLS files, the PHP code will be the same but you will have to use the SimpleXLS dedicated class provided by the developer of SimpleXLSX.
Parsing and Reading the Excel file will be explained in next post►

Follow me for more Technical Info
Thank You..