Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Java
How to construct a program
that will require the user to input
their salary and your program can
calculate the increment in salary that
will obtain based on the table below.
Using if----else selection control statement.
 
Salary Increment
 
N 800 - N1000 2%
 
N 101 - N2000 5%
 
N 201 - N4000 8%
 
N 400> 10%
i have tried this code but i wants to know weither i am correct or not. Thanks
 
import java.util.Scanner;
public class Question2
{
 public static void main(String [] args)throws IOException
 {
  float salary;
  double increment;
 Scanner scanner = new Scanner(new InputStreamReader(System.in));
 

  System.out.print("Enter Your Salary:");
 
  salary = scanner.nextFloat();
 
  if ((salary >=800) && (salary <=1000))
 
  {
   increment=(2 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if ((salary >=101) && (salary <=2000))
  {
   increment=(5 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if ((salary >=201) && (salary <=4000))
  {
   increment=(8 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if (salary > 400 )
  {
   increment=(10 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else
  System.out.println("\nNo More Increment For This Salary\n");
 
    }
}
Posted 1-May-13 14:29pm
Edited 1-May-13 22:59pm
v2
Comments
ThePhantomUpvoter at 1-May-13 21:04pm
   
"i have tried this code but i wants to know weither i am correct or not", if it works as you expect it to then it is correct. If it does not work as you expect it to then it is not correct.
Philippe Mori at 2-May-13 0:09am
   
Are you sure that your condition are the right ones. I think that some numbers are incorrect. For example 101 is probably 1001... It would make sense only if salary is an integer too since people with a salary in between would not have the augmentation. If original number are used things are also inconsistant...
p.uday kishore at 2-May-13 1:49am
   
is that working fine?if not we can look into it further..
Sudhakar Shinde at 2-May-13 4:02am
   
If salary is 900 then it will satisfy almost all the conditions. Please have a look at these conditions first.
@sunusi1 at 4-May-13 13:36pm
   
you have talked true, the range of salary is
800-1000
1001-2000
2001-4000
400>

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should check the logic behind your code. Do you realize salary ranges overlap?
As Sudhakar Shinde already pointed out, a salary of 900 would satisfy all your conditions (while due to the else if chain, only first condition's statement would be executed.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Praneet Nadkar 237
2 Marcin Kozub 225
3 Sergey Alexandrovich Kryukov 195
4 Shweta N Mishra 161
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100