Thursday, 21 July 2016

Modified CRUD in Servlets

A CRUD application is very important in any project developemt.
C- Create database
R- Read database
U- Update database
D- Delete database
It is an modified form because In this we are using the concept of cookies also.As cookies are saved in browser in one browser only one user can access it.To access application you have to login again.
Anyone can view record in table but if he wants to update,delete or insert data in database he has to first login.
step-1:
create database with table login,student with the following fields.
login-username,password
Student-name,age,company,uniqueid

step-2:
index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
              <title>index page</title>
</head>
<body>
             <form action="Register" method="post">
             <input type="submit" value="showalldata" name="i"/>
             <input type="submit" value="login" name="i"/>
             </form>
             <form action="operation.html" method="post">
             <input type="submit" value="access" name="i"/>
             </form>
</body>

</html>

login.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
          <title>MyHtml.html</title>
</head>
<body>
          <form action="Login" method="post">
          Enter UserName:<input type="text"  name="i1"/>
          <br/>
          <br/>
          Enter PassWord:<input type="password"  name="i2"/>
          <br/>
          <br/>
         <input type="submit" value ="submit"/>
 </form>
</body>
</html>

operation.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
          <title>MyHtml.html</title>
</head>
<body>
          <form action="Register" method="post">
          <table>
    <tr>
          <td>Name:</td>
          <td><input type="text" name="name"/></td>
   </tr>
    <tr>
           <td>Age:</td>
           <td><input type="text" name="age"/></td>
   </tr>
   <tr>
           <td>CompanyName:</td>
          <td><input type="text" name="companyname"/></td>
  </tr>
  <tr>
            <td>UniqueNo:</td>
            <td><input type="text" name="sid"/></td>
   </tr>
   </table>
   <br>
   <br/>
           <input type="submit" value="insert" name="i"/>
           <input type="submit" value="update" name="i"/>
           <input type="submit" value="delete" name="i"/>
           <input type="submit" value="showalldata" name="i"/>
</form>
</body>

</html>

step-3:

Login.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.DriverManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 5211745231183573478L;

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("i1");
String password=request.getParameter("i2");
PreparedStatement ps=Register.con.prepareStatement("select * from login where UserName=? and PassWord=?");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rst=ps.executeQuery();

if(rst.next())
{

Cookie ck=new Cookie("Uname",username);
response.addCookie(ck);
RequestDispatcher rd=request.getRequestDispatcher("operation.html");
rd.forward(request,response);
//Register.access=true;
}
else
{
out.print("<h3>wrong username or password</h3>");
RequestDispatcher rd=request.getRequestDispatcher("login.html");
rd.include(request,response);
}

out.close();

}
catch(Exception e)
{
System.out.println("Error Occured : " + e.getMessage());
}

}


}

Register.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
//public static boolean access = false;

