Go to Top

Monday, 3 October 2011

Utilities 14 : Percentage change

This tool will help you to calculate the increment/decrements and increased/decreased value, while increasing or decreasing a value by a constant percentage.

Java Program :

import java.io.*;
class percent_change
{
   static double n;
   static double p,c;
  static void main()throws IOException
  {
      BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter the number : ");
      n=Integer.parseInt(b.readLine());
      System.out.print("\nEnter percentage (w/o % sign) : ");
      p=(Double.parseDouble(b.readLine())*n)/100;
      System.out.print("\nSelect mode : Enter 'i' to increment or 'd' to decrement : ");
      String o=b.readLine();
      switch((int)o.charAt(0))
      {
          case 105:
          case 73:
          increment();
          break;
          case 100:
          case 68:
          decrement();
          break;
          default:
          System.out.print("\nWrong input\n\n");
          main();
        }
        display();
    }
    static void increment()
    {
        n=n+p;
        c=0;
    }
     static void decrement()
    {
        n=n-p;
        c=1;
    }
    static void display()
    {
        System.out.print("\n");
        if(c==0)
        System.out.print("The incremented value is : "+n+"\n\nChange : + ");
        if(c==1)
        System.out.print("The decremented value is : "+n+"\n\nChange : - ");
        System.out.print(p);
    }
}

1 comment:

ShareThis