Objective:
- Functions in C
Activity 1:
Write C program
that will ask the user to enter the width and length of a rectangle and then display
the rectangle’s area. The program calls the following functions, which have not
been written:
- getLength: This
function should ask the user to enter the rectangle’s length and then return
that value as a double .
- getWidth: This
function should ask the user to enter the rectangle’s width and then return
that value as a double .
- getArea: This
function should accept the rectangle’s length and width as arguments and return
the rectangle’s area. The area is calculated by multiplying the length by the
width.
- displayData: This function should accept the rectangle’s length, width, and area as arguments and display them in an appropriate message on the screen.
Solution:
#include <stdio.h>
double getLength()
{
double length;
printf("Enter Length of Rectangle: \n");
scanf("%lf", &length);
return length;
}
double getWidth()
{
double width;
printf("Enter Width of Rectangle: \n");
scanf("%lf", &width);
return width;
}
double getArea(double i, double j)
{
double area;
area = i * j;
return area;
}
void displayData(double A)
{
printf("Area of Rectangle is: %.2lf\n", A);
}
int main()
{
double x, y, area;
x = getLength();
y = getWidth();
area = getArea(x, y);
displayData(area);
return 0;
}
Activity 2:
Write a
function called displayShape that
accepts size and shapetype as parameters and displays a square or a triangle of
the required size.
So e.g. if
the function is called with the following parameters:
displayShape(‘S’, 5); a square of
size 5 is displayed as given
1 |
2 |
3 |
4 |
5 |
2 |
4 |
6 |
8 |
10 |
3 |
6 |
9 |
12 |
15 |
4 |
8 |
12 |
16 |
20 |
5 |
10 |
15 |
20 |
25 |
displayShape(‘T’, 6); a triangle
of size 6 is displayed as given
1
1
1
1
2 1
1
3 3 1
1
4 6 4 1
1
5 10
10 5 1
Solution:
#include <stdio.h>
void print_square(int Size)
{
int num, i, j, res;
for (i = 1; i <= Size; i++)
{
num = i;
for (j = 1; j <= Size; j++)
{
res = num * j;
printf("%d\t", res);
}
printf("\n");
}
printf("\n");
}
void print_triangle(int Size)
{
int coef = 1;
for (int i = 0; i < Size; i++)
{
for (int space = 1; space <= Size - i; space++)
{
printf(" ");
}
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{
coef = 1;
}
else
{
coef = coef * (i - j + 1) / j;
}
printf("%d ", coef);
}
printf("\n");
}
}
int main()
{
char choice2 = 'y';
do {
int size;
char choice;
printf("Enter Size of Shape: \n");
scanf("%d", &size);
printf("Enter s for Square-shape or t for triangle-shape: \n");
scanf(" %c", &choice);
if (choice == 's')
{
print_square(size);
}
if (choice == 't')
{
print_triangle(size);
}
printf("If you want to Repeat then press y. \n");
scanf(" %c", &choice2);
} while (choice2 == 'y');
return 0;
}
Activity 3:
Write a program that converts a binary number into decimal number.
Solution:
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int decimalConverter(int num)
{
int dec = 0, i = 0, rem;
bool possible = true;
while (num != 0)
{
rem = num % 10;
num /= 10;
if (rem == 0 || rem == 1)
{
dec += rem * (int)pow(2, i);
++i;
}
else
{
printf("Conversion not possible: it is not a binary number.\n");
possible = false;
break;
}
}
if (possible == true)
{
return dec;
}
return -1; // Return a negative value to indicate the conversion failed.
}
int main()
{
int n;
printf("Enter a Binary Number: ");
scanf("%d", &n);
printf("Decimal Number: %d\n", decimalConverter(n));
return 0;
}