/**
* @see HttpServlet#HttpServlet()
*/
public Register() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
public static Connection con;
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String age = request.getParameter("age");
String company = request.getParameter("companyname");
String uniqueid = request.getParameter("sid");
String buttonPressed = request.getParameter("i");
//System.out.println("value of button : " + buttonPressed);
Cookie ck[]=request.getCookies();
try {

// loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");

// creating connection with the database
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "root");
if (buttonPressed.equals("insert")) {
if (ck==null)
{
//System.out.println("my name is khan");
out.print("<h3>please login first:</h3>");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
//response.sendRedirect("index.html");
//JOptionPane.showMessageDialog(this,,1);
//JOptionPane.showMessageDialog(null, "please login first : ", null, 0);
//request.setAttribute("errorMessage", "Please submit an amount of at least 1");
//response.sendRedirect("http://localhost:8080/LoginLogout/index.html");
}
else {
//request.getSession().removeAttribute("errorMessage");
PreparedStatement ps = con.prepareStatement("insert into Student values(?,?,?,?)");
ps.setString(1, name);
ps.setString(2, age);
ps.setString(3, company);
ps.setString(4, uniqueid);
int i = ps.executeUpdate();

if (i > 0) {
out.println("You are sucessfully inserted");
}
}
} else if (buttonPressed.equals("update"))
{
// String query = "update student set name= '"+name+"'";
if (ck==null)
{
//out.print("please login first:");
//JOptionPane.showMessageDialog(null, "please login first : ", null, 0);
//request.setAttribute("errorMessage", "Please submit an amount of at least 1");
out.print("<h3>please login first:</h3>");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);

//response.sendRedirect("http://localhost:8080/LoginLogout/index.html");
}
else
{
System.out.println("i am updated:");
PreparedStatement ps = con.prepareStatement("update student set name=?,age=?,company=? where sid=?");
ps.setString(1, name);
ps.setString(2, age);
ps.setString(3, company);
ps.setString(4, uniqueid);
int i = ps.executeUpdate();

if (i > 0) {
out.println("You are sucessfully updated");
}
}
}
else if (buttonPressed.equals("delete"))
{
//System.out.println("access value is :"+access);
if (ck==null)
{
out.print("<h3>please login first:</h3>");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);

//System.out.println("i am in delete button:");
//out.print("please login first:");
//JOptionPane.showMessageDialog(null, "please login first : ", null, 0);
//response.sendRedirect("http://localhost:8080/LoginLogout/index.html");
}
else {
String query = "DELETE FROM student WHERE sid = '"
+ uniqueid + "'";
Statement st = con.createStatement();
int i=st.executeUpdate(query);
if(i>0)
out.print("you have successfully deleted");
}

}
else if (buttonPressed.equals("showalldata")) {

// PreparedStatement ps=con.prepareStatement("");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
out.print("<table border=1>");
out.print("<tr>");
out.print("<td>Name: </td>");
out.print("<td>Age: </td>");
out.print("<td>Company: </td>");
out.print("<td>Unique Id: </td>");
out.print("</tr>");

while (rs.next()) {
// Retrieve by column name
name = rs.getString(1);
age = rs.getString(2);
company = rs.getString(3);
uniqueid = rs.getString(4);

// Display values
out.print("<tr>");
out.print("<td>"+name+"</td>");
out.print("<td>"+age+"</td>");
out.print("<td>"+company+"</td>");
out.print("<td>"+uniqueid+"</td>");
out.print("</tr>");
out.print("<tr>");





}
out.print("</table>");
}
else if(buttonPressed.equals("login"))
{
//response.sendRedirect("http://localhost:8080/LoginLogout/MyHtml.html");
//response.sendRedirect("http://localhost:8080/LoginLogout/MyHtml.html");

RequestDispatcher rd=request.getRequestDispatcher("login.html");
rd.forward(request,response);
}
}
catch (Exception se) {
System.out.println("Error Occurred : " + se.getMessage());
}

}


}

Tuesday, 12 April 2016

Major Projects:-









Minor Projects:-

Digital Clock



import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
public class DigitalClock extends JFrame
{
JButton jb;
String timeupdated="";
DigitalClock()
{
jb=new JButton();
setSize(400,400);
add(jb);
setVisible(true);
while(true)
{
Date time=new Date();
SimpleDateFormat formatter=new SimpleDateFormat("hh:mm:ss");
timeupdated=formatter.format(time);
printable();
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

public void printable()
{
jb.setText(timeupdated);
}
public static void main(String args[])
{
DigitalClock dc=new DigitalClock();
}
}


Monday, 4 April 2016

hackerrank:Encryption

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n=s.length();
int row=(int)Math.floor(Math.sqrt(n));
int coloum=(int)Math.ceil(Math.sqrt(n));
char ch[]=s.toCharArray();
String s1="";
for(int i=0;i<coloum;i++)
{
int j=i;
while(j<n)
{
s1=s1+ch[j];
 j=j+coloum;
}
s1=s1+" ";
}
System.out.println(s1);
}
}

