13 Programming Questions
13 Programming Questions
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);
break;
case 'T':
case 't': printf ("\nPlease enter the length of the base of the
triangle...\n");
scanf ("%lf", &base);
break;
case 'C':
case 'c': printf ("\nPlease enter the length of the radius of the
circle...\n");
scanf ("%lf", & radius);
break;
default:
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;
hundred = t1/100;
t2 = t1%100;
ten = t2/10;
one = t2%10;
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;
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;
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;
if ( F < 50)
{
newM = M * 1.03;
newA = A * 1.04;
}
else
{
newM = M * 1.015;
newA = A * 1.02;
}
#include <stdio.h>
#include <math.h>
void main()
{
int a, b, c;
double temp, ans1, ans2;
if (temp >= 0)
{
ans1 = (-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;
else
else
else
if (sales >39999)
{
commission = sales * 0.14;
bonus = 2000;
}
else
printf ("\n\nENTER A NUMBER GREATER THAN 0
\n\n\n");
#include <stdio.h>
void main ()
{
int days, km;
double cost = 0; //initializes the variable cost to 0
char car;
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;
}
//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;
#include <stdio.h>
void main ()
{
double 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 ("\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;
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;
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;
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;
break;
break;
default:
printf ("\n\nINVALID DEPARTMENT CODE ENTERED\n\n");
break;
}