Java Programs-
1. Java Program to print “Hello World”.
class First{
public static void main(String args[]){
System.out.println("Hello World");
}
}
2. Java Program for adding two integer numbers.
public class AddTwo{
public static void main(String[] args) {
int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y
}
}
3. Java Program for taking command line arguments.
public class ComLine{
public static void main(String args[]){
System.out.println ("Your input argument is:" + args[0]);
}
}
4. Java Program for creating constant PI.
public class ConstantExample {
public static void main(String args[]) {
static final double PI=3.14;
System.out.println("value of PI = "+PI);
}
}
5. Java Program for Simple Interest.
public class SimpleInterest {
public static void main (String args[]) {
float p, r, t, si;
p = 1000;
r = 2;
t = 5;
si = (p*r*t)/100;
System.out.println("Simple Interest is= " +si);
}
}
6. Java Program to print Standard Default Value of a variable.
public class sdv {
static int a;
public static void main(String[] args) {
System.out.println(“a=”+a); //standard default value
}
}
7. Java Program to display the use of instanceof operator.
public class Hello{
public static void main(String args[]){
Hello h=new Hello();
System.out.println(h instanceof Hello); //TRUE
}
}
8. Java Program to swap values of two variables using third variable.
import java.util.*;
class Swapping{
void swap(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
System.out.println("Numbers after swapping d="+a+" e="+b);
}
}
public class SwapTest{
public static void main(String args[]){
int d,e;
Scanner a=new Scanner(System.in);
Swapping s=new Swapping();
System.out.println("Enter first no=");
d=a.nextInt();
System.out.println("Enter second no=");
e=a.nextInt();
System.out.println("Numbers before Swapping d="+d+" e="+e);
s.swap(d,e);//call by value
System.out.println("Actual arguments remain same:\n d="+d+" e="+e);
}
}
9. Java Program to check that given number is Even or Odd.
import java.util.*;
public class EvenOdd{
public static void main(String args[]){
int num;
Scanner s=new Scanner(System.in);
System.out.println("Enter an Integer Number\n");
num=s.nextInt();
if(num%2==0)
System.out.println(num+" is Even number\n");
else
System.out.println(num+" is Odd number\n");
}
}
10. Java Program to display percentage and division of a student when subject marks are given.
import java.util.*;
public class Marksheet{
public static void main(String args[]){
int m,c,p,e,h,maxmarks;
float per;
Scanner s=new Scanner(System.in);
System.out.println("Enter Subject marks of Student:\n 1. maths\n 2. physics\n 3. chemistry\n4. hindi\n5. english\n");
m=s.nextInt();
p=s.nextInt();
c=s.nextInt();
h=s.nextInt();
e=s.nextInt();
System.out.println("Enter Maximum marks\n");
maxmarks=s.nextInt();
per=((float)(m+p+c+h+e)/maxmarks)*100;
if(per>=60)
System.out.println("I division with "+per+"%");
else{
if(per>=45)
System.out.println("II division "+per+"%");
else{
if(per>=33)
System.out.println("III division with "+per+"%");
else
System.out.println("Fail!!");
}
}
}
}
11. Java Program to perform arithmetic operations(simple calculator).
import java.util.*;
public class Calculator{
public static void main(String args[]){
int a,b,c=0;
int ch;
while(true){
System.out.println("Calculator:-\nA.Addition\nS.Subtraction\nM.Multiplication\nD.Division\nEnter Your Choice\n");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
System.out.println("Enter Two Integers\n");
a=s.nextInt();
b=s.nextInt();
switch(ch){
case 1:
c=a+b;
break;
case 2:
c=a-b;
break;
case 3:
c=a*b;
break;
case 4:
if(b!=0) c=a/b;
else System.out.println("Divisor must not be zero!!");
break;
default:
System.out.println("Wrong Choice");
System.exit(0);
}
System.out.println("Answer="+c);
}
}
}
14. Java program to find that given number is prime or not.
import java.util.*;
public class Prime{
public static void main(String args[]){
int num;
Scanner s=new Scanner(System.in);
System.out.println("Enter a Positive Integer Number\n");
num=s.nextInt();
if(num>0){
if(num==1){
System.out.println("Universal Prime Number\n");
System.exit(0);
}
int i;
for(i=2;i<num;i++){
if(num%i==0)break;
}
if(num==i)
System.out.println(num+" is prime number\n");
else
System.out.println(num+" is not a prime number\n");
}
else
System.out.println("Wrong Number Inserted\n");
}}
15. Java program to display the use of labeled loop.
public class LabledWhileLoop{
public static void main(String args[]){
int i = 0;
whilelabel:
while (i < 5){
System.out.println("outer value of i= " + i);
i++;
forlabel:
for (int j = 0; j < 5; j++){
if (j > 0){
continue forlabel;
}
if (i > 1){
continue whilelabel;
}
System.out.println("inner value of i= " + i + ", j= " + j);
} }
} }
16. Java program to calculate compound interest.
import java.util.*;
class CompoundInterest
{
private static double p,CI,R=5,P,S=1;
int T=10;
CompoundInterest()
{
this.p=p;
}
public double calCompoundInterest()
{
for(int i=1;i<=T;i++)
{
S=S*(1+(R/100));
}
CI=p*S-1;
return CI;
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the principle");
p=s.nextDouble();
CompoundInterest coin=new CompoundInterest();
System.out.println("Compound Interest :" +coin.calCompoundInterest());
}
}
17. Java program to find the Largest Number among three numbers
import java.util.Scanner;
public class Biggest_Number
{
public static void main(String[] args)
{
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
x = s.nextInt();
System.out.print("Enter the second number:");
y = s.nextInt();
System.out.print("Enter the third number:");
z = s.nextInt();
if(x > y && x > z)
{
System.out.println("Largest number is:"+x);
}
else if(y > z)
{
System.out.println("Largest number is:"+y);
}
else
{
System.out.println("Largest number is:"+z);
}
}
}
18. Java program to calculate gross salaray of an employee when basic salary is given.
import java.util.*;
public class EmployeeTest{
class Employee{
double bs,HRA,TA,DA,gs;
void setSalary(){
Scanner s=new Scanner(System.in);
System.out.println("Enter Basic Salary of Employee\n");
bs=s.nextDouble();
HRA=0.05*bs;
TA=0.1*bs;
DA=0.2*bs;
}
void getSalary(){
System.out.println("Basic Salary="+bs+"\nHRA="+HRA+"\nTA="+TA+"\nDA="+DA+"\nGross Salary="+grossSalary());
}
double grossSalary(){
if(bs>=20000)
gs=bs+HRA+TA+DA+5000;
else
gs=bs+HRA+TA+DA;
return gs;
}
}
public static void main(String args[]){
Employee.setSalary();
Employee.getSalary();
}
}
19. Java program to display the use of class object.
public class Student{
int rollno;
String sname;
float marks;
void sinfo(){
System.out.println("Roll Number: "+rollno+"Name: "+sname+"Marks: "+marks);
}
public static void main(String[] args) {
Student s = new Student();
s.rollno = 1;
s.sname = "Ajay";
s.marks = 421;
s.sinfo();
}
}
No comments:
Post a Comment