Content
1. Hello world example
class program1
{
public static void main(String args[])
{
System.out.println("hello world..");
}
}
output:
hello world..
2. Program to find area of circle
class AreaCircle
{
public static void main( String args[ ])
double rad;
final double PI = 3.14159;
rad = 10;
double area = PI * rad * ra;
System.out.print( "\n Area of Circle is = " + area );
}
Output:
Area of Circle is = 314.1
3. Program to find area of triangle
class AreaTriangle
{
public static void main( String args[ ] )
{
double area, a, b, c, s;
a = 3;
b = 4;
c = 5;
s = (a + b + c) / 2;
area = Math.sqrt( s * (s-a) * (s-b) * (s-c) );
System.out.println("Area of Triangle is = " + area );
}
}
Output:
Area of Triangle is = 6.0
4. Program to convert ruppes to paisa
public class SimpleConversion
{
public static void main(String args[])
{
double n=26.50;
int a=(int) n;
double p=(n-a)*100;
System.out.println("Rs : "+a);
System.out.println("paise :" +(int)p);
}
}
Output:
Rs : 26
paisa : 50
5. Simple example to calculate Interest
class CompInterest
{
public static void main( String args[ ] )
{
double a, p, r ,n ,ci;
p = 1000;
r = 10;
n = 3;
a = p * Math.pow(( 1 + ( r / 100.0 ) ), n );
ci = a - p;
System.out.println( " Amount = " + a );
System.out.println( " Compound interest = " + ci );
}
}
Output:
Amount = 1331.0000000000005
Compound interest = 331.000000000000045
6. Boolean variable Example
class BoolTest
{
public static void main(String args[ ])
{
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
if(b)
{
System.out.println("This is executed.");
}
}
}
Output:
b is false
This is executed.
7. Swapping example using third variable
class Swap
{
public static void main( String args[ ] )
{
int a, b, t;
a = 10;
b = 20;
System.out.print( "\nBefore Swapping :" );
System.out.print( a + " " + b );
t = a;
a = b;
b = t;
System.out.print( "\nAfter Swapping : " );
System.out.print( a + " " + b );
}
}
Output:
Before Swapping : 10 20
After Swapping : 20 10
8. Swapping example without third variable
class SwapWithoutThirdVariable
{
public static void main( String args[ ] )
{
int a, b;
a = 10;
b = 20;
System.out.print("\nBefore Swapping : " );
System.out.print( a + " " + b );
a = a + b;
b = a - b;
a = a - b;
System.out.print( "\nAfter Swapping : " );
System.out.print( a + " " + b );
}
}
Output:
Before Swapping : 10 20
After Swapping : 20 10
9. Program to convert Total Number of Days into Years, Months, Weeks and remaing days.
class Days
{
public static void main( String args[ ] )
{
int days, years, months, weeks;
days = 1050;
years = days / 365;
days = days % 365;
months = days / 30;
days = days % 30;
weeks = days / 7;
days = days % 7;
System.out.print("\n Years = " + years );
System.out.print("\n Months=" + months );
System.out.print "\n Weeks = " + weeks );
System.out.print( "\n Days = " + days );
}
}
Output:
Years = 2
Months = 10
Weeks = 2
Days = 6
----------------------------------------------------------------------
10. Input using Console Example
import java.io.*;
class ConsoleExample
{
public static void main( String args[ ] )
{
Console cn = System.console();
int n;
System.out.print( "Enter a Number : ");
n = Integer.parseInt( cn.readLine() );
System.out.println( "The given number : " + n );
}
}
Output:
Enter a Number : 20
The given number : 20
11. Scanner example
import java.util.Scanner;
class ScannerExample
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner( System.in );
int n;
System.out.print( "Enter a Number : ");
n = sc.nextInt();
System.out.println( "The given number : " + n );
}
}
Output:
Enter a Number : 10
The given number : 10
12.Program to use of return statement
class ReturnTest
{
public static void main( String args[] )
{
int a = 10;
System.out.println( "Before return." );
if( a == 10 )
return;
System.out.println( "After return." );
}
}
Output:
Before return.
13. Program to use exit() function
class ExitTest
{
public static void main( String args[] )
{
int a = 10;
System.out.println( "MMM" );
if( a == 10 )
System.exit(0);
System.out.println( "NNN" );
}
}
Output:
MMM
0 Comments