0% found this document useful (0 votes)
5 views39 pages

Std 12 Java Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views39 pages

Std 12 Java Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 39

HEBRON HIGHER SECONDARY SCHOOL

Std: 12(Sci/Gen) Java Programs


Points to be remembered and followed strictly:
1. Write the java programs in notepad.
2. Name of public class and file name should be same.
3. We have to first compile the java program and then execute(run).
4. To save java file, filename.java will save the file as java file.
5. To compile the java program: javac filename.java.
6. ‘javac’ command is used to compile a java file.
7. To execute(run) java program: java filename.java.
8. ‘java’ command is used to execute(run) a java file.
9. Use command prompt to compile and run java programs.

E.g1 : Write a Java program to print welcome message on the screen.


class A1
{
public static void main(String args [ ])
{
System.out.println("Hello! Mr., Mrs. _______, Welcome to Java
Program");
}
}
E.g.2: Write a program to print your name, class and school name.

class A2
{
public static void main(String args[])
{
System.out.println("Name=Jay");
System.out.println("Class=12th");
System.out.println("School=Hebron");
}
}
E.g.3: Write a Java program to print your name and school name and
class with star pattern.
class A3
{
public static void main (String args[])
{
System.out.println("**********************");
System.out.println("*name: ______________ *");
System.out.println("class :________________*");
System.out.println("school:_______________*");
System.out.println("**********************");
}
}
E.g. 4: Write a Java program to find Sum of two integer values.
class A4
{
public static void main(String args[])
{
int x=5;
int y=10;
int Sum=0;
Sum= x+y;
System.out.println(“The Sum is” +Sum);
}
}
E.g. 5: Write a Java program to take the marks of three Subjects and find
out total and percentage.
class A5
{
public static void main(String args[])
{
int ac=60;
int stat=55;
int eco=65;
int total=0;
float per=0;
total=ac+stat+eco;
per=total/3;
System.out.println(“The total is”+total);
System.out.println(“The per is”+per);
}
}
E.g. 6: Write a program to find out Simple interest of the Principle
amount is Rs. 1000 and time is 5 years and rate of interest in 4%
class A6
{
public static void main(String args[])
{
int P=1000;
int N=5;
int R=4;
int SI=0;
SI=(P*R*N)/100;
System.out.println(“The SI is” +SI);
}
}
E.g7.: Write a program to find out the area of a circle of the radius=5
class A7
{
public static void main(String args[])
{
int r=5;
double area=0;

area=3.14*r*r;

System.out.println(“The area is” +area);


}
}
E.g. 8. Write a program to find out whether the student is pass or fail.
class A8
{
public static void main(String args[])
{
int marks;
marks=Integer.parseInt(args[0]);
if (marks>=35)
{
System.out.println("Student is pass");

}
else
{
System.out.println( "Student is Fail");
}
}
}
E.g.9 Write a program to check whether the person is eligible to vote or
not.
class A9
{
public static void main (String args[])
{
int Age;
Age=Integer.parseInt(args[0]);
if (Age >=18)
{
System.out.println("Person is eligible to vote");
}
else
{
System.out.println("Person is not eligible to vote");
}
}
}
E.g. 10 Write a program to check whether the given number is even
number or odd number.
class A10
{
public static void main (String args[])
{
int no;
no=Integer.parseInt(args[0]);
if (no%2 ==0)
{
System.out.println("The number is even");
}
else
{
System.out.println("The number is odd ");
}
}
}
Eg. 11 Write a program to check whether the given year is leap year or
not.

class A11
{
public static void main(String args [])
{
int year ;
year=Integer.parseInt(args[0]);
if (year%4==0)
{
System.out.println(“It is a leap year”);
}
else
{
System.out.println(“It is not a leap year”);
}
}
}
E.g.12 Write a program to check whether the number is positive or
negative or zero.
class A12
{
public static void main(String args[]);
{
int no;
no=Integer.parseInt(args[0])
if (no>0)
{
System.out.println(" Number is positive");
{
else (no<0)
{
System.out.println(“Number is Negative”)
}
else
{
System.out.printhin("Number is zero")
}
}
}
Eg.13 Write a program to find out grade on the basis of percentage.
class A13
{
public static void main(String args[])
{
int per;
per=Integer.parseInt(args[0]);
if (per>=75)
{
System.out.println(“Grade A”);
}
else if (per>=60)
{
System.out.println(“Grade B”);
}
else if (per>=50)
{
System.out.println(“Grade C”);
}
else if (per>=40)
{
System.out.println(“Grade D”);
}
else
{
System.out.println(“Grade E”);
}
}
}
}
Eg. 14 Write a program to input a day number between 1 to 7 and print
the answer in words.
(E.g. Monday - 1)
class 14
{
public static void main (String args[])
{
int day;
day=Integer.parseInt(args[0]);
switch (day)
{
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
case 3:
System.out.println(“Wednesday”);
break;
case 4:
System.out.println(“Thursday”);
break;
case 5:
System.out.println(“Friday”);
break;
case 6:
System.out.println(“Saturday”);
break;
case 7:
System.out.println(“Sunday”);
break;
default:
System.out.println(“Invalid day no.”);
break;
}
}
}
E.g. 15: Write a program to find month from given numeric value using
switch case statement.
class A15
{
public static void main(String args[])
{
int month=0;
month=Integer.parseInt(args[0]);
switch(month)
{
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
default:
System.out.println("Invalid month number. Try again...");
break;
}
}
}
E.g 16 & 17 Write a program to print first 10 natural numbers using for
and do while loops.
(i) using for loop
class A16
{
public static void main(String args[])
{
int i;
for (i=1; i<=10; i++)
{
System.out.println(i);
}
}
}

(i) Using do while loop

class A
{
public static void main(String args[])
{
int no=1;
do
{
System.out.println(“no”);
no++
}
while (no<=10);
}
}
E.g. 18. Write a Java program to print the number in reverse order
between 1 to 20.
class A18
{
public static void main (String args[ ])
{
int i;
for(i=20;i>=1;i--)
{
System.out.println(i);
}
}
}
Eg. 19. Write a program to print the table of given number.
class A19
{
public static void main(String args[ ])
{
int i,no=0;
no=Integer.parseInt(args[0]);
for(i=1;i<=10;i++)
{
System.out.println(no+"x"+i+"="+no*i);
}
}
}
E.g. 20 Write a Java program to display even number from 1 to 100.
class A20
{
public static void main(String args[ ])
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
}
}
E.g. 21 Write a Java program to display odd number from 1 to 100.
class A21
{
public static void main(String args[ ])
{
int i;
for(i=1;i<=100;i++)
{
if(i%2!=0)
{
System.out.println(i);
}
}
}
}
E.g. 22 Write a Java program to display a pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class A22
{
public static void main(String args[ ])
{
int i,j, row;
row=Integer.parseInt(args[0]);
for(i=1;i<=row;i++)
{
for (j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println(“ ”);
}
}
}
E.g. 23 Write a Java program to display a pattern:
*
* *
* * *
* * * *
* * * * *
class A23
{
public static void main(String args[ ])
{
int i,j, row;
row=Integer.parseInt(args[0]);
for(i=1;i<=row;i++)
{
for (j=1;j<=i;j++)
{
System.out.print(“*”);
}
System.out.println(“ ”);
}
}
}
E.g.24 Write a Java program to find factorial.
class Factorial
{
public static void main(String args[])
{
long fact=1;
int count;
int n=Integer.parseInt(args[0]);
for (count=n; count>1; count --)
{
fact=fact*count;
System.out.println(“The value of fact is” +fact);
}
}
}
E.g. 25 Lab Exercise 5 of Chapter no. 7
class A25
{
public static void main(String args[])
{
int i;
for(i=5;i<=50;i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
}
}
E.g. 26 Lab Exercise 4 of Chapter No.7
public class A
{
public static void main(String args[])
{
double P=1000;
int R=12;
int N=36;
int i=0;
double interest =0;
interest=((P*R*N)/100);
System.out.println("The monthly interest is" +interest);
}
}

Eg. 27 Write a Java program to print 0 to 10 numbers.


class X1{
public static void main(String args[]){
int i;
for(i=0;i<=10;i++){
System.out.println(i);
}
}
}

E.g. 28 Write a Java program to list the marks using array.

class Array
{
public static void main(String args[])
{
int marks[];
marks=new int [3];
for (int i=0; i<3; i++)
{
mark[i]=Integer.parseInt(args[i]);
System.out.println(“marks[“ + i + “ ] is” + marks[i]);
}
}
}
E.g. 29 Write a Java program to calculate the sum of decimal numbers
and find the average.

class ArrayAvg
{
public static void main(String args[])
{
double number[]={10.5,20.6,30.8,15.5,17.3,25.5,27.2, 20,30,10.5};
byte ctr;
double Sum=0, avg;
System.out.println(“List of number is “);
for(ctr=0; ctr<10; ctr ++)
{
System.out.println(Number[ctr]);
Sum=Sum + number [ctr];
}
avg=Sum /10;
System.out.println(“Average of above number is” + avg);
}
}
E.g. 30 Write a Java program to display five number and find out total
by using array.
class Array1
{
public static void main(String args[])
{
int A[] ={50, 60, 70, 75,80};
int i, sum=0;
for (i=0; i<5; i++)
{
sum=sum + A[i];
}
System.out.println(“The sum is” + sum);
}
}
E.g. 31 Write a program to input to input marks of five Subject by using
command line argument & find out total & percentage.
class Array 2
{
public static void main(String args[])
{
int A2[]=new int[5];
int i, Sum=0, Per=0;
for (i=0; i<5; i ++)
{
A2[i]=Integer.parseIn(args[i]);
Sum=Sum +A2[i];
Per=Sum/5;
for (i=0; i<5; i++)
{
System.out.println(“Subject” + i+ ” : +A2[i]);
{
System.out.println(“The total marks” + Sum);
System.out.println(“The percentage” +Per);
}
}
E.g. 32 Write a program to input 10 elements in an array & print the
output in reverse order.
class Array3
{
public static void main(String args[])
{
int A[]=new int[10];
int i;
for (i=0; i<10; i ++)
{
A[i]=Integer.parsenInt(args[i]);
}
for (i=9; i>=0; i--)
{
System.out.println(A[i]);
}
}
E.g. 33 Write a program to find out the maximum value from “n”
element of an array
class Array4
{
public static void main(String args[])
{
int A4[]=new int[5];
int i, max=0;
for(i=0;i<5; i++)
{
A4[i]=Integer.parsenInt(args[i]);
}
max=A4[0];
for (i=0;i<5; i ++)
{
if (max<A4[i])
{
max=A4[i];
}
}
System.out.println(“The maximum value is +max);
}
}
E.g. 34 Write a program to find out the minimum value from “n’
elements of an array.
class Array5
{
public static void main(String args[])
{
int A5[]=new int[5];
int i, min=0;
for (i=0; i<5; i++)
{
A5[i]=Integer.parseInt(args[i]);
min=A5[0];
for (i=0; i<5; i++)
{
if (min>A5[i])
{
min=A5[i];
}
}
System.out.println(“The minimum value is” + min);
}
}
E.g. 35 Write a Java program to display HEBRON using character
array.

class CharArray
{
public static void main(String args[])
{
int i;
char Str1[]={‘H’, ‘E’, ‘B’, ‘R’, ‘O’, ‘N’);
for (i=0; i<str1.length; i++)
{
System.out.println(str1[i]);
}
}
}
Eg. 36 Write a Java program to print today’s date and time.
import java.util.date;
class DateDemo
{
public static void main(String args[])
{
Date d = new Date();
System.out.println(“Today’s date is ” + d);
}
}

Exception Handling
Write a java program to show the use of ArrayIndexOutOfBounds
exception
class Exp1{
public static void main (String args[])
{
int a[]={50, 70, 80, 90, 60}, i;
try
{
for(i=1;i<=6;i++)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
System.out.println(“The Exception is” + e)
}
}
}

You might also like