Monday, July 29, 2013

Java Program to Find the Sum of the Digits of a Number

Program to Find the Sum of the Digits of a Number
import java.io.*;
class Add
{
void number()
{
try
{
int num,sum=0;
System.out.println("Enter any number:");
DataInputStream dts=new DataInputStream(System.in);
int n=Integer.parseInt(dts.readLine());
while ( n > 0 )
{
num = n % 10;
n = n / 10;
sum = sum + num;
}
System.out.println("The sum of the digits is:"
+sum);
}
catch(Exception s)
{
System.out.println(s.getMessage());
}
}
}

class Digit_add
{
public static void main(String args[])
{
Add a = new Add();
a.number();
}
}


Saturday, July 27, 2013

java Program to Find Square Root of a Number

Program to Find Square Root of a Number
import java.lang.*;
import java.io.*;
class sqrt
{
public static void main(String a[])
{
try
{
DataInputStream dts=new DataInputStream(System.in) ;
System.out.println("enter number to find squaroot");
int m=Integer.parseInt(dts. readLine());
System.out.println("square root
of"+m+"="+Math.sqrt(m));
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
}
}

java Program to Draw a Concentric Circles with Random Colors

Program to Draw a Concentric Circles with Random Colors

Java Source Code
import java.awt.*;
import java.applet.*;
import java.util.*;
public class cir_c extends Applet
{
public void paint(Graphics g)
{
Random rg = new Random();
for (int i=1; i<=3; i++)
{
int r = rg.nextInt(255);
int gr = rg.nextInt(255);
int b = rg.nextInt(255);
Color c = new Color(r,gr,b);
g.setColor(c);
g.drawOval(100+i*5,100+i*5,100-i*10,100-i*10);
}
}
}

Output:

java Program to Create an Application that Represents Biodata Form

 Program to Create an Application that Represents Biodata Form
import java.lang.*;
import java.io.*;
class EmployeeS
{
int age, num;
String nme, add;
void emp_Info(int x)
{
try
{
num = x;
DataInputStream dts=new DataInputStream(System.in);
System.out.println("Enter Your Name:");
nme = dts.readLine();
System.out.println("\nEnter Your Address:");
add = dts.readLine();
System.out.println("\nEnter Your Age:");
age = Integer.parseInt(dts.readLine());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
class EmployeeM extends EmployeeS
{
String dep;
int sal;
void emp_dep(int da)

{
try
{
DataInputStream pts=new DataInputStream(System.in);
System.out.println("\nEnter your Department:");
dep = pts.readLine();
System.out.println("\nEnter your Salary:");
sal = Integer.parseInt(pts.readLine());
sal = sal-((sal*da)/100);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
class EmployeeF extends EmployeeM
{
void show_info()
{
System.out.println("\nThe name of Employee is:" + nme);
System.out.println("\nThe number of Employee is:" + num);
System.out.println("\nThe address of Employee is:"
+ add);
System.out.println("\nThe age of Employee is:" + age);
System.out.println("\nThe department of Employee is:"
+ dep);
System.out.println("\nThe salary of Employee is:" + sal);
}
}
class Main
{

public static void main(String arg[])
{
EmployeeF ep = new EmployeeF();
ep.emp_Info(30);
ep.emp_dep(5);
ep.show_info();
}
}
Output:

java Program to convert Fahrenheit temperature into Celsius temperature

Program to convert Fahrenheit temperature into Celsius
temperature using command line

import java.lang.*;
import java.io.*;
class temp
{
public static void main(String a[])
{
try
{
DataInputStream dts=new DataInputStream(System.in) ;
float m=Float.parseFloat(a[0]);
double f;
f = ( ( 9 * m ) / 5 + 32 );
System.out.println("tempreture in fahrenheit ="+f);
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
}
}
Output:
java Program to convert Fahrenheit temperature into Celsius temperature

java Program to Convert a Decimal Number into Binary, Octal, Hexadecimal

java Program to Convert a Decimal Number into Binary, Octal, Hexadecimal

import java.io.*;
class conversion
{
public static void main(String s[])throws IOException
{
int a;
DataInputStream ins=new DataInputStream (System.in);
System.out.println("Enter value");
a = Integer.parseInt(ins.readLine());
System.out.println("Binary:"+ Integer.toBinaryString(a));
System.out.println("Hexadecimal:" +
Integer.toHexString(a));
System.out.println("Octal:" + Integer.toOctalString(a));
}
}
Output:
java Program to Convert a Decimal Number into Binary, Octal, Hexadecimal

java program to check whether the number is palindrome or not


/*** Program to Check Whether a Number is Palindrome or Not ***/
import java.io.*;
class rev
{
public static void main(String a[ ])
{
int n, m, rev = 0;
int k = Integer.parseInt(a[0]);
n = k;
while( n > 0 )
{
m = n % 10;
rev = ( rev * 10 ) + m;
n = n / 10;
}
if( rev == k )
System.out.println("palindrome" );
else
System.out.println(" not palindrome" );
}
}
Output:
java program to check whether the number is palindrome or not

java Program to Calculate Area and Circumference of Circle

Program to Calculate Area and Circumference of Circle

import java.lang.*;
import java.io.*;
class crcl
{
public static void main(String a[])
{
try
{
DataInputStream dts=new DataInputStream(System.in) ;
System.out.println("enter radius of circle");
float m = Float.parseFloat(dts. readLine());
double area, cir;
area = 3.14 * m * m;
cir = 2 * 3.14 * m;
System.out.println("area of circle =" + area);
System.out.println("circumfrance of circle =" +cir);
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
}
}
Output:

java program to add two matrix

java  program to add two matrix
import java.lang.*;
import java.io.*;
class addmatrix
{
static void add()
{
int a[][], b[][], c[][];
a = new int[2][2];
b = new int[2][2];
c = new int[2][2];
DataInputStream dts=new DataInputStream(System.in);
try
{
System.out.println("enter first matrix
2*2 order :");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=Integer.parseInt(dts.readLine());
}
}
System.out.println("enter second matrix
2*2 order :");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)

{
b[i][j]=Integer.parseInt(dts.readLine());
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("\t");
}
}
catch(Exception x)
{
System.out.println("error");
}
}
public static void main(String arg[])
{
add();
}
}
java program to add two matrices