hackerrank:Modified Kaprekar Numbers

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static boolean checkKaprekar(long number)
{
long square=number*number;
String s=square+"";
//System.out.println(s);
long temp=Long.parseLong(s);
long last=0;
if(temp<10)
last=temp;
else
{
long s1=Long.parseLong(s.substring(0,s.length()/2));
//System.out.println(s1);
long s2=Long.parseLong(s.substring(s.length()/2,s.length()));
//System.out.println(s2);
last=s1+s2;
}
if(last==number)
return true;
 
return false;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
long p=sc.nextInt();
long q=sc.nextInt();
long temp=0;
boolean found=false;
for(long i=p;i<=q;i++)
{
found=checkKaprekar(i);
if(found==true)
{
temp=1;
System.out.print(i+" ");
}
}
if(found==false&&temp==0)
System.out.println("INVALID RANGE");
}
}

hackerrank: The Time in Words

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int h = in.nextInt();
int m = in.nextInt();
String s="";
int t=h;
if(m>30)
{
 t=h+1;
 m=60-m;
}
switch(m)
{
        case 1:
            s=s+"one minute";
            break;
        case 2:
            s=s+"two minute";
            break;
        case 3:
            s=s+"three minutes";
            break;
        case 4:
            s=s+"four minute";
            break;
        case 5:
            s=s+"five minute";
            break;
        case 6:
            s=s+"six minute";
            break;
        case 7:
            s=s+"seven minute";
            break;
        case 8:
            s=s+"eight minute";
            break;
        case 9:
            s=s+"nine minute";
            break;
        case 10:
            s=s+"ten minutes";
            break;
        case 11:
            s=s+"eleven minutes";
            break;
        case 12:
            s=s+"twelve minutes";
            break;
        case 13:
            s=s+"thirteen minutes";
            break;
        case 14:
            s=s+"fourteen minutes";
            break;
        case 16:
            s=s+"sixteen minutes";
            break;
        case 17:
            s=s+"seventeen minutes";
            break;
        case 18:
            s=s+"eighteen minutes";
            break;
        case 19:
            s=s+"nineteen minutes";
            break;
        case 20:
            s=s+"twenty minutes";
            break;
        case 21:
            s=s+"twenty one minutes";
            break;
        case 22:
            s=s+"twenty two minutes";;
            break;
        case 23:
            s=s+"twenty three minutes";;
            break;
        case 24:
            s=s+"twenty four minutes";
            break;
        case 25:
            s=s+"twenty five minutes";
            break;
        case 26:
            s=s+"twenty six minutes";
            break;
        case 27:
            s=s+"twenty seven minutes";
            break;
        case 28:
            s=s+"twenty eight minutes";
            break;
        case 29:
            s=s+"twenty nine minutes";
}
if(m==15)
   s=s+"quarter";
     
if(t==h && m!=0 && m!=30 && m!=45)
    s=s+" past ";
      if(t==h+1 && m!=0)
      s=s+" to ";  
  if(m==30)
      s=s+"half past ";
switch(t)
{
           case 1:
                s=s+"one ";
                break;
           case 2:
                s=s+"two ";
                break;
           case 3:
                s=s+"three ";
                break;
           case 4:
               s=s+"four ";
               break;
           case 5:
            s=s+"five ";
            break;
           case 6:
            s=s+"six ";
            break;
           case 7:
            s=s+"seven ";
            break;
           case 8:
            s=s+"eight ";
            break;
           case 9:
            s=s+"nine ";
            break;
           case 10:
            s=s+"ten ";
            break;
           case 11:
            s=s+"eleven ";
}
           if(m==0)
           s=s+"o' clock ";
         
     
     System.out.println(s);
}
}

hackerrank:Taum and B'day

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int i=0;i<t;i++)
        {
        int black = in.nextInt();
        int white = in.nextInt();
        int cost_black = in.nextInt();
        int cost_white = in.nextInt();
        int change_b_w = in.nextInt();
        long temp1=cost_black;
        long temp2=cost_white;
        if(cost_black>=cost_white)
        temp1=cost_black;
        else
        temp2=cost_white;  
        if(temp1+change_b_w<temp2)
            temp2=temp1+change_b_w;
        if(temp2+change_b_w<temp1)
            temp1=temp2+change_b_w;
         
        long cost=black*temp1+white*temp2;
        System.out.println(cost);      
     
}
}
}

