Pages

Tuesday, September 29, 2009

Accepting integer and float - Simple Interest Program

We can easily write a program that accepts an integer and a float value at the same time. Just make sure that both the values are accepted using proper specifier i.e. %d for integer and %f for float.
Example 1 : Simple Interest Program
void main()
{
int p;
float rate,time,si;
printf("\nEnter Principle Amount : ");
scanf("%d",&p); // %d for integer
printf("\nEnter Rate of Interest : ");
scanf("%f",&rate); // %f for float
printf("\nEnter Term : ");
scanf("%f",&time);
si=(p*rate*time)/100;
printf("\nSimple Interest %f",si);
}
Note :
  1. // denotes comments. The statements following // will not be processed by the compiler and will be treated as notes or comments.
  2. The ":" symbol used in printf() is not compulsory. It has been used just to provide a proper space to the user to enter values.
  3. Use clrscr() to make the previous output disappear.

So, as you can see there isn't any problem accepting both integers and floats in a program. Just make sure that you use proper specifiers.

Example 2 : The Salary Calculator

void main()
{
int salary;
float hra,pf,deduction,gross_income,net_income;
printf("\nEnter your basic salary : ");
scanf("%d",&salary);
hra=salary*3/100;
pf=hra+(salary*2/100); // calculated as hra + 2% of salary
gross_income=salary+hra+pf;
deduction=gross*3/100; // 3% of salary
net_income=gross_income-deduction;
printf("\nPRINTING DETAILS OF YOUR SALARY ");
printf("\============================");
printf("\nBasic Salary %d",salary);
printf("\nHouse Rent Allowance : %f",hra);
printf("\nProvident Fund : %f",pf):
printf("\nGross Salary %f",gross_salary);
printf("\deduction %f",deduction);
printf("\nNet Salary %f",net_income);
}

Note :

  1. All the formula's are imaginative.
  2. The first printf() and is used to print a heading of the program which is optional and so is the second printf() which is used a separator by typing the equal to ("=") sign.

0 comments:

Post a Comment