0% found this document useful (0 votes)
117 views5 pages

Assignment 4 Fop

The document contains 5 questions and solutions regarding programming in C. Question 1 involves writing a program to calculate the amount in a savings account over 10 years using a while loop. Question 2 involves writing a program to sum a sequence of integers read from the user. Question 3 calculates the average of integers entered by the user until a sentinel value is input. Question 4 calculates the sum of even integers between 2 and 30. Question 5 involves writing a program to calculate weekly pay for different types of employees using a switch statement based on their pay code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views5 pages

Assignment 4 Fop

The document contains 5 questions and solutions regarding programming in C. Question 1 involves writing a program to calculate the amount in a savings account over 10 years using a while loop. Question 2 involves writing a program to sum a sequence of integers read from the user. Question 3 calculates the average of integers entered by the user until a sentinel value is input. Question 4 calculates the sum of even integers between 2 and 30. Question 5 involves writing a program to calculate weekly pay for different types of employees using a switch statement based on their pay code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT 4

Question 1:
A person invests $1000.00 in a savings account yielding 5% interest. Assuming that all interest is left on
deposit in the account, using while loop calculate and print the amount of money in the account at the
end of each year for 10 years. Use the following formula for determining these amounts:

𝑎 = 𝑝 (1 + 𝑟) 𝑛

Where

p is the original amount invested (i.e., the principal)


r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.

Solution:
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
//Declaring variables
int n, p, r, a;

//Initializing values

p = 1000.0f; //Principal amount


r = 0.05f; //Interest rate
n = 1; //Number of years

//Using loop for initializing the number of years


while(n <= 10)
{
a = p * (pow (1 + 0.05, n));
printf("\nAmount on deposit at the end of the %d year is $%d.", n, a);
n++;
}

//Exit the program


return 0;
}
Question 2:
Write a program that sums a sequence of integers. Assume that the first integer read with scanf
specifies the number of values remaining to be entered. Your program should read only one value each
time scanf is executed. A typical input sequence might be

5 100 200 300 400 500


where the 5 indicates that the subsequent five values are to be summed.

Solution:

#include <stdio.h>
#include <math.h>

int main()
{
//Declaring variables
int x, y, Value, Sum = 0;

//Input and storage of "y"


printf("Enter the numbers of integers to be added: ");
scanf_s("%d", &y);

//Loop for calculating "x"


for ( x = 1; x <= y; x++)
{
printf("Enter integer %d: ", x);
scanf_s("%d", &Value); //Storing value of integer entered

Sum += Value;//Adding the values stored


}

//Calculating sum
printf("\nThe Sum of integers is %d. ", Sum);

//Exit the program


return 0;
}

Question 3:
Write a program that calculates and prints the average of several integers. Assume the last value read
with scanf is the sentinel 9999. A typical input sequence might be

10 8 11 7 9 9999

indicating that the average of all the values preceding 9999 is to be calculated.

Solution:
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
//Declaring variables
int i, count = 0, sum = 0;
float Average;

//Using loop to calculate integers


for (i = 0; i < 9999; i++)
{
printf("Enter an Integer (Press 9999 to exit): "); //Input and Storage of i
scanf_s("%d", &i);

//if else statement


if (i == 9999)
{
break;

}
else
{
sum += i;
}
count++;
}
//Calculating Average of integers
Average = sum / count;

printf("The Average of integers is %f.", Average);

//Exit the program


return 0;
}

Question 4:
Write a program that calculates and prints the sum of the even integers from 2 to 30.

Solution:
#include <stdio.h>
#include <math.h>

int main()
{
//Declaring variables
int i, Sum = 0;

//Using Loop to Calculate Sum


for (i = 2; i <= 30; i += 2)
{
Sum += i;
}

//Printing the sum of even numbers


printf("The Sum of Even numbers between 2 and 30 is %d.\n", Sum);

//Exit the program


return 0;
}

Question 5:
A company pays its employees as managers (who receive a fixed weekly salary), hourly workers (who
receive a fixed hourly wage for up to the first 40 hours they work and “time-and-a-half”—i.e., 1.5 times
their hourly wage—for overtime hours worked), commission workers (who receive $250 plus 5.7% of
their gross weekly sales), or pieceworkers (who receive a fixed amount of money for each of the items
they produce—each pieceworker in this company works on only one type of item). Write a program to
compute the weekly pay for each employee. You do not know the number of employees in advance.
Each type of employee has its own pay code: Managers have pay code 1, hourly workers have code 2,
commission workers have code 3 and pieceworkers have code 4. Use a switch to compute each
employee’s pay based on that employee’s pay code. Within the switch, prompt the user (i.e., the payroll
clerk) to enter the appropriate facts your program needs to calculate each employee’s pay based on that
employee’s pay code. [Note: You can input values of type double using the conversion specifier %lf with
scanf.]

Solution:
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
//Declaring variables
int Paycode = 0, hours, items;
double Weekly_salary, Hourly_salary, Gross_sales, Pay_per_item, Total_pay;

//input and Storage of Paycode


printf("Enter Paycode(-1 to break): ");
scanf_s("%d/n", &Paycode);

//Loop to calculate salary


while (Paycode != -1)
{
//Switch statement to calculate paycode
switch (Paycode)
{
case 1:
//Salary of manager
printf("You have selected Manager.\nEnter Manager Salary:$");
scanf_s("%lf",&Weekly_salary);
Total_pay = Weekly_salary;
break;

case 2:
//Salary of Hourly Worker
printf("You have selected Hourly Worker.\nEnter Hourly Salary:$");
scanf_s("%lf", &Hourly_salary);

printf("Enter the number of hours worked: ");


scanf_s("%d", &hours);

//If Else statement


if (hours <= 40)
{
Total_pay = hours * Hourly_salary;
}
else
{
Total_pay = (40 * Hourly_salary) + (hours - 40) *
(Hourly_salary * 1.5);
}
break;

case 3:
//Salary of Commission Worker
printf("You have selected Commission Worker.\nEnter Gross Sales:$");
scanf_s("%lf", &Gross_sales);
Total_pay = 250 + Gross_sales * 0.057;
break;

case 4:
//Salary of Pieceworker
printf("You have selected Pieceworker.\nEnter Number of Items
Produced by Pieceworker:");
scanf_s("%d", &items);

printf("Enter Pay for each Item:$");


scanf_s("%lf", &Pay_per_item);

Total_pay = items*Pay_per_item;
break;

default:
//if paycode is invalid
printf("You have entered an INVALID PAYCODE!\n");
Total_pay = 0;
break;
}
//Printing of Paycode
printf("The Total Pay of the Worker is:$%.2lf\n", Total_pay);
printf("\nEnter Paycode for next Employee(-1 to Exit): ");
scanf_s("%d", &Paycode);
}

//Exit the program


return 0;
}

You might also like