Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:22:44+00:00 2026-06-17T04:22:44+00:00

consider the code: public double calculatePrice() { int discount = 0 ; double totalPrice=itemCount*itemPrice;

  • 0

consider the code:

public double calculatePrice()
    { 
      int discount = 0 ;
      double totalPrice=itemCount*itemPrice;
      double netPrice;
      for(int i=0;i<NOOFITEMS.length;i++)
      {
        /*  if(i==0)
          {
              if(itemCount<NOOFITEMS[i])
               { discount=0;
                 break;
               }
          }
          /*if(i==NOOFITEMS.length-1)
          {  
        if(itemCount>NOOFITEMS[i])
           {  discount=50;
             break;
           }
          }
          if(itemCount<NOOFITEMS[i]&&itemCount>NOOFITEMS[i+1])
          {
            discount=DISCOUNTPERCENTAGE[i];  
          }*/
          discount=10;
      }
     System.out.println("discount "
             +discount);  
     System.out.println("totalprice "
             +totalPrice); 
      netPrice=totalPrice-(totalPrice*(discount/100));
      return netPrice;
    }

output::

Bill  ID:   501
Item NAME: grocery
 NO of Item: 11
 Price of Item: 250.0
discount 10
totalprice 2750.0
net price          2750.0
TRAINEE ID:   502
TRAINEE NAME: fruits
 Price of Item: 300.0
Item NO: 15
discount 10
totalprice 4500.0
net price         4500.0

The value of "netprice" should be 4050 [4500-(4500*(10/100)] as per the given statement.But the value is outputed as 4500 always..
the discount/100 is not computed correctly.Always the decimal part is ignored.
Can’t figure out the error..!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:22:45+00:00Added an answer on June 17, 2026 at 4:22 am

    The culprit is:

    totalPrice*(discount/100)
    

    discount is an int, so discount/100 is an integer division and decimals get truncated. For example, 10/100 is 0.

    To fix the issue, you can force the use of doubles:

    totalPrice*(discount/100d)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code snippet: class Test { public int Length{ get; set; }
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
Consider the code public class Base { public virtual int Add(int a,int b) {
Consider the code below: public class Analyzer { protected Func f,fd; public delegate double
Consider the following code: [Serializable] public class Region { public double North {get; set;}
Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2
Consider this code sample: public abstract class Parent { public int val; public Parent()
Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider the following code: public interface A { public A another(); } public interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.