Java - Programs - Practice
Java - Programs - Practice
JAVA PROGRAMS
WITH OUTPUT
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
Program no 1
PROGRAMME:- A
public class Switch{
System.out.println("1.ARITHMATIC
OPERATORS\n2.LOGICAL OPERATORS\n3.RELATIONAL
OPERATORS\n\t\t(Choice=1)\n"); //Main menu
int choice=1;
int a=10,b=5,c;
switch(choice) //Main switch case
{
Copyrighted By @Curious_Coder
System.out.println("1.Addition\n2.Subtraction\n3.Multiplicati
on\n4.Division\nVALUES OF a AND b ARE 10 AND 5
RESPECTIVELY\n\t\t(Choice=3)\n");
int aChoice=3;
switch(aChoice) //Nested switch case
{
case 1: //addition
c=a+b;
System.out.println("Addtion of a and b is "+ c+"\n");
break;
case 2: //subtraction
c=a-b;
System.out.println("Subtraction of a and b is "+
c+"\n");
break;
Copyrighted By @Curious_Coder
case 3: //Multiplication
c=a*b;
System.out.println("Multiplication of a and b is "+
c+"\n");
break;
case 4: //division
c=a/b;
System.out.println("Divisiom of a and b is "+ c+"\n");
break;
default:
System.out.println("Please enter right choice");
}
break;
int lchoice=1;
switch(lchoice)
{
case 1: // AND operator
int age=25;
if(age>18 && age<50)
System.out.println("(AGE=25)\nYou are eligible for
this job.\n");
break;
if(Age!=18 || Age>18)
System.out.println("(Age=20)\nYou are not eligible
for voting\n");
break;
default:
System.out.println("Please enter right choice");
}
break;
int aage=20;
if(aage > 18)
System.out.println("(Age=20) You are eligible for
having a driving licens");
break;
default:
System.out.println("Please enter right choice");
}
break;
default:
System.out.println("Please enter right choice");
}
}
}
Copyrighted By @Curious_Coder
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 2
(a *****
****
***
**
*
(b) *
**
***
****
*****
****
***
**
*
Programme:-
public class Pattern{
{
for (int j=5; j>i;j--)
{
System.out.print(" ");
}
for (int k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5-1; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for (int k = 5-1; k >= i; k--)
{
System.out.print("*");
}
Copyrighted By @Curious_Coder
System.out.println();
}
}
}
OUTPUT:
Copyrighted By @Curious_Coder
Program : 3
3) WAP to find
(a) palendrome
(b) fibonacci series
(c) leap year
(d) even/odd number
Programmer:
(a) palindrome :
public class Palindrome{
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
OUTPUT :
Copyrighted By @Curious_Coder
System.out.println("---------------
Fibonacci series ------------------------ \n");
int n1=0,n2=1,n3, ,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
OUTPUT:
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
c) leap year
public class LeapYear{
System.out.println("\n---------------Leap year-----------------
\n(year=2020)");
int year=2020;
if ((year % 4 == 0) && (year % 100!= 0))
System.out.println("Entered year is a leap year");
else if(year%400 == 0)
System.out.println("Entered year is leap year");
else
System.out.println("Entered year is year is not a leap
year");
}
}
OUTPUT:
Copyrighted By @Curious_Coder
System.out.println("\n---------------Even/Odd number------------
-----\n(num=10)");
int num=10;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
Copyrighted By @Curious_Coder
}
}
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 4
//PROGRAM NO: 4
System.out.println("----------------MTRIX ADDITION--------------
\n");
int result[][] = new int[2][2];
int i,j;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
result[i][j]=arr1[i][j]+arr2[i][j];
System.out.println(result[i][j] );
}
System.out.println();
}
}
System.out.println("----------------MTRIX Multiplication---------
-----\n");
int c[][]=new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c[i][j]=0;
for(int k=0;k<2;k++){
c[i][j]+=ary1[i][k]*ary2[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
System.out.println("-------------------Matric Transpose------------
--\n");
int result[][]=new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
result[i][j]=mat1[j][i];
System.out.println(result[i][j]);
}
System.out.println();
}
}
}
OUTPUT :
Copyrighted By @Curious_Coder
@Curious_Coder
Practical No-1
CODE:
import java.applet.Applet; import
java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class login extends Applet
{
Label title = new Label("Login Page");
Label username = new Label("Username");
Label password = new Label("Password");
TextField tusername = new TextField(20);
TextField tpassword = new TextField(10);
Button loginn = new Button("Login");
Button reset = new Button("Reset");
@Curious_Coder
//Setting Bounds
title.setBounds(150, 50, 200, 100);
username.setBounds(20, 150, 150, 100);
password.setBounds(20, 240, 150, 100);
tusername.setBounds(180, 180, 250, 40);
tpassword.setBounds(180, 270, 250, 40);
loginn.setBounds(100, 350, 100, 40);
reset.setBounds(250, 350, 100, 40); er.setBounds(180,
400, 250, 40);
//Setting Fonts
title.setFont(new Font("Lucida",Font.PLAIN,34));
username.setFont(new Font("Lucida",Font.PLAIN,24));
tusername.setFont(new Font("Lucida",Font.PLAIN,24));
username.setFont(new Font("Lucida",Font.PLAIN,24));
tpassword.setFont(new Font("Lucida",Font.PLAIN,24));
tpassword.setEchoChar('*'); add(username);
add(title);
@Curious_Coder
add(password);
add(tusername);
add(tpassword);
add(loginn);
add(reset);
add(er);
setVisible(true);
}
}
/*
<APPLET CODE= login.class WIDTH=500 HEIGHT=500>
</APPLET>
*/
OUTPUT:
@Curious_Coder
CODE:
import java.applet.Applet; import
java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Setting Bounds
@Curious_Coder
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub