Question
Write a program in C language. PART 1: Use the GetInteger function get a number from the user Get the number from the user (n)
Write a program in C language.
PART 1:
Use the GetInteger function get a number from the user
Get the number from the user (n)
(For example if the user enters a 4, each loop below will repeat 4 times)
Calculate the sum of the squares of the first (n) numbers using a for loop.
For example, if the user entered a 4, you would calculate 1*1 + 2*2 + 3*3 + 4*4 and print the total onto the screen
Calculate the product of the squares of the first (n) numbers using a while loop.
For example, if the user entered a 4, you would calculate (1*1) * (2*2) *(3*3) * (4*4) and print the total onto the screen
**Make sure use the both loops: for loops for sum of total and while loop for product total**
PART 2:
Write a small program that calculates the perimeter and area of a square using pass by reference function arguments
//asks and gets an integer from the user
int GetInteger (void);
//takes one integer argument and two integer pointer arguments //1. calculate the area of the square and store the result in areaPtr //2. calculate the perimeter of the square and store the result in perimeterPtr void CalculateBothSquare(int side, int*areaPtr, int * perimeterPtr );
//takes one integer argument and two double pointer arguments //1. calculate the area of the circle and store the result in areaCirPtr (use 3.141592) //2. calculate the circumference of the circle and store the result in circumPtr (use 3.141592)
void CalculateBothCircle (int radius, double*areaCirPtr, double * circumPtr ); Call all the functions from main. Note: No calculations may be done in the main function. Code should be well commented!
Main function (hints):
Declare variables: number, sumTotal, productTotal, radius, length, squareArea, squarePerimeter, circleArea, circleCir
Dont forget to initialize sumTotal and productTotal
Call GetInteger to get a number from the user for the loops
Calculate and print the sumTotal
Calculate and print the productTotal
PART 2
6. Call GetInteger to get the radius of the circle
Pass the radius, address of circleArea, and address of circleCir to the CalculateBothCircle function
Print the area and the circumference onto the screen
Call GetInteger to get the length of the side of the square
Pass the length, address of squareArea, and address of squarePerimeter to the CalculateBothSquare function
Print the area and the perimeter onto the screen
NOTE: The area and perimeter should be printed twice, once from inside the functions and then a second time in the main function (See sample output below)
CalculateBothCircle function logic (and hints):
Calculate the area and store the result in the value at areaCirPtr
Print the value at areaPtr onto the screen
Calculate the circumference and store the result in the value at circumPtr
Print the value at circumPtr onto the screen
//the function has a void return type
CalculateBothSquare function logic (and hints):
Calculate the area and store the result in the value at areaPtr
Print the value at areaPtr onto the screen
Calculate the perimeter and store the result in the value at perimeterPtr
Print the value at perimeterPtr onto the screen
//the function has a void return type
Sample output:
PART 1:
Enter an integer: 4
The sum total is 30 and the product total is 576
PART 2:
You will be entering the radius:
Enter an integer: 6
Inside the CalculateBothCircle function
The calculation results inside the CalculateBothCircle function:
The area is 113.097 and the circumference is 37.699
The calculation results back inside the main function:
The area is 113.097 and the circumference is 37.699
You will be entering the length of the side of a square:
Enter an integer: 7
Inside the CalculateBothSquare function
The calculation results inside the CalculateBothSquare function:
The area is 49 and the perimeter is 28
The calculation results back inside the main function:
The area is 49 and the perimeter is 28
Press any key to continue . . .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started