0% found this document useful (0 votes)
6 views

programming exercise 4 documentation

Uploaded by

Kondwani Nyanga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

programming exercise 4 documentation

Uploaded by

Kondwani Nyanga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

National Institute of Public Administration

Course Code: BC2010


Course Name: Programming with C
Student ID: 2023001588
Student Name: Kondwani Nyanga

Title: Exercise 4
Due Date: 2024
TABLE OF CONTENT
INTRODUCTION...........................................................................................................................................3
Question 1: Write and test a function that takes two arguments radius and height and returns surface
area of the cylinder.....................................................................................................................................4
Question 2: Write and test a function that takes two arguments a and b and prints multiplication table..5
INTRODUCTION

The goal of this project is to write two C programs that illustrate basic programming ideas such
input/output operations, loops, functions, and conditional checks. The first application uses the
mathematical formula 2 π r 2 +2 πrh to compute the surface area of a cylinder depending on user-
provided dimensions. For two integers that are also supplied by the user, the second software
creates multiplication tables and presents them in an understandable, organized manner. When
taken as a whole, these exercises demonstrate the modularity and logical flow of C
programming, demonstrating how functions may be utilized to separate computations and
improve code reuse. This assignment reinforces the value of clear, organized, and user-friendly
output while providing a hands-on introduction to the principles of C programming.
Question 1: Write and test a function that takes two arguments radius
and height and returns surface area of the cylinder.
Aim:

To develop a C program that calculates the surface area of a cylinder based on user-inputted values for
the radius and height. The program will utilize the surface area formula 2 π r 2 +2 πrh and display the
result with the input values.

Task Description:

1. Prompt the user to input the radius and height of the cylinder.
2. Use the formula for the surface area of a cylinder:
2
2 π r +2 πrh
Where r is the radius and h is the height.
3. Calculate the surface area using the provided formula.
4. Display the calculated surface area along with the input values for radius and height in a clear
and formatted output.

Method/Algorithm:

 Start
 Declare variables:
o radius (double) for the radius of the cylinder.
o height (double) for the height of the cylinder.

 surface_area (double) to store the calculated surface area.


 Display prompt: “Input Radius: “
 Get user input for radius using scanf.
 Display prompt: “Input Height: “
 Get user input for height using scanf.
 Call the function cylinder_surface_area(radius, height):
Inside the function, apply the formula:
2
Surface Area=2 π r + 2 πrh

 Return the calculated surface area.


 Store the returned value in surface_area.
 Display the result: "The surface area of the cylinder with radius X and height Y is: Z",
where X is the radius, Y is the height, and Z is the calculated surface area.
 End.

Program Code:
Conclusion:

Using the radius and height entered by the user, this C program successfully determines the
surface area of a cylinder. The program calculates the surface area using the mathematical
formula 2 π r 2 +2 πrh and presents the result to the user in a structured way. The application
shows how to employ mathematical calculations, user interaction, and fundamental input/output
functions in C. In addition to presenting chances for additional enhancements, like input
validation and error management, to guarantee more reliable user interaction, it offers a
straightforward yet efficient method for determining the geometric features of a cylinder.

Question 2: Write and test a function that takes two arguments a and b
and prints multiplication table.
Aim:

To develop computer software in C that shows the user's multiplication tables for two integer
inputs between 1 and 10. Using functions, loops, and fundamental input/output operations, this
program seeks to illustrate how to utilize C programming.

Task Description:

Create a C program that generates multiplication tables for two user-provided integers. The
program should:

1. Prompt the user to enter two integers.


2. Display the multiplication table for each number, showing results from 1 to 10 for each
table.
3. Use a separate function to handle the display of each multiplication table to promote
modularity.
4. Format the output so that each table is easy to read and separated by a blank line.

Method/Algorithm:

 Start
 Declare two integer variables a and b to store the user’s input numbers.
 Prompt the user to enter the first integer (a).
 Read and store the value in variable a.
 Prompt the user to enter the second integer (b).
 Read and store the value in variable b.
 Define a function print_multiplication_table:
o This function takes one integer parameter, num, Inside the function:
o Use a for loop with a counter variable j that iterates from 1 to 10.
o In each iteration, calculate the product of num and j (i.e., num * j).
o Print the result in the format: num x j = product.

 End function.
 Call the function:
 Call print_multiplication_table(a) to print the multiplication table for the first
number.
 Print a blank line to separate the tables for readability.
 Call print_multiplication_table(b) to print the multiplication table for the second
number.
 End Program:
 Return 0 to signify successful completion.

Program Code:
Conclusion:

The creation and display of multiplication tables for two user-defined numbers between 1 and 10 are
successfully demonstrated by this C application. The program highlights modularity by dividing the table
display logic into a separate function and demonstrates basic C programming ideas through the use of
functions, loops, and basic input/output operations. By ensuring that only integers falling within the
designated range are accepted, input validation improves the program's dependability. All things
considered, this solution offers a methodical and transparent way to deal with loops and functions,
making it a useful illustration for novices learning C programming.

You might also like