Thread: Program not performing the specified arithmetic operations.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    Question Program not performing the specified arithmetic operations.

    Okay, so I am brand new to C programming. I am following Kanetkar's "Let us C" book and now I am stuck at the exercise of the first chapter itself.

    Question: Ramesh's basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

    Here's what I came up with:

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    
    int main()
    {
    	int basicSalary;
    	int da, hra, grossSalary; // da is dearness allowance; hra is house rent allowance
    
    
    	printf("Enter basic salary: ");
    	scanf("%d", &basicSalary);
    
    
    	da = 40 / 100 * basicSalary; // DA is 40% of the basic salary
    	hra = 20 / 100 * basicSalary; // HRA is 20% of the basic salary
    	grossSalary = basicSalary + da + hra;
    
    
    	printf("Gross salary is: %d\n", grossSalary);
    
    
    	return 0;
    }
    But the program gives the gross salary same as that of the basic salary. That is, it does not perform the specified arithmetic operations. It's supposed to calculate the da, hra and add them to the basic salary to get the gross salary.

    Example output:
    Enter basic salary: 10000
    The gross salary is: 10000
    What am I doing wrong?

    p.s. I am using Visual Studio 2013 as my ide.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    a quick read on C data types should help you solve your problem.

    An integer in c is a natural number, all values stored into integers are stored as natural numbers. The integer division of lets say 4/3 will give you 1 and not the expected result.Thus the division of 40/100 gives you a result of zero.floating points would be the appropriate data type to use in your case.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by africanwizz
    An integer in c is a natural number, all values stored into integers are stored as natural numbers.
    That is not true since the set of natural numbers is the set of positive integers (or the set of non-negative integers, if you prefer), whereas the range of values of signed integer types in C include negative integers. I suppose you could argue that the bit pattern corresponding to any given integer in C can be interpreted as a natural number, but then the same applies for any bit pattern, including those for values of floating point types.

    I think the key point here is that the result of integer division is of an integer type, hence "the integer division of lets say 4/3 will give you 1 and not the expected result", if you were expecting a result that was not an integer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by laserlight View Post
    That is not true since the set of natural numbers is the set of positive integers (or the set of non-negative integers, if you prefer), whereas the range of values of signed integer types in C include negative integers. I suppose you could argue that the bit pattern corresponding to any given integer in C can be interpreted as a natural number, but then the same applies for any bit pattern, including those for values of floating point types.
    Oh Yeah,thanks ..

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    I did try to use floating points. I declared the da, hra and the grossSalary variables as floating points but that still gives the same result.

    Example output:
    Enter basic salary: 1000
    The gross salary is 1000.000000

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You need to make sure the code does the multiplication before the divide.
    Code:
    #include <stdio.h>
    
    /* If you are not expecting any function inputs, put "void": */
    int main(void)
    {
        /* Separate vars and comment on each one */
        int basicSalary;    // ...
        int da;             // da is dearness allowance
        int hra;            // hra is house rent allowance
        int grossSalary;    // ...
    
    
        printf("Enter basic salary: ");
        scanf("%d", &basicSalary);
        
        da = (40 * basicSalary) / 100; // DA is 40% of the basic salary
        hra = (20 * basicSalary) / 100; // HRA is 20% of the basic salary
        grossSalary = basicSalary + da + hra;
    
        printf("Gross salary is: %d\n", grossSalary);
    
        return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Wow, that worked! Thanks a lot Click_here. But just curious though; Why does it need to perform the multiplication before division? Because I remember the math, to calculate a percentage of a number we need to do something like:
    percentage / 100 * number
    and that's exactly what I did in my version of this program. What went wrong then?

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    As explained by the other posts, integers don't have decimal points.

    Therefore:
    40 / 100 * basicSalary
    = (40 / 100) * basicSalary
    = (0) * basicSalary
    = 0
    Note: When 0.4 has its decimal points dropped, it becomes 0

    When you do the multiplication first (using 10000 as bS)
    = (40 * basicSalary) / 100
    = (40 * (10000)) / 100
    = (400000) / 100
    = 4000
    Last edited by Click_here; 03-29-2014 at 11:13 PM.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Ah, I see now. So it was basically just equivalating da and hra to 0 thus adding 0 to the bS to get the same amount as gS.
    Thanks a lot for your help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arithmetic Operations and formatting help!
    By SirSig in forum C Programming
    Replies: 3
    Last Post: 09-18-2011, 03:14 PM
  2. Listening to stdin while performing certain operations
    By Horsey in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2010, 09:10 AM
  3. arithmetic operations
    By XodoX in forum C++ Programming
    Replies: 6
    Last Post: 06-29-2009, 12:28 AM
  4. Resolve Arithmetic operations
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 09:14 PM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM