CS-114 FUNDAMENTALS OF PROGRAMMING
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:
a= p(1+r )n
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
Code:
//program to find the amount on deposit
#include<stdio.h>
#include<math.h> //this library has been included in this program for power function
int main() {
int n; //n is the number of years
int x = 0; //x is the counter
float p = 0.0; //p is the original amount invested (i.e., the principal)
double a = 0.0; //a is the amount on deposit at the end of the nth year
float r = 0.0; //r is the annual interest rate
printf("Enter the principal amount:Rs.");
scanf_s("%f", &p); //asks the user to enter principal amount
printf("Enter the %% annual interest rate:");
scanf_s("%f", &r); //asks the user to enter annual interest rate
printf("Enter the number of years: ");
scanf_s("%d", &n); //asks the user to enter the number of years
while (x <= n) {
a = (p) * pow((1 + (r / 100)), x); //formula for amount on deposit
printf("The amount on deposit at the end of the %dth year is %.2lf\n", x,
a); //prints the amount on deposit at the end of the nth year
x++;
}
return 0;
Output:
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.
Code:
//program to find the sum of sequence of integers
#include<stdio.h>
int main() {
//initialization
int x;
int num;
int i = 0;
int sum = 0;
printf("Enter the number of integers to be added: ");
scanf_s("%d", &x); //asks the user to enter the number of integers to be added
printf("Enter the integers:");
while (i < x) //while loop for adding a sequence of integers
{
scanf_s("%d", &num); //asks the user to enter the number
sum =sum + num; //calculates sum
i++;
}
printf("The sum of %d integers is %d", x, sum); //prints the sum
return 0;
Output:
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.
Code:
//program to calculate the average of several integers with the condition that 9999 is
the last integer and it won't be in the calculation of average
#include<stdio.h>
int main() {
int num; //integers
float avg = 0.0;
int i = 0; //i is the loop counter
int sum = 0;
printf("Enter the integers(9999 to break): "); //prompt
for (i=0;1; i++) //this loop will be used to get the integers and calculate their
sum
{
scanf_s("%d", &num); //asks the user to enter the integers
if (num == 9999) //loop will terminate when the user enters 9999
{
break;
}
sum = sum + num; //calculates sum
avg = ((float)sum )/ i; //formula of average
//the technique of typecasting has been used to get the exact value of average
otherwise we'll get a rounded off value
printf("Average of the entered numbers is %.4f",avg); //prints average up to 4
decimal places
return 0;
}
Output:
Question 4:
Write a program that calculates and prints the sum of the even integers from 2
to 30.
Code:
//program to calculate the sum of even numbers from 2 to 30
#include<stdio.h>
int main() {
//initializing variables
int x = 0;
int sum = 0;
for (x = 0; x <= 30; x += 2) //for loop to find the sum
{
sum = sum + x; //calculates sum
}
printf("The sum of even numbers from 2 to 30 is %d", sum); //sum
return 0;
}
Output:
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.]
Code:
//program to calculate the weekly salary of different employees in a company
#include<stdio.h>
int main() {
//initializing variables
int paycode, hours, overt, weeklysales, npieces;
float manager_s, hourlywages, hourlyw_s = 0.0, totalhw_s, overhw_s =0.0,
commissionw_s, piecep, pieceworker_s;
printf("The pay codes for different employees are:\n1 for Manager2 for Hourly
Workers\n3 for Commission Workers\n4 for Pieceworkers\n"); //prompt
printf("Enter the pay code: ");
scanf_s("%d", &paycode); //asks the user to enter paycode
switch (paycode) //switch statement whose controlling expression is paycode begins
here
{
case 1:
//calculates the weekly salary of manager
{
printf("Enter manager's salary: $");
scanf_s("%f", &manager_s);
printf("The weekly salary of manager is $%.2f", manager_s); //prints weekly
salary of manager
break;
}
case 2:
{
//calculates weekly salary of an hourly worker
printf("Enter the hourly wage of an hourly worker: $");
scanf_s("%f", &hourlywages); //asks the user to enter the amount received
by the worker for one hour
printf("Enter the number of hours that an hourly worker has worked for: ");
scanf_s("%d", &hours); //asks the user to enter the number of hours worker
has worked
//if else to see the overtime of the worker
if (hours > 40)
overt = hours - 40;
else
{
overt = 0;
//checks different conditions to calculate the weekly salary of the worker
if (hours <= 40) {
hourlyw_s = hours * hourlywages;
totalhw_s = hourlyw_s;
else
{
hourlyw_s = 40 * hourlywages;
overhw_s = 1.5 * hourlyw_s * (float)overt;
totalhw_s = overhw_s + hourlyw_s;
}
printf("The over hours weekly pay of the Hourly Worker is: $%.2f\n",
overhw_s); //prints the salary for overtime
printf("The total weekly pay of the Hourly Worker is: $%.2f", totalhw_s);
//prints the weekly salary
break;
}
case 3:
{
//calculates the salary of a commission worker
printf("Enter weekly sales: ");
scanf_s("%d", &weeklysales); //asks the user to enter weekly sales
commissionw_s = 250 + (0.057 * weeklysales); //formula for the salary of
commission worker's salary
printf("The weekly salary of Commission Worker is: $%.2f", commissionw_s);
//prints the weekly salary of commission worker
break;
}
case 4:
{
//prints the weekly salary of pieceworker
printf("Amount earned per a piece: $");
scanf_s("%f", &piecep); //asks the user to enter the profit per a piece
printf("Enter the number of pieces sold:");
scanf_s("%d", &npieces); //asks the user to enter the number of sold
pieces
pieceworker_s = piecep * npieces; //calculates the weekly salary of a
pieceworker
printf("The weekly salary of a Pieceworker is $%.2f", pieceworker_s);
//prints the weekly salary of a pieceworker
break;
}
default: //when user enters a paycode other than 1,2,3 and 4
printf("\t\t\t\t"); //tabs are being used for formatting
printf("*****************INVALID PAYCODE******************\n"); //prompt
break;
}
return 0;
}
Output:
Case 1: Manager’s Weekly Salary
Case 2: Hourly Worker’s Weekly Salary
Without overtime
With overtime
Case 3: Commission Worker’s Weekly Salary
Case 4: Pieceworker’s Weekly Salary
Case 5: When the user enters a paycode other 1,2,3 and 4