Java Simple Program


1.  Write a program to evaluate simple interest of a given principle, rate and time.

import java.util.*;

public class intrest

{

public static void main(String[] args)

{

int p,n;

float r,intr;

Scanner s1=new Scanner(System.in);

System.out.println("Enter main price:-");

p=s1.nextInt();

System.out.println("Enter time duration in days:-");

n=s1.nextInt();

System.out.println("Enter intrest rate:-");

r=s1.nextInt();

intr=p*r*n/100;

System.out.println("intrest:-"+intr);


}

}

2. A motor cycle dealer sells two-wheelers to his customer on loan, which is to be repaid in 5 years. The dealer charges simple interest for the whole term on the day of giving the loan itself. The total amount is then divided by 60(months) and is collected as equated monthly instalment (EMI). Write a program to calculate the EMI for a loan of Rs. X, where X is given from command line argument. Print the EMI value in rupees.

import java.util.*;

public class EMI

{

public static void main(String[] args)

{

float price,rate,m_rate,year=5,month,m_price,m_emi; 

month=year*12;

Scanner s1=new Scanner(System.in);

System.out.println("Enter Motor Cycle Price:-  ");

price=s1.nextFloat();

m_price=price/month;

System.out.println("Enter Motor Cycle Rate:-  ");

rate=s1.nextFloat();

m_rate=rate/12;

m_emi=m_price +(m_price*m_rate);

System.out.println("Motor Cycle Monthly EMI:-  "+m_emi);

}

}

3. A car accessories shop assigns code 1 to seat covers, 2 to steering wheel covers , 3 to car lighting and 4 for air purifiers. All other items have code 5 or more. While selling the goods, a sales tax of 2% to seat covers ,3% to steering wheel covers, 4% to car lighting, 2.5% to air purifiers and 1.2% for all other items is charged. A list containing the product code and price is given for making a bill. Write a java program using switch statements to prepare a bill.

import java.util.*;

public class car_accessories

{

public static void main(String[] args)

{

int choice;

double amt;

char iteam;

Scanner s1=new Scanner(System.in);

System.out.println("1. Seat cover:-15000RS/-");

System.out.println("2. Streeing Wheel cover:-1000RS/-");

System.out.println("3. Car Lighting:-7000RS/-");

System.out.println("4. Air Purifiers:-500RS/-");

System.out.println("5. For Any Other:-Enter Amount");

System.out.println("Enter your choice: ");

choice=s1.nextInt();

switch(choice)

{

case 1:

amt=15000 + (15000*2/100);

System.out.println("Seat cover total charges are:- "+amt);

break;

case 2:

amt=1000 +(1000*3/100);

System.out.println("Streeing Wheel total charges are:- "+amt);

break;

case 3:

amt=7000 + (7000*4/100);

System.out.println("Car lighting total charges are:- "+amt);

break;

case 4:

amt=500 +(500*2.5/100);

System.out.println("Air Purifiers total charges are:- "+amt);

break;

case 5:

System.out.println("Enter amount:- ");

amt=s1.nextDouble();

/*System.out.println("Enter iteam name:- ");

iteam=s1.nextChar();

*/

amt=amt + (amt*1.2/100);

System.out.println(" total charges are:- "+amt);

break;

default:

System.out.println("Enter valid choice!");

break;

}

}

}

4. Write a java program to scan 3 integer values from the command line argument and display the maximum number using conditional operator.

import java.util.*;

public class max

{

public static void main(String[] args)

{

int a,b,c;

Scanner s1=new Scanner(System.in);

System.out.println("Enter value of a=");

a=s1.nextInt();

System.out.println("Enter value of b=");

b=s1.nextInt();

System.out.println("Enter value of c=");

c=s1.nextInt();

if(a>b && a>c)

{

System.out.println("Gretar value is a="+a);

}

else if(b>c)

{

System.out.println("Greater value is b="+b);

}

else

{

System.out.println("Greater value is c="+c);

}

}

}

5. Write a program to calculate the hypotenuse of right angled triangle when other sides of the triangle are given. (Hypotenuse = square root (x*x + Y *Y))

import java.util.*;

public class hypotenuse

{

public static void main(String[] args)

{

int x,y;

double hypotenuse;

Scanner s1=new Scanner(System.in);

System.out.println("Enter value of x: ");

x=s1.nextInt();

System.out.println("Enter value of y: ");

y=s1.nextInt();

hypotenuse=Math.sqrt(x*x + y*y);

System.out.println("Value of hypotenuse is: "+hypotenuse);

}

}

6. Write a program to calculate the area of square and rectangle by overloading the area method.

import java.util.*;

public class overloading

{

public static void main(String[] args)

{

int x,y,sum;

Scanner s1=new Scanner(System.in);

System.out.println("Enter value of x: ");

x=s1.nextInt();

System.out.println("Enter value of y: ");

y=s1.nextInt();

sum=area(x);

System.out.println("Area of square is = "+sum);

sum=area(x,y);

System.out.println("Area of rectangle is = "+sum);

}

static int area(int l)

{

int sum;

sum = 4*l;

return sum;

}

static int area(int l,int b)

{

int sum;

sum = l*b;

return sum;

}

}

7. Create a complex number class. The class should have a constructor and methods to add, subtract and multiply two complex numbers and to return the real and imaginary parts.

import java.util.*;

public class complexnumber

{

double rear , img;

complexnumber(double r,double i)

{

this.rear=r;

this.img=i;

}

public static complexnumber sum(complexnumber c1,complexnumber c2)

{

complexnumber temp=new complexnumber(0,0);

temp.rear=c1.rear+c2.rear;

temp.img=c1.img+c2.img;

return temp;

}

public static void main(String[]  args)

{

complexnumber c1=new complexnumber( );

complexnumber c2=new complexnumber( );

Scanner s1=new Scanner(System.in);

System.out.println("Enter Real part of c1:- ");

c1.rear=s1.nextDouble();

System.out.println("Enter Imaginary part of c1:- ");

c1.img=s1.nextDouble();

System.out.println("Enter Real part of c2:- ");

c2.rear=s1.nextDouble();

System.out.println("Enter Imaginary part of c2:- ");

c2.img=s1.nextDouble();

complexnumber temp=sum(c1,c2);

System.out.println(temp.rear + "+" +temp.img+"i");

}

}

8.A shop during festival season offers a discount 10% for purchase made up to Rs.1,000, 12% for purchase value of Rs.1,000 or more up to Rs 1,500 and 15% for purchase value of Rs.1,500 or more. Write a program to implement the above scheme for a given sales and print out the sales and print out the sales value, discount and net amount payable by a customer. Create necessary methods and constructors.

import java.util.*;

public class Discount

{

Discount(double price)

{

int discount;

if(price<=1000)

{

discount=10;

System.out.println("Price is :  "+price+"\nDiscount:- "+discount);

price=price - ((price*discount)/100);

System.out.println("NetAmount is:- "+price);

}

else if(price>1000 &&price<1500)

{

discount=12;

System.out.println("Price is :  "+price+"\nDiscount:- "+discount);

price=price - ((price*discount)/100);

System.out.println("NetAmount is:- "+price);

}

else

{

discount=15;

System.out.println("Price is :  "+price+"\nDiscount:- "+discount);

price=price - ((price*discount)/100);

System.out.println("NetAmount is:- "+price);

}

}

public static void main(String[] args)

{

double rs;

System.out.println("Enter Total Price:- ");

Scanner s1=new Scanner(System.in);

rs=s1.nextDouble();

Discount d1=new Discount(rs);

}

}

9. A bank gives 6.5% per annum interest on deposits made in that bank. Write a program to calculate the total amount that a person will receive after the end of 5 years for a deposit of Rs.5000 for compound interest. Create necessary methods and constructors too.

public class bank

{

bank(double p1,double r1,int y1)

{

double ans;

ans=p1*Math.pow((1+r1/12),(y1*12));

System.out.println("Amount  is:- "+ans); 

}


public static void main(String[] args)

{

bank b1=new bank(5000,6.5,5);

}

}

10. Write a java program to display powers of 2 i.e. 2,4,8,16 etc up to 1024 using bitwise operators

public class power

{

public static void main(String[] args)

{

int a=1;

for(int i=1;i<11;i++)

{

System.out.println(a<<i);

/*a=1,i=1

1<<1(Bitwise shift operator)=1*2^1=2

1<<2=1*2^2=4

1<<3=1*2^3=8

*/

}

}

}

The End

No comments:

Post a Comment

Aarsh Prajapati

Welcome! Java Programs And Theory  Practical    Theory Java Basic Program Java Simple Program Array, Inheritance and Interface Program Packa...