JAVA PROGRAMS
1 //example of java program
class prog1
{
public static void main(String args[])
{
[Link]("Hello Students");
}
}
2 //write a java program to display the given output.
class prog2
{
public static void main(String args[])
{
[Link]("*********************");
[Link]("Name :Priya");
[Link]("City :Baroda");
[Link]("Std :12B");
[Link]("*********************");
}
}
3 //addition of two number
class prog3
{
public static void main(String args[])
{
int n1,n2,add;
n1=10;
n2=5;
add=n1+n2;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
[Link]("Addition of two numbers:"+add);
}
}
4 //subtraction of two numbers
class prog4
{
public static void main(String args[])
{
int n1,n2,sub;
n1=10;
n2=5;
sub=n1-n2;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
[Link]("Subtraction of two numbers:"+sub);
}
}
5 //multiplication of two numbers
class prog5
{
public static void main(String args[])
{
int n1,n2,mul;
n1=10;
n2=5;
mul=n1*n2;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
[Link]("Multiplication of two numbers:"+mul);
}
}
6 //division of two numbers
class prog6
{
public static void main(String args[])
{
int n1,n2,div;
n1=10;
n2=5;
div=n1/n2;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
[Link]("Division of two numbers:"+div);
}
}
7 //write a java program for addition, subtraction, multiplication
and division of two numbers
class calc
{
public static void main(String args[])
{
int n1,n2,add,mul,sub,div;
n1=10;
n2=5;
add=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n1/n2;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
[Link]("Addition of two nos:"+add);
[Link]("Subtaction of two nos:"+sub);
[Link]("Multiplication of two nos:"+mul);
[Link]("Division of two nos:"+div);
}
}
8 //accept input at command prompt and display no is odd or
even
import [Link].*;
class evenodd
{
public static void main(String args[])
{
int no;
Scanner s=new Scanner([Link]);
[Link]("Enter no1:");
no=[Link]();
if(no%2==0)
{
[Link](no+ " is even");
}
else
{
[Link](no+ " is odd");
}
}
}
9 //write a java program to display the length of the given string
class s1
{
public static void main(String args[])
{
String str="Welcome";
int len;
len=[Link]();
[Link]("String:"+str);
[Link]("Length:"+len);
}
}
10 // write a java program to display given string into uppercase
and lowercase
class s2
{
public static void main(String args[])
{
String str="Welcome";
[Link]("String:"+str);
[Link]("Uppercase:"+[Link]());
[Link]("Lowercase:"+[Link]());
}
}
11 // write a java program to enter rollno,name and marks of
three subjects and find out total and percentage .
import [Link].*;
class result
{
public static void main(String args[])
{
int rno,sub1,sub2,sub3,tot,per;
Scanner s=new Scanner([Link]);
[Link]("Enter Rollno:");
rno=[Link]();
[Link]("Enter marks of sub1:");
sub1=[Link]();
[Link]("Enter marks of sub2:");
sub2=[Link]();
[Link]("Enter marks of sub3:");
sub3=[Link]();
tot=sub1+sub2+sub3;
per=tot/3;
[Link]("\n\tResult");
[Link]("***************");
[Link]("Roll no:"+rno);
[Link]("Marks of sub1:"+sub1);
[Link]("Marks of sub2:"+sub2);
[Link]("Marks of sub3:"+sub3);
[Link]("Total:"+tot);
[Link]("Percentage:"+per);
[Link]("***************");
}
}
12 // write a java program to display larger no. from the given two
numbers
class large
{
public static void main(String args[])
{
int n1,n2;
n1=100;
n2=200;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
if(n1>n2)
{
[Link]( n1 +" is larger than "+ n2);
}
else
{
[Link]( n2 +" is larger than "+ n1);
}
}
}
13 // write a java program to display smaller no from the given
numbers
class min
{
public static void main(String args[])
{
int n1,n2;
n1=10;
n2=20;
[Link]("No1:"+n1);
[Link]("No2:"+n2);
if(n1<n2)
{
[Link]( n1 +" is smaller than "+ n2);
}
else
{
[Link]( n2 +" is smaller than "+ n1);
}
}
}
14 // write a java program to display even or odd number from
the given number.
class evenodd
{
public static void main(String args[])
{
int i=21;
if(i %2==0)
{
[Link](i +" is even number");
}
else
{
[Link](i +" is odd number");
}
}
}
15 //display 1 to10 using for loop
class p1
{
public static void main(String args[])
{
int i;
for(i=0;i<=10;i++)
{
[Link](i);
}
}
}
16 //display 10 to1 using for loop
class p1
{
public static void main(String args[])
{
int i;
for(i=10;i>=1;i--)
{
[Link](i);
}
}
}