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 :
- // denotes comments. The statements following // will not be processed by the compiler and will be treated as notes or comments.
- The ":" symbol used in printf() is not compulsory. It has been used just to provide a proper space to the user to enter values.
- 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 :
- All the formula's are imaginative.
- 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