Monday 26 August 2013

Let Us C Chapter 1 Exercise I-a

Q. 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.

I'm doing these exercises so that I have something to do with C. I didn't use doubles or %.2f  because they haven't been taught in chapter 1.

Code:

#include <stdio.h>

main()
{
    float BasicSalary,DAllowance,HRAllowance,temp;

    printf("Enter Ramesh's Basic Salary: ");
    scanf("%f",&BasicSalary);

    /**Calculate 1% of salary*/
    temp=BasicSalary/100;

    /**Assign Dearness Allowance (40%) and House Rent Allowance (20%)**/
    DAllowance=temp*40;
    HRAllowance=temp*20;

    /**display**/
    printf("Ramesh's Dearness Allowance is: %f\n",DAllowance);
    printf("Ramesh's House Rent Allowance is: %f\n\n",HRAllowance);

    printf("Ramesh's Total Salary is: %f\n",BasicSalary+DAllowance+HRAllowance);

    return 0;
}

Following are simplifications of this program:

#include <stdio.h>

main()
{
    float BasicSalary,GrossSalary;

    printf("Enter Ramesh's Basic Salary: ");
    scanf("%f",&BasicSalary);

    GrossSalary=BasicSalary+(BasicSalary*.6);

    printf("Ramesh's Gross Salary is= %f",GrossSalary);
}


OR

#include <stdio.h>

main()
{
    float BasicSalary,GrossSalary;

    printf("Enter Ramesh's Basic Salary: ");
    scanf("%f",&BasicSalary);

    GrossSalary=BasicSalary*1.6;

    printf("Ramesh's Gross Salary is= %f",GrossSalary);

}


2 comments:

  1. After reading the question I suddenly started missing my school days when my teacher used to ask us to write a program to do so and so things.

    Thanks for the blog post. Keep sharing such profitable blog article in future also.

    Best Regards,
    Silvester Norman
    Change MAC Address

    ReplyDelete
    Replies
    1. Yeah, my programs are and will remain pretty basic for a while.
      Glad you could come around Silverster, happy blogging.

      Delete