Wednesday, 15 April 2015

CONDITION CHECKING PROGRAMS

program to find highest of three number using nested if
import java.util.*;
class check2
{
public static void main(String args[])
{
Scanner Sn=new Scanner(System.in));
int x,y,z,m;
Systemout.println(“enter three number”);
x=Sn.nextInt();
y= Sn.nextInt();
z= Sn.nextInt();
if(x>=y)
{         
            if(x>=z)
                        m=x;
            else
                        m=z;
}
else
{
            if(y>=z)
                        m=y;
            else
                        m=z;
}
System.out.println(“highest  number is ”+m);
}
}

// above program will input 3 number from user if x is greater than y  then x will be compared with z if this condition is also true then it will display x otherwise it will display z else if x is less then y then y will be compared with z and depending upon condition given in else block y or z will be displayed.


// program to find calculate net salary of the employee depending upon basic salary.


Basic                                       DA      HRA
Less than 2000                        10%     5%
Greater than equal to 2000     25%     15%
But less than 4000
Greater than equal to 4000     30%     25%
But less than 8000
Greater than 8000                   40%     30%
import java.util.*;
class check2
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int basic;
double da,hra,net;
Systemout.println(“enter basic salary”);
basic=Sn.nextInt();
if(basic<2000)
{
da=0.10*basic;
hra=0.05*basic;
}
else if(basic<4000)
{
da=0.25*basic;
hra=0.15*basic;
}
else if(basic<8000)
{
da=0.30*basic;
hra=0.25*basic;
}
else
{
da=0.40*basic;
hra=0.30*basic;
}
net=basic+da+hra;
System.out.println(“Basic Salary “+basic);
System.out.println(“Dearness allowance “ + da);
System.out.println(“House rent “+hra);
System.out.println(“Net Salary “+net);
}
}



A cloth showroom has announced the following discounts on the purchase of items, based on the total cost of the item purchased:-
Total Cost                                    Discount(in percentage)
Less than Rs 2000                       5%
Rs 2001 to Rs. 5000         25%
Rs. 5001 to Rs 10000       35%
Above Rs. 10000              50%
Write a program to input the total cost and to compute and display the amount to be paid by the customer after availing the discount.                                             [2006]
import java.util.*;
class prog2
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int cost;
double amt,dis;
Systemout.println(“Enter Cost to the item”);
cost=Sn.nextInt();
if(cost<=2000)
  dis=cost*0.05;
else if(cost<=5000)
  dis=cost*0.25;
else if(cost<=10000)
  dis=cost*0.35;
else
  dis=0.50;
amt=cost-dis;
System.out.println(“Amount to be paid “+amt);
}
}


program to input two numbers and an operator perform arithmetic operation as per operator
import java.util.*;
class check7
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int a,b;
char op;
System.out.println(“enter 2 numbers and one operator”);
a= Sn.nextInt();
b= Sn.nextInt();
op=Sn.next().charAt(0);
switch(op)
{
case  ‘+’: System.out.println(a+b);
                break;
case  ‘-’: System.out.println(a-b);
                break;
case  ‘*’: System.out.println(a*b);
                break;
case  ‘/’: if(b==0)
                System.out.println(“divide by zero”);
               else
                        System.out.println(a/b);
                break;
default : System.out.println(“invalid operator”);
            break;
}
Write a menu driven program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit
Solution :-
import java.io.*;
public class first
{
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
System.out.println("Press 1 for Fahrenhite to Celsius");
        System.out.println("Press 2 for Celsius to Fahrenhite");
       
System.out.println("Enter Your choice :- ");
        int c=Integer.parseInt(br.readLine());
       
double t,res;
       
switch(c)
        {
            case 1:System.out.println("Enter temperature in Fahrenhite :- ");
                    t=Double.parseDouble(br.readLine());
                    res=(t-32)* (5/9);
                    System.out.println("Temperature in Celsius is :- " + res);
            break;
       
    case 2:System.out.println("Enter temperature in Celsius :- ");
                    t=Double.parseDouble(br.readLine());
                    res=t*(9/5) + 32;
                    System.out.println("Temperature in Fahrenhite is :- " + res);
            break;
       
    default : System.out.println("Wrong choice");
            break;
         }
    }
}

Output
Press 1 for Fahrenhite to Celsius
Press 2 for Celsius to Fahrenhite
Enter Your choice :-
1
Enter temreture in Fahrenhite :-
78.97
Tempreture in Celsius is :- 26.094444444444445

Variable Description
Variable
Type
Purpose
c
Integer
Storing choice of user
t
double
Storing temperature from user
res
double
Storing temperature after convertion


Write a program that outputs the results of the following evaluations based on number entered by the               User:-
a)  Natural Logarithm of the number.
b)  Absolute value of the number.
c)   Square root of the number.
d)  Random numbers between 0 and 1.
Solution :-
import java.io.*;
public class ten
{
public static void main() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int choice=1,n;
        double cel,fah;
        while(choice!=0)
        {
           System.out.println("Press 1 for Natural Log of a number");
          System.out.println("Press 2 for Absolute value of a number");
          System.out.println("Press 3 for Squire root of a number");
          System.out.println("Press 4 for random number between 0 and 1");
            System.out.println("Press 0 for exit");
           System.out.println("Enter your choice :- ");
            choice =Integer.parseInt(br.readLine());
            switch(choice)
            {
                case 1: System.out.println("Enter a number :- ");
                        n =Integer.parseInt(br.readLine());
                        System.out.println("Natural Logarithm =\t"+Math.log(n));
                break;
                case 2: System.out.println("Enter a number :- ");
                        n =Integer.parseInt(br.readLine());
                        System.out.println("Absolute value =\t"+Math.abs(n));
                break;
                case 3: System.out.println("Enter a number :- ");
                        n =Integer.parseInt(br.readLine());
                        System.out.println("Square Root =\t"+Math.sqrt(n));
                break;
                case 4: System.out.println("Random Number ="+Math.random());
                break;
                case 0  : break;
                default: System.out.println("Wrong Choice");
            }
        }
    }
}
Output
Press 1 for Natural Log of a number
Press 2 for Absolute value of a number
Press 3 for Squire root of a number
Press 4 for random number between 0 and 1
Press 0 for exit
Enter your choice :-
1
Enter a number :-
6
Natural Logarithm =      1.791759469228055
Press 1 for Natural Log of a number
Press 2 for Absolute value of a number
Press 3 for Squire root of a number
Press 4 for random number between 0 and 1
Press 0 for exit
Enter your choice :-
0
Variable Description
Variable
Type
Purpose
choice
Integer
Storing choice of user
n
Integer
Storing number of doing operation




No comments:

Post a Comment