• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Exam (elaborations)

COMP1170 Lab Exercise 02

Document preview thumbnail
Preview 2 out of 5 pages

COMP1170 Lab Exercise 02 Questions and Answers Task 1: Selection structures: if statement Task 2: Using only the techniques you learned in this chapter, write a program that calculate the squares and cubes of the number from 0 to 10 and uses tabs to print the following table of values: number sequare cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 Reference Code: #include stdio.h int main(void) { // initialization int i; printf("NumbertSquarestCubesn"); // iterate i for ten times for (i=0; i=10; i++){ printf("%dt%dt%dn", i, i*i, i*i*i); } return 0; } Note: we use a repetition sturcure by for statement. Task 3: Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Hints: 1. using floating-point numbers %f instead of %d to print out the result. 2. float pi; pi = 3.14156. 3. diameter = 2*radius; circumference = 2*pi*radius; area = pi*radius*radius

Content preview

COMP1170 Lab Exercise 02

Questions and Answers

Task 1: Selection structures: if statement

Task 2: Using only the techniques you learned in this chapter, write a
program that calculate the squares and cubes of the number from 0 to
10 and uses tabs to print the following table of values:
number sequare cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

Reference Code:

#include <stdio.h>

int main(void)
{
// initialization
int i;

printf("Number\tSquares\tCubes\n");

// iterate i for ten times
for (i=0; i<=10; i++){
printf("%d\t%d\t%d\n", i, i*i, i*i*i);
}

return 0;
}

Note: we use a repetition sturcure by for statement.

, Task 3: Write a program that reads in the radius of a circle and prints
the circle’s diameter, circumference and area.

Hints:
1. using floating-point numbers %f instead of %d to print out the result.
2. float pi; pi = 3.14156.
3. diameter = 2*radius; circumference = 2*pi*radius; area = pi*radius*radius.

Reference Code:

#include <stdio.h>

int main(void)
{
// initialization
float radius, diameter, circumference, area;
float pi = 3.14156;

// input radius
printf("Please enter the radius:\n");
scanf("%f", &radius);

// calculate diameter, circumference and area
diameter = 2 * radius;
circumference = 2 * radius * pi;
area = radius * radius * pi;

// output and termination
printf("Diameter is: %f\n", diameter);
printf("Circumference is: %f\n", circumference);
printf("Area is: %f\n", area);

// return the value of main function
return 0;
}



Task 4: Write a program that asks the users to enter two numbers, and
obtains the two numbers from the user and prints the sum, product,
difference, quotient and remainder of the two numbers.

Reference Code:
#include <stdio.h>

int main(void)
{
// initialization
int num1, num2;

Document information

Uploaded on
August 3, 2024
Number of pages
5
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$14.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
StudyCenter1
4.3
(29)
Sold
232
Followers
91
Items
3811
Last sold
1 month ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions