0% found this document useful (0 votes)
50 views3 pages

Lab 5 - Solution

The document contains code for 6 questions that demonstrate different C programming concepts. Question 1 calculates the area of an ellipse by taking the product of two radii and pi. Question 2 calculates integer powers by using a for loop to repeatedly multiply a base number by itself. Question 3 converts between Celsius and Fahrenheit temperatures by defining separate functions. Question 4 changes the case of a character from upper to lower or vice versa. Questions 5 and 6 calculate the area and circumference of a circle by defining separate functions.

Uploaded by

Nawaf Alshareef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Lab 5 - Solution

The document contains code for 6 questions that demonstrate different C programming concepts. Question 1 calculates the area of an ellipse by taking the product of two radii and pi. Question 2 calculates integer powers by using a for loop to repeatedly multiply a base number by itself. Question 3 converts between Celsius and Fahrenheit temperatures by defining separate functions. Question 4 changes the case of a character from upper to lower or vice versa. Questions 5 and 6 calculate the area and circumference of a circle by defining separate functions.

Uploaded by

Nawaf Alshareef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

q1

---------------------------------------
#include <stdio.h>
double areaOfEllipse(double r1, double r2) {
return 3.14*r1*r2;
}
void main() {
double r1,r2;
printf("Enter Radiuses of the ellipse");
int i;
for(i=1; i<5; i++)
scanf("%lf%lf", &r1,&r2);
printf("Area of Ellipse:%.2f\n",areaOfEllipse(r1,r2));
}

q2
--------------------------------------
*//--------------Q2 ---------------
#include <stdio.h>
int integerPower(int b, int e) {
int result=1;
int i;
for(i=1; i<=e; i++)
result=result*b;
return result;
}
void main() {
int base,exp;
printf("Enter Integer Base & Exponent:");
scanf("%d%d",&base,&exp);
int result=integerPower(base,exp);
printf("%d to the power %d = %d\n", base,exp,result);

}
q3
---------------------------------------------------------
//-------------Q3---------------------
#include <stdio.h>
float fahrenheitToCelcius(int degrees);
float celciusToFahrenheit(int degrees);
void main() {
int i;
for (i=0;i<=9;i++){
printf("%5d Celcius equals to %3.1f
fahrenheit.\n",i,celciusToFahrenheit(i));
}
printf("\n");
for (i=32;i<=41;i++){
printf("%5d Fahrenheit equals to %3.1f celcius.\n",i,fahrenheitToCelcius(i));
}
}
float fahrenheitToCelcius(int degrees){
return (degrees-32)/1.8;
}
float celciusToFahrenheit(int degrees){
return degrees*1.8+32;
}

q4
------------------------------------------------------------------
//-------------Q4-------------------
#include <stdio.h>
char changeCase(char ch) {
if (ch >= 'A' && ch <= 'Z')
ch = ch + 32;
else if (ch >= 'a' && ch <= 'z')
ch = ch - 32;
return ch;
}

void main () {
char alpha;

printf("Input a character\n");
scanf("%c",&alpha);
printf("%c\n", changeCase(alpha));

}
q5
-----------------------------------------------------------
//--------------Q5----------------
#include <stdio.h>
const float PI = 3.14;
float area(float radius);
float circum(float radius);
void main() {
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Area : %.2f\n", area(radius));
printf("Circumference: %.2f\n", circum(radius));
}
float area(float radius) {
return PI * radius * radius;
}
float circum(float radius) {
return 2 * PI * radius;
}
q6
-----------------------------------------------------------------
//------------Q6----------------------
#include <stdio.h>
const float PI = 3.1415927;
float area(float radius);
float circum(float radius);
void main() {
int n;
printf("Enter How many radiuses to calculate: ");
scanf("%d", &n);
int i;
for(i=0; i<=n; i++)
{
printf("%d\t %.2f\t %.2f\n", i,area(i),circum(i));
}
}
float area(float radius) {
return PI * radius * radius;
}
float circum(float radius) {
return 2 * PI * radius;
}

You might also like