JAVA PROGRAMS
1) Write a program to accept marks(m1,m2,m3) of a student and display
the total and average
//Program to accept the marks of a student .Display the total and
average
import java.util.Scanner;
public class StudentMarks //Defining a class
{
public static void main(String[] args) //main method
{
int m1,m2,m3,total;//Variable declaration
float avg;
//Scanner class
Scanner sc=new Scanner(System.in);
//Accept the marks subjectwise
System.out.println("Enter the marks of the student..");
System.out.print("Enter Marks in subject1:");
m1=sc.nextInt();
System.out.print("Enter Marks in subject2:");
m2=sc.nextInt();
System.out.print("Enter Marks in subject3:");
m3=sc.nextInt();
//calculate total and average
total=m1+m2+m3;
avg=total/3;
//Display the output
System.out.println("Ouput...");
System.out.println("---------");
System.out.println("Mark1:" + m1);
System.out.println("Mark2:" + m2);
System.out.println("Mark3:" + m3);
System.out.println("Total:" + total);
System.out.println("Average:" + avg);
}
2) Write a Java Program to accept principal,number of years and rate of
interest and Calculate Simple Interest (Use the formula pnr/100)
//Program to calculate the Simple Interest
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args)
{
//Declare the variables p,r,t
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.println("SIMPLE INTEREST PROGRAM");
//Accept the values ..p,r,t
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
//Declare sis
float si;
//Calculate simple interest
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
}
3) Write a program to accept perpendicular and base of a right
angled triangle.Calculate and display hypotenuse, area and parameter
of the triangle
//Program to program to accept perpendicular and base of a right
angled triangle.Calculate and display hypotenuse, area and parameter
of the triangle
import java.util.Scanner;
public class Triangle
{
public static void main(String str[])
{
int p,b;
double h,area,per;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the perpendicular:");
p=sc.nextInt();
System.out.println("Enter the base:");
b=sc.nextInt();
h=Math.sqrt(p*p+b*b);
area=1.0/2.0*p*b;
per=(p+b+h);
System.out.println("Hypotenuse of the Trianlge:"+h);
System.out.println("Area of the Trianlge:"+area);
System.out.println("Perimeter of the Trianlge:"+per);
}
}
4)A computer manufacturing company announces a special offer to their
customers on purchasing Laptops and the Printers accordingly
On Laptop Discount - 15%
On Printers Discount - 10%
Write a program in Java to calculate the discount,if a customer
purchases a Laptop and Printer
//To find the discount on Computer & Printer
import java.util.Scanner;
public class Discount
{
public static void main(String str[])
{
int r1=15,r2=10,c,p;
double d1,d2,m=0,n=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the price of Laptop:");
c=sc.nextInt();
System.out.println("Enter the price of Computer:");
p=sc.nextInt();
d1=(double)r1/100*c;
d2=(double)r2/100*p;
m=c-d1;
n=p-d2;
System.out.println("Price of Laptop after discount:"+m);
System.out.println("Price of Printer after discount:"+n);
}
}
5) Write a program in Java to accept the number of days and display the
result after converting into number of years, number of months and
the remaining number of days
//To convert number of days into years,months,weeks and the number of
days
//To convert days into years,months and the number of days
import java.util.Scanner;
public class Days1
{
public static void main(String str[])
{
int n,b,year, week, day,mon;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of days:");
n = s.nextInt();
year = n / 365;
b = n % 365;
System.out.println("No. of Years:"+year);
mon=b/30;
System.out.println("No. of Months:"+mon);
week = n / 7;
n = n % 7;
System.out.println("No. of Weeks:"+week);
day = n;
System.out.println("No. of Days:"+day);
Note: Accept n(number of days)= 789 and check for the output
6) Write a class with name employee and basic as its data member.
Find the gross_pay of an employee for the following allowances and
deduction.
Use meaningful variables
Dearness Allowance=25% of Basic Pay (da)
House Rent Allowance=15% of Basic Pay (hra)
Provident Fund=8.33% of Basic Pay (pf)
Net Pay=Basic Pay + Dearness Allowance + House Rent Allowance(np)
Gross Pay=Net Pay – Provident Fund (gp)
(Note: Please don’t use lengthy variable names
Caution: Use underscore for separating variable names and avoid white
spaces)
//A Sample Program to calculate Gross Salary using Scanner Class
import java.util.Scanner;
public class Employee
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
double basic,hra,da,pf,np,gp;
String name;
System.out.println(“Enter name of the Employee:”);
name =sc.nextFloat();
System.out.println(“Enter Basic Salary:”);
basic =sc.nextLine();
da = basic *25/100;
hra = basic * 15/100;
pf = basic*8.33/100;
gp = basic+hra+da;
np=gp-pf;
System.out.println(“Name of the Employee:”+name);
System.out.println(“Gross Pay:”+gp);
System.out.println(“Net Pay:”+np);
}
}