In: Computer Science
#include <stdio.h>
int main()
{
float sum_salary=0,sum_raise=0,sum_newsalary=0;
while(1)
{
float salary,rate,raise,newsalary;
printf("Enter Salary :");
scanf("%f",&salary);
if(salary==-1)
{
if(sum_salary==0)
return 0;
else
{
printf("Total Salary :%f Total raise :%f Total New Salary:
%f",sum_salary,sum_raise,sum_newsalary);
return 0;
}
}
else
{
if(salary>=0 &&salary<30000)
rate=7;
else if(salary>=30000 &&salary<=40000)
rate=5.5;
else if(salary>40000)
rate=4;
raise=rate*salary/100;
newsalary=salary+raise;
sum_salary=salary+sum_salary;
sum_raise=raise+sum_raise;
sum_newsalary=newsalary+sum_newsalary;
printf("Salary :%f rate :%f Raise :%f New Salary:
%f\n",salary,rate,raise,newsalary);
}
}
}
Hi can you please make sure that the following program will ask the user the following question before the salaries are entered? I am not sure where I would have to add this in for it ask that. If you can fit this requirement in the program that will be greatly appreciated. Thank you
Here is the question that the program should ask, "How many salaries do you want to enter?"
Source code of the program after changes made and its explanation are given below.Changes are shown in bold case.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Changes made
Program as per the requirement
#include <stdio.h>
int main()
{
int n,i=1;
float sum_salary=0,sum_raise=0,sum_newsalary=0;
printf("How many salaries do you want to enter? ");
scanf("%d",&n);
//while loop executes as long as i less than or equal to n
while(i<=n)
{
float salary,rate,raise,newsalary;
printf("Enter Salary %d: ",i);
scanf("%f",&salary);
if(salary>=0 &&salary<30000)
rate=7;
else if(salary>=30000 &&salary<=40000)
rate=5.5;
else if(salary>40000)
rate=4;
raise=rate*salary/100;
newsalary=salary+raise;
sum_salary=salary+sum_salary;
sum_raise=raise+sum_raise;
sum_newsalary=newsalary+sum_newsalary;
printf("Salary :%f rate :%f Raise :%f New Salary:
%f\n",salary,rate,raise,newsalary);
//updating i
i++;
}
printf("Total Salary :%f Total raise :%f Total New Salary:
%f",sum_salary,sum_raise,sum_newsalary);
return 0;
}
Screen shot of the code
Screen shot of the output