//A program which allows the user to choose the figure whose area is to be
determined.
//There are three figures. Circle, Rectangle and Triangle.
//Upon choosing the switch statement is used to request the dimesions to
determine the area.
#include <stdio.h>
#include <math.h>
void main()
{
char fig;
double length, width, radius, base, height;
double area;
printf ("Which shape would you like to find the area for?\n");
printf ("Enter T for triangles... C for circles... R for
rectangles...\n");
scanf ("%c", &fig);
switch(fig)
{
case 'R':
case 'r': printf ("\nPlease enter the length of the
rectangle...\n");
scanf ("%lf", &length);
printf ("\nPlease enter the width of the
rectangle...\n");
scanf ("%lf", &width);
area = length * width;
printf ("\nArea of the shape is: %5.2f units\n\n",
area);
break;
case 'T':
case 't': printf ("\nPlease enter the length of the base of the
triangle...\n");
scanf ("%lf", &base);
printf ("\nPlease enter the length of the height of
the triangle...\n");
scanf ("%lf", &height);
area = 0.5 * (base * height);
printf ("\nArea of the shape is: %5.2f units\n\n",
area);
break;
case 'C':
case 'c': printf ("\nPlease enter the length of the radius of the
circle...\n");
scanf ("%lf", & radius);
area = 3.15 * pow(radius,2);
printf ("\nArea of the shape is: %5.2f units\n\n",
area);
break;
default:
printf ("\nINCORRECT FIGURE\n\n");
break;
}
//A program which allows the user to enter a number equal to four digits, it
then displays the number into its respective units(thousands
//hundreds etc...) If the number of digits exceed 4 then an error message is
displayed.
#include <stdio.h>
void main ()
{
int n, thousand, hundred, ten, one;
int t1, t2;
printf ("Please enter a number (eg. 1234)...\n");
scanf ("%d" , &n);
if (n<10000 && n>0)
{
thousand = n/1000;
t1 = n%1000;
hundred = t1/100;
t2 = t1%100;
ten = t2/10;
one = t2%10;
printf("\nThousands digit = %d\nHundreds digit = %d\nTens digit =
%d\nOnes digit = %d\n\n", thousand, hundred, ten, one);
}
else
printf("\nINVALID INPUT\n\n");
}
//A program which allows the user to enter a number equal to four digits, it
then displays the number into its respective units(thousands
//hundreds etc...) If the number of digits exceed 4 then an error message is
displayed. It aslo shows the ammount of even and odd numbers entered.
#include <stdio.h>
void main ()
{
int n, thousand, hundred, ten, one;
int t1, t2, t3;
int odd =0, even=0;
printf ("Please enter a number (eg. 1234)...\n");
scanf ("%d" , &n);
if (n <=9999)
{
thousand = n/1000;
t1 = n%1000;
hundred = t1/100;
t2 = t1%100;
ten = t2/10;
t3 = t2%10;
one = t3;
if(thousand%2 == 1)
odd = odd + 1;
else
even = even +1;
if(hundred%2 == 1)
odd = odd +1;
else
even = even + 1;
if(ten%2 == 1)
odd = odd + 1;
else
even = even + 1;
if(one%2 ==1)
odd = odd + 1;
else
even = even +1;
printf("\nThousands digit = %d\nHundreds digit = %d\nTens digit =
%d\nOnes digit=%d", thousand, hundred, ten, one);
printf("\n\nNo. of Odd Numbers = %d", odd);
printf("\nNo. of Even Numbers = %d\n\n", even);
}
else
printf("\nINVALID INPUT\n\n");
}
//A program which allows the user to enter a persons final midtem and
assignment grade.
//If the person has less than 50% the program will execute a specific task, if
the percentage is greater than 50% another task will be executed.
#include <stdio.h>
#include <math.h>
void main ()
{
double F, M, A;
double newF, newM, newA;
printf ("Please enter the Final Grade: ");
scanf ("%lf", &F);
printf ("Please enter the Mid-Term Grade: ");
scanf ("%lf", &M);
printf ("Please enter the Assigment Grade: ");
scanf ("%lf", &A);
printf("\n");
if ( F < 50)
{
newM = M * 1.03;
newA = A * 1.04;
}
else
{
newM = M * 1.015;
newA = A * 1.02;
}
printf ("Please note the following changes:\n");
printf ("MID-TERM: %2.0f % to %2.1f % \nASSIGNMENT: %2.0f % to %2.1f
%.\n", M, newM, A, newA);
}
//A program to determine whether there are real or no real solutions to a
mathematical problem.
//It reads in the values associated with a, b, and c and computes weather or
not the discriminat is valid enough to get real solutions.
#include <stdio.h>
#include <math.h>
void main()
{
int a, b, c;
double temp, ans1, ans2;
printf("Please enter the coefficient of X^2...\n");
scanf ("%d",&a);
printf("\nPlease enter the coefficient of X...\n");
scanf ("%d",&b);
printf("\nPlease enter the constant...\n");
scanf ("%d",&c);
temp = powf(b,2) - (4*a*c);
if (temp >= 0)
{
ans1 = (-b + powf(temp,0.5)) / (2*a);
ans2 = (-b - powf(temp,0.5)) / (2*a);
printf ("\n\nThe solutions are real and there values are: %3.2f
and %3.2f respectively\n\n\n", ans1, ans2);
}
else
printf("\n\nThere are no real solutions that exist for this
problem\n\n\n");
}
//A program which allows you to input a salesman monthly sales and determines
the salesperson's commission and bonus based on a table of figures.
#include <stdio.h>
void main ()
{
double sales;
double commission, bonus;
printf ("Please enter the salesperson monthly sales...\n");
scanf ("%lf", &sales);
if (sales >=0 && sales <= 9999)
{
commission = sales * 0.08;
bonus = 700;
}
else
if (sales >9999 && sales <=19000)
{
commission = sales * 0.105;
bonus = 1200;
}
else
if (sales >19000 && sales <= 39999)
{
commission = sales * 0.12;
bonus = 1500;
}
else
if (sales >39999)
{
commission = sales * 0.14;
bonus = 2000;
}
else
printf ("\n\nENTER A NUMBER GREATER THAN 0
\n\n\n");
printf ("\n\nFor the monthly sale of $%8.2f \nThe commission is: $
%8.2f\nThe bonus is: $%8.2f\n\n\n", sales, commission, bonus);
}
//A program that allows the user to enter the type of car rented, the number
of days rented for and the number of kilometers driven.
//The program then calculates the total cost for the rented car using
predetermined formulas.
#include <stdio.h>
void main ()
{
int days, km;
double cost = 0; //initializes the variable cost to 0
char car;
printf ("Enter the type of car rented...\n");
scanf ("%c", &car);
printf ("\nEnter # of days...\n");
scanf ("%d", &days);
printf ("\nEnter # of km...\n");
scanf ("%d", &km);
if (car == 'H' || car == 'h') //this will cater for UPPERCASE and
lowercase letters
cost = 200 * days + 0.80 * km;
else
if (car == 'M' || car =='m') //begining of the nested
statements
cost = 180 * days + 0.70 * km;
else
if (car == 'V' || car == 'v') //an if does not
necessarily need to be associated with an else statement
cost = 190 * days + 0.85 * km;
if (cost != 0) //if there was a change in the initial variable cost do
this
printf ("\n\nTotal cost = $%3.2f\n\n\n", cost);
else //if not there is an error in the type of car rented
printf ("\n\nWE ARE SORRY BUT WE DONT OFFER THE CAR TYPE YOU HAVE
ENTERED FOR RENT...\n\n\n");
}
//A program that allows the user to enter a numeric grade and converts it to a
letter grade using a provided scheme.
#include <stdio.h>
void main()
{
int ngrade;
char lgrade;
printf ("Please enter the numeric mark...\n");
scanf ("%d", &ngrade);
if (ngrade >= 80 && ngrade <= 100)
lgrade = 'A';
else
if (ngrade >= 70 && ngrade <80)
lgrade = 'B';
else
if (ngrade >=60 && ngrade <70)
lgrade = 'C';
else
if (ngrade >=50 && ngrade <60)
lgrade = 'D';
else
if (ngrade <50)
lgrade = 'F';
printf ("\n\nThe mark you attained of %d is equivalent to a grade %c.
\n\n\n", ngrade, lgrade);
//The program crashes if a number is entered higher that 100
}
//A program which allows the user to enter their salary and computes the tax,
health-sucharge and NIS - which are dependant on certain conditions.
//If the salary exceeds $50000 then tax is computed at 35%, health surcharge
at 5% and NIS at 2%, otherwise tax is 28%, health surcharge is 3.5% and NIS is
1.5%
#include <stdio.h>
void main ()
{
double salary, tax, health, NIS;
printf ("Please enter your salary...\n$");
scanf ("%lf", & salary);
if (salary > 50000)
{
tax = salary * 0.35;
health = salary * 0.05;
NIS = salary * 0.02;
}
else
{
tax = salary * 0.28;
health = salary * 0.035;
NIS = salary * 0.015;
}
printf ("\n\nFor your salary of $%5.2f:\nTax = $%5.2f\nHealth Surcharge
= $%5.2f\nN.I.S. = $%5.2f\n\n\n", salary, tax, health, NIS);
}
//A program which allows the user to enter their salary and computes the tax -
which are dependant on certain conditions.
//It reads in Salary and Computes tax. If the salary is less than or equal to
$50000 then tax is a rate of 35%. At any other
//value tax is at a rate of 28%.
#include <stdio.h>
void main ()
{
double salary;
double tax;
printf ("Please enter your salary...\n");
scanf ("%lf", &salary);
if (salary > 50000)
tax = salary * .35;
else
tax = salary * .28;
printf ("\n\nThe tax for your salary at $%5.2f is: $%5.2f ... \n\n",
salary, tax);
}
//A program that reads in the salary of an employee and the department that
they work in and calculates the new salary that they earn
//based on a table that indicates the salary increases for different
department employees of the organization.
#include <stdio.h>
void main()
{
int DepCode, IniSal;
double NewSal=0, increase=0;
printf ("Enter the employee's salary...\n");
scanf ("%d", &IniSal);
printf ("\n\nCodes are as follows:\n\nAccounting = 2\nShipping =
5\nStores = 6");
printf ("\nSecurity = 8\nCorperate = 9\nProcurement = 10\nMaintenance =
11\n\n");
printf ("Enter the employee's department code...\n");
scanf ("%d", &DepCode);
if (DepCode == 5 || DepCode == 8 || DepCode == 11)
{
NewSal = IniSal * 1.025;
increase = 2.5;
}
else
if (DepCode == 2 || DepCode == 10)
{
NewSal = IniSal * 1.035;
increase = 3.5;
}
else
if (DepCode == 6)
{
NewSal = IniSal * 1.04;
increase = 4;
}
else
if (DepCode == 9)
{
NewSal = IniSal * 1.015;
increase = 1.5;
}
if (NewSal !=0)
{
printf ("\n\nIntial salary of employee = %d\n", IniSal);
printf ("New salary of employee = %3.2f\n", NewSal);
printf ("Increse in salary = %3.1f percent\n\n", increase);
}
else
printf ("\n\nINVALID DEPARTMENT CODE ENTERED\n\n");
}
//A program that reads in a number of grams and calculates the mailing price
of the parcel. The calculation is based on the following:
//$0.60 for the first 100 grams or part thereof plus
//$0.55 for the next 100 grams or part thereof plus
//$0.50 for the next 125 grams or part thereof plus
//$0.40 for the next 150 grams or part thereof plus
//$0.35 per 150 grams or part thereof for the reamining weight
#include <stdio.h>
void main()
{
int grams;
double mon1, mon2, mon3, mon4, mon5;
double cost;
printf ("Enter the weight of the parcel in grams...\n");
scanf ("%d", &grams);
if (grams>0 && grams<=100)
{
mon1 = grams * 0.60;
cost = mon1;
}
else
if (grams>100 && grams<=200)
{
mon1 = 100 * 0.60;
mon2 = (grams - 100)*0.55;
cost = mon1 + mon2;
}
else
if (grams>200 && grams<=325)
{
mon1 = 100 * 0.60;
mon2 = 100 * 0.55;
mon3 = (grams - 200)*0.50;
cost = mon1 + mon2 + mon3;
}
else
if (grams>325 && grams<=475)
{
mon1 = 100 * 0.60;
mon2 = 100 * 0.55;
mon3 = 125 * 0.50;
mon4 = (grams - 325)*0.40;
cost = mon1 + mon2 + mon3 + mon4;
}
else
if (grams>475)
{
mon1 = 100 * 0.60;
mon2 = 100 * 0.55;
mon3 = 125 * 0.50;
mon4 = 150 * 0.40;
mon5 = (grams - 475)*0.35;
cost = mon1 + mon2 + mon3 + mon4 + mon5;
}
if (grams<0)
printf ("\n\nINVALID WEIGHT ENTERED - ENTER A NUMBER GREATER THAN
ZERO\n\n");
else
printf ("\n\nThe cost to mail this parcel = %3.2f\n\n", cost);
}
//A program that reads in the salary of an employee and the department that
they work in and calculates the new salary that they earn
//based on a table that indicates the salary increases for different
department employees of the organization.
#include <stdio.h>
void main()
{
int DepCode, IniSal;
double NewSal=0, increase=0;
printf ("Enter the employee's salary...\n");
scanf ("%d", &IniSal);
printf ("\n\nCodes are as follows:\n\nAccounting = 2\nShipping =
5\nStores = 6");
printf ("\nSecurity = 8\nCorperate = 9\nProcurement = 10\nMaintenance =
11\n\n");
printf ("Enter the employee's department code...\n");
scanf ("%d", &DepCode);
switch (DepCode)
{
case 5:
case 8:
case 11: NewSal = IniSal * 1.025;
increase = 2.5;
printf ("\n\nIntial salary of employee = %d\n",
IniSal);
printf ("New salary of employee = %3.2f\n", NewSal);
printf ("Increse in salary = %3.1f percent\n\n",
increase);
break;
case 2:
case 10: NewSal = IniSal * 1.035;
increase = 3.5;
printf ("\n\nIntial salary of employee = %d\n",
IniSal);
printf ("New salary of employee = %3.2f\n", NewSal);
printf ("Increse in salary = %3.1f percent\n\n",
increase);
break;
case 6: NewSal = IniSal * 1.04;
increase = 4;
printf ("\n\nIntial salary of employee = %d\n",
IniSal);
printf ("New salary of employee = %3.2f\n", NewSal);
printf ("Increse in salary = %3.1f percent\n\n",
increase);
break;
case 9: NewSal = IniSal * 1.015;
increase = 1.5;
printf ("\n\nIntial salary of employee = %d\n",
IniSal);
printf ("New salary of employee = %3.2f\n", NewSal);
printf ("Increse in salary = %3.1f percent\n\n",
increase);
break;
default:
printf ("\n\nINVALID DEPARTMENT CODE ENTERED\n\n");
break;
}