hackerrank:Extra Long Factorials

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();  
BigInteger fact=BigInteger.ONE;
for(int i=n;i>=1;i--)
{
fact=fact.multiply(BigInteger.valueOf(i));  
}
System.out.println(fact);
}
}

hackerrank:Library Fine

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
        Scanner in = new Scanner(System.in);
        int d1 = in.nextInt();
        int m1 = in.nextInt();
        int y1 = in.nextInt();
        int d2 = in.nextInt();
        int m2 = in.nextInt();
        int y2 = in.nextInt();
 
         if(y1<y2)
             System.out.println("0");
         else if(y1==y2 && m1<m2)
             System.out.println("0");
         else if(y1==y2 && m1==m2 && d1<=d2)
             System.out.println("0");
         else if(d1>=d2 && m1==m2 && y1==y2)
             System.out.println(15*(d1-d2));
         else if(m1>m2 && y1==y2)
             System.out.println(500*(m1-m2));
         else
             System.out.println("10000");
}
}

hackerrank:Caesar Cipher

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String s=in.next();
        int k = in.nextInt();
        String s2="";
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)>=65 && s.charAt(i)<=90)
            {
            int temp=(int)(s.charAt(i))+k%26;
            if(temp<=90)
            s2=s2+(char)temp;
            else
            s2=s2+(char)(temp-26);  
            }
            else if (s.charAt(i)>=97 && s.charAt(i)<=122)
            {
             int temp=(int)(s.charAt(i))+k%26;
             if(temp<=122)
               s2=s2+(char)temp;
             else
               s2=s2+(char)(temp-26);
            }
            else
               s2=s2+s.charAt(i);
       }
        System.out.println(s2);
}
}

hackerrank:Cavity Map

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String grid[] = new String[n];
        for(int i=0;i<n;i++)
          grid[i]=in.next();
        for(int i=0;i<n;i++)
          {
            for(int j=0;j<grid[i].length();j++)
               {
                if(i==0||j==0||i==n-1||j==n-1)
                  System.out.print(grid[i].charAt(j));
                else if(grid[i].charAt(j)>grid[i].charAt(j+1)&&grid[i].charAt(j)>grid[i-1].charAt(j)&&grid[i].charAt(j)>grid[i+1].charAt(j)&&grid[i].charAt(j)>grid[i].charAt(j-1))
         System.out.print("X");
         else
         System.out.print(grid[i].charAt(j));
       
               }      
             System.out.println();
        }
         
}
}

hackerrank:The Grid Search

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
static int r,c,m,n;
static String arr[];
static String pattern[];
static void input(Scanner sc)
        {
        r=sc.nextInt();
        c=sc.nextInt();
        arr=new String[r];
        String s=sc.nextLine();
        for(int i=0;i<r;i++)
            {
                arr[i]=sc.nextLine();
            }
        m=sc.nextInt();
        n=sc.nextInt();
        pattern=new String[m];
        s=sc.nextLine();
        for(int i=0;i<m;i++)
            {
                pattern[i]=sc.nextLine();
            }
        int value=(int)pattern[0].charAt(0);
        boolean found=false;
        outer:
        for(int i=0;i<=r-m;i++)
        {
            for(int j=0;j<=c-n;j++)
            {
            if((int)(arr[i].charAt(j))==value)
            {
                found=check(i,j);
                if(found==true){
                    System.out.println("YES");
                    break outer;
            }
            }
        }
        }
        if(found==false)
            System.out.println("NO");
}
 
static boolean check(int m,int k)
{
for(int i=0,t=m;i<pattern.length && t<=m+pattern.length;i++,t++)
{
for(int j=0,l=k;j<pattern[i].length() && l<=k+pattern[i].length();j++,l++)
{
if(arr[t].charAt(l)==pattern[i].charAt(j))
continue;
else
return false;
}
}
return true;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
for(int i=0;i<test;i++)
{  
Solution g1=new Solution();
g1.input(sc);
}
}
}

