01 Program to Demonstrate simple JAVA program
class A
{
public static void main(String args[])
{
System.out.println("Welcome to my World");
}
}
-----------------------------------------------------------------------------------------
Save the above Program as A.java
Compile Program in Command by using javac
C:\>javac A.java
Run the Program in Command by using java
C:\>java A
---------------------------------------------------------------------------------------------
OUTPUT :
Welcome to my World
02 Program to compute the area of a circle
class A2
{
public static void main(String args[])
{
int r=10;
System.out.println("Radius of Circle :" + r);
System.out.println("Area of Circle :" + r*r);
}
}
OUTPUT :
Radius of Circle : 10
Area of Circle : 314
03 Program to demonstrate type casting in JAVA
class conversion
{
public static void main(String args[])
{
byte b;
int i=257;
double d=323.142;
System.out.println("\nConversion of int to byte.");
b=(byte) i;
System.out.println("i and b " +i+" "+b);
System.out.println("\nConversion of double to int.");
i=(int)d;
System.out.println("d and i " +d +" "+i);
System.out.println("\nConversion of double to byte.");
b=(byte)d;
System.out.println("d and b " +d + " " +b);
}
}
OUTPUT :
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
04 Program to demonstrate the basic Arithmetic Operations
class arith
{
public static void main(String args[])
{
int a=10,b=3;
System.out.println("sum is "+(a+b));
System.out.println("difference is "+(a-b));
System.out.println("product is "+(a*b));
System.out.println("quotient is "+(a/b));
System.out.println("remainder is "+(a%b));
}
}
OUTPUT :
sum is 13
difference is 7
product is 30
quotient is 3
remainder is 1
05 Program to find largest of three numbers
class large
{
public static void main(String args[])
{
int a=10,b=30,c=20;
System.out.println("Greatest of given 3 numbers");
if(a>b&&a>c)
System.out.println(+a);
else if(b>c)
System.out.println(+b);
else
System.out.println(+c);
}
}
OUTPUT :
Greatest of given 3 numbers
30
06 Program to print first 10 Fibonacci numbers
class fib
{
public static void main(String args[])
{
int i=9,f=0,s=1,temp;
System.out.println(+f);
while(i>0)
{
System.out.println(+s);
temp=f+s;
f=s;
s=temp;
i--;
}
}
}
OUTPUT :
0
1
1
2
3
5
8
13
21
34
07 Program to Calculate the factorial of a number using RECURSION
class Factorial
{
int fact(int n)
{
int result;
if(n==1)
return 1;
result=fact(n-1)*n;
return result;
}
}
class Recursion
{
public static void main(String args[]){
Factorial f=new Factorial();
System.out.println("Factorial of 3 is " +f.fact(3));
System.out.println("Factorial of 4 is " +f.fact(4));
System.out.println("Factorial of 5 is " +f.fact(5));
}
}
OUTPUT :
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
08 Program to print prime numbers using CLA
class primecla
{
public static void main(String s[])
{
int n,r;
n=Integer.parseInt(s[0]);
for(int i=2;i<=n;i++)
{
int c=0;
for(int j=1;j<=i;j++)
{
r=i%j;
if (r==0)
c+=1;
}
if(c==2)
System.out.println(i);
}}}
OUTPUT :
java primecla 10
2
3
5
7
09 Program to Demonstrate Command Line Argument
class CommandLine
{
public static void main(String args[])
{
for(int i=0; i
System.out.println("args[" +i + "]: "+args[i]);
}
}
OUTPUT :
java CommandLine 1 3 4 2 5
args[0]: 1
args[1]: 3
args[2]: 4
args[3]: 2
args[4]: 5
10 Program to convert temperature from Fahrenheit to Celsius scale using CLA
class ftoccla
{
public static void main(String s[])
{
int f;
double c;
f=Integer.parseInt(s[0]);
c=5*(f-32)/9;
System.out.println(f+" fahrenheit = "+c+" celsius");
}
}
OUTPUT :
java ftoccla 34
0 comments:
Post a Comment