Uploaded by Shongile Nwa mavone

Test 4

advertisement
test 4
#include <stdio.h>
//Function Prototypes
void Display();
float PowerFunction (int Base, int Power);
float CircleCircumference (int Radius);
float TriangleSurface (int Base, int Height);
int FactorialFunction (int N);
int main (void)
{
int choice, Base, Power, Radius, Height, N;
do{
Display();
scanf("%d",&choice);
switch (choice)
{
case 1: printf("Base:");
scanf("%d",&Base);
printf("Power:");
scanf("%d",&Power);
printf("Answer=%.4f\n\n",PowerFunction(Base,Power));
break;
case 2: printf("Radius:");
scanf("%d",&Radius);
printf("Answer=%.2f\n\n",CircleCircumference(Radius));
break;
case 3: printf("Base:");
scanf("%d",&Base);
printf("Height:");
scanf("%d",&Height);
printf("Answer=%.2f\n\n",TriangleSurface(Base,Height));
break;
case 4: printf("N:");
scanf("%d",&N);
printf("Answer=%d\n\n",FactorialFunction(N));
break;
case 5: printf("Bye Bye\n\n");
break;
default:printf("Invallid Input\n\n");
break;
}
} while (choice != 5);
return 0;
}
//Functions
void Display()
{
printf("Welcome to Advance Calculator:\n");
printf("1. Power Function\n");
printf("2. Circle Circumference\n");
printf("3. Triangle Surface\n");
printf("4. Factorial Function\n");
printf("5. Exit\n");
printf("Choice:");
}
float PowerFunction (int Base, int Power)
{
int i;
int answer = 1;
if (Power == 0)
return 1;
else if (Power < 0)
{
for (i= 0; i>Power+1; i--)
answer = answer*Base;
return 1.0/(answer*Base);
}
else if (Power > 0)
{
for (i= 0; i<Power-1; i++)
answer = answer*Base;
return answer*Base;
}
}
float CircleCircumference (int Radius)
{
return 3.14 * 2 * Radius;
}
float TriangleSurface (int Base, int Height)
{
return (Base * Height)/2.0;
}
int FactorialFunction (int N)
{
int i;
int answer = 1;
for (i = 1; i<=N; i++)
answer = i*answer;
return answer;
}
Download