hackerrank:Lisa's Workbook

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int max=sc.nextInt();
    int totalproblem[]=new int[n];
    for(int i=0;i<n;i++)
    totalproblem[i]=sc.nextInt();
    int special=0;int page=1;
    for(int chapter=0;chapter<n;chapter++)
    {
    if(chapter!=0)
    page++;
int problem=1;
    int k=1;
    inner: while(problem<=totalproblem[chapter]&&k<=max)
    {
    if(problem==page)
    {
    special++;
    problem++;
    k++;
    }
    else
    {
    problem++;
    k++;
    }
if(problem<=totalproblem[chapter]&&k>max)
{
page++;
k=1;
continue inner;
} 
}
}
 System.out.println(special);
}
}

hackerrank:Chocolate Feast

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++)
{
System.out.println(Solve(in.nextInt(), in.nextInt(), in.nextInt()));
}
}
private static long Solve(int N, int C, int M)
{
int toffes=N/C;
int paper=toffes;
int extra=0;
while(paper>0)
{
paper=paper-M;
if(paper>=0)
extra++;
if(paper>0)
paper=paper+1;
}  
return toffes+extra;
}
}

hackerrank:Cut the sticks

#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,min,count,m;
int a[1000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf(" %d",&a[i]);
count=n;
while(count>0)
{
for(i=0;i<n;i++)
{
if(a[i]!=0)
{
min=a[i];
goto next;
}
}

next:
for(i=0;i<n;i++)
{
if(a[i]<min&&a[i]!=0)
min=a[i];
}
count=0;
for(i=0;i<n;i++)
{
if(a[i]>0)
count++;
}
if(count==0)
exit(0);
printf("%d\n",count);
for(i=0;i<n;i++)
{

a[i]=a[i]-min;
if(a[i]<0)
a[i]=0;
}
}
return 0;
}

hackerrank:Service Lane

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int t = in.nextInt();
        int width[] = new int[n];
        for(int width_i=0; width_i < n; width_i++){
            width[width_i] = in.nextInt();
        }
        for(int a0 = 0; a0 < t; a0++){
            int i = in.nextInt();
            int j = in.nextInt();
            int min=width[i];
            for(int k=i+1;k<=j;k++)
            {
                if(width[k]<min)
                    min=width[k];
            }
            System.out.println(min);
                   
        }
    }
}

hackerrank:Sherlock and Squares

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
    public static void main(String[] args)
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc=new Scanner(System.in);
        int test=sc.nextInt();
        while(test>0)
        {
           int a=sc.nextInt();
           int b=sc.nextInt();
           int count=(int)(Math.floor(Math.sqrt(b))-Math.ceil(Math.sqrt(a)))+1;
         
            System.out.println(count);
            test--;
        }
    }
}

hackerrank:Find Digits

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
while(test>0)
{
int count=0;  
int n=sc.nextInt();
int arr[]=new int[12];
int i=0;
int temp=n;
while(n>0)
{
arr[i]=n%10;
n=n/10;
i++;
}
n=temp;
for(int k=0;k<i;k++)
{
if(arr[k]!=0)
{
if(n%arr[k]==0)
count++;
}
}
System.out.println(count);
test--;
}
}
}        

hackerrank:Sherlock and The Beast

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++)
        {
            int n = in.nextInt();
            int div=n%3;
            if(n%3==0)
            {
            for(int i=0;i<n;i++)
                System.out.print("5");
                System.out.println();
            }
            else if(div==1&&n>=10)
            {
                for(int i=0;i<n-10;i++)
                    System.out.print("5");
                for(int i=0;i<10;i++)
                    System.out.print("3");
                System.out.println();
            }  
            else if(div==2&&n>=5)
            {
                for(int i=0;i<n-5;i++)
                    System.out.print("5");
                for(int i=0;i<5;i++)
                    System.out.print("3");  
            System.out.println();
            }
            else if(n%5==0)
            {
                for(int i=0;i<n;i++)
                    System.out.print("3");
                System.out.println();
            }
            else
                System.out.println("-1");

            }  
         
}
}