Java
Java
class Reverse
{
public static void main(String args[])
{
int i,s=0,a = 120;
int no[] = new int[10];
System.out.print("Original Number : "+a);
for(i=0;i< 10;i++)
{
if(a!= '\0')
{
no[i] = a%10;
a = a / 10;
s++;
}
else break;
}
System.out.print("\n\nReversed Number : ");
for(i=s;i>0;i--)
{
System.out.print(no[i]);
}
}
}
/************ OUTPUT ************
* Original Number : 120
* Reversed Number : 021 */
/* Function Overloading program in java. The program overloads the area() functi
on and calculates the area for square, rectangle and triangle */
class overloading
{
double area,root;
//Area Of Square
public void Area(double a)
{
area = a*a;
System.out.println("The Area Of Square : " +area);
}
// Area of rectangle
public void Area(double a,double b)
{
area= a*b;
System.out.println("The Area Of Rectangle : " +area);
}
// Aera Of Triangle
public void Area(double a,double b,double c)
{
double s= (a+b+c);
root = s*(s-a)*(s-b)*(s-c);
area = Math.sqrt(root);
System.out.println("The Area Of Triangle : " +area);
}
public static void main(String args[])
{
overloading ar = new overloading();
ar.Area(5.65);
ar.Area(5.32,43.2);
ar.Area(4,7,7);
}
}
/* Output
The Area Of Square : 31.922500000000003
The Area Of Rectangle : 229.82400000000004
The Area Of Triangle : 174.61958653026298 */
Program in java for inserting, recording, deleting, editing and searching studen
t details stored in the SQL database.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
class studentdetails
{
Statement stmt;
String strSql;
Connection con;
BufferedReader bufferObj;
studentdetails (){
bufferObj=new BufferedReader(new InputStreamReader(System.in));
}
void establishConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
try
{
String url="jdbc:odbc:new_dsn";
con=DriverManager.getConnection(url,"sa","");
stmt=con.createStatement();
}
catch(SQLException ce)
{
System.out.println("Error... "+ce);
}
}
void add()
{
try
{
System.out.println("\nEnter Roll Number: ");
String rollno=bufferObj.readLine();
System.out.println("\nEnter Students Name: ");
String name=bufferObj.readLine();
System.out.println("\nEnter Course Name");
String course=bufferObj.readLine();
strSql="Insert into Student values(" +rollno+",'"+name+"',' "+course+" ')";
stmt.executeUpdate(strSql);
System.out.println("\nRecords Successfully Added!");
System.out.println();
}
catch(SQLException ce)
{
System.out.println("Error... "+ce);
}
catch(Exception e)
{
System.out.println(e);
}
}
void update()
{
try
{
System.out.println("\nEnter Roll Number whose "+"record should be updated: ");
String roll=bufferObj.readLine();
System.out.println("Enter Students Name to be Modified: ");
String name=bufferObj.readLine();
System.out.println("Enter course name to be modified: ");
String course=bufferObj.readLine();
strSql="update Student set name=' "+name+" ',course='"+course+"' where rollno='"
+roll+"'";
stmt.executeUpdate(strSql);
System.out.println("\nRecord Successfully Modified !");
System.out.println();
}
catch(SQLException ioe)
{
System.out.println(ioe);
}
catch(Exception er)
{
System.out.println(er);
}
}
void delete()
{
try
{
System.out.println("Enter Students Name to be Deleted :") ;
String name=bufferObj.readLine();
strSql="Delete from Student where rtrim(name) like '"+name+ "'";
stmt.executeUpdate(strSql);
System.out.println("Record Successfully Deleted !");
System.out.println();
}
catch(SQLException et)
{
System.out.println("Error in Deletion.... "+et);
}
catch(Exception ty)
{
System.out.println("Error.... "+ty);
}
}
void search()
{
try
{
System.out.println("Enter Roll Number "+"whose record should be searched: ");
int roll=Integer.parseInt(bufferObj.readLine());
System.out.println("Enter Students Name to be Searched");
String name=bufferObj.readLine();
strSql="select * from student where name like'"+name;
strSql=strSql+"' and Rollno="+roll;
ResultSet rs=stmt.executeQuery(strSql);
if(!rs.next())
{
System.out.println("Name");
System.out.println(rs.getString(1)+"\t");
System.out.println("Roll Number");
System.out.println(rs.getInt(2)+"\t");
System.out.println("Course");
System.out.println(rs.getString(3)+"\n");
}
}
catch(SQLException er)
{
System.out.println(er);
}
catch(Exception t)
{
System.out.println(t);
}
}
public void menudisplay() throws IOException
{
char choice;
while(true)
{
System.out.println();
System.out.println("1. Add a Record");
System.out.println("2. Modify Record");
System.out.println("3. Delete a Record");
System.out.println("4. Search a Record");
System.out.println("5. Exit");
System.out.println("Enter your choice....:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
choice=(char) br.read();
switch(choice)
{
case '1' :
System.out.println("Adding a Record........");
add();
break;
case '2' :
System.out.println("Updating a Record........");
update();
break;
case '3' :
System.out.println("Deleting a Record........");
delete();
break;
case '4' :
System.out.println("Searching a Record........");
search();
break;
case '5' :
System.exit(0);
break;
default :
System.out.println("Adding a Record........");
add();
break;
}
}
}
}
class student
{
public static void main(String[] args) throws IOException
{
studentdetails stud = new studentdetails();
stud.establishConnection();
stud.menudisplay();
}
}
/*Write a program to find SUM AND PRODUCT of a given Digit. */
class Sum_Product_ofDigit{
public static void main(String args[]){
int num = Integer.parseInt(args[0]); //taking value as comma
nd line argument.
int temp = num,result=0;
//Logic for sum of digit
while(temp>0){
result = result + temp;
temp--;
}
System.out.println("Sum of Digit for "+num+" is : "+result);
//Logic for product of digit
temp = num;
result = 1;
while(temp > 0){
result = result * temp;
temp--;
}
System.out.println("Product of Digit for "+num+" is : "+result);
}
}
import java.io.*;
public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num);
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}