0% found this document useful (0 votes)
18 views5 pages

Tutorial 2 - ANS Programming

This document contains C code solutions to programming problems posed in a tutorial for the course BETC1313 Programming Fundamentals. The problems include writing code to print sample outputs, performing mathematical operations, evaluating logical expressions, and writing programs to calculate the area and volume of a cylinder, convert between pounds and kilograms, and calculate a person's age based on their birth year. Flowcharts and C code are provided as solutions for each problem.

Uploaded by

nurfazlinda123
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)
18 views5 pages

Tutorial 2 - ANS Programming

This document contains C code solutions to programming problems posed in a tutorial for the course BETC1313 Programming Fundamentals. The problems include writing code to print sample outputs, performing mathematical operations, evaluating logical expressions, and writing programs to calculate the area and volume of a cylinder, convert between pounds and kilograms, and calculate a person's age based on their birth year. Flowcharts and C code are provided as solutions for each problem.

Uploaded by

nurfazlinda123
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/ 5

BETC1313

Programming Fundamental

Tutorial 2: Answer

1. Write a C program that generates the following output:


a)
This is BETC1313 tutorial 1
Have a nice day!

ANS:
#include <stdio.h>

int main()
{
printf(”This is BETC1313 tutorial 1”);
printf(”\nHave a nice day!”);
return 0;
}

b)

weight item 1: 98.2


weight item 2: 157.2
weight item 3: 86.8

Total weight = 342.2g

ANS:
#include <stdio.h>

float item1, item2, item3, total;

int main()
{
printf(”weight item 1:”);
scanf ("%f", &item1);
printf(”weight item 2:”);
scanf ("%f", &item2);
printf(”weight item 3:”);
scanf ("%f", &item3);

total = item1 + item2 + item3;


printf(”\nTotal weight = %5.1f g”, total);

return 0;
}
BETC1313
Programming Fundamental

2. Assume a, b and c are integer variables with a = 3 and b = 7 and each expression is
independent. Determine the results of these statements:
a) c = b / a; ANS: 2
b) c = b % a; ANS: 1
c) c = a + b * a; ANS: 24
d) c = ++b / --a; ANS: 4
e) c = a * 2 / a + b++; ANS: 9
f) c = 9 * b % a + --b; ANS: 6

3. Given with i = 2, j = 5 and k = 15, and each expression is independent. Evaluate each
of the following expressions (True or False):
a) i > j - k ANS: T
b) i != k ANS: T
c) k <= k / j ANS: F
d) (j < i) || (k > j) ANS: T
e) (i >= 1) && (j == 5) ANS: T
f) (i > 0) && (j < k) || (k < i) ANS: T

4. Draw a flowchart and write a C program that reads radius and length of a cylinder,
and computes area and volume using the following formulas:
area = π (radius)2
volume = area x length

Start

Read radius and


length

area = radius*radius*pi
volume = area*length

Display radius, length, area &


volume

END
BETC1313
Programming Fundamental

//ANS:
#include <stdio.h>

int main()
{

double radius, length, area, volume;

printf("\nEnter the radius and length of a cylinder: ");


scanf("%lf %lf", &radius, &length);
area = radius * radius * 3.1419;
volume = area * length;

printf("\nRadius = %0.3lf and length = %0.3lf" ,radius, length);


printf("\nArea = %0.3lf" ,area);
printf("\nVolume = %0.3lf\n" ,volume);
return 0;
}

5. Draw a flowchart and write a C program that converts pounds into kilograms. The
program prompts user to enter a number in pounds, converts it to kilograms, and
displays the result. 1 pound is 0.454 kilogram.

Start

Read a number in
pounds

kilograms = pounds*0.454

Display pounds & kilograms

END
BETC1313
Programming Fundamental

//ANS:
#include <stdio.h>

int main()
{
double pound, kg;

printf("\nEnter a number in pounds: ");


scanf("%lf", &pound);

kg = pound * 0.454;
printf("\n\nPounds in kilograms: ");
printf("\n%0.4lf pounds = %0.4lf kilograms\n", pound, kg);
return 0;
}

6. Draw a flowchart and write a C program that asks user to enter his/her birth year. The
program then calculates the corresponding age and displays it to the console. If the
age is larger than 100, prints a message “You are blessed!” and if the age is less than
5, displays the message “You are a smart kid!”.

Start

Read birth year

age = yearNow - yearBorn

Display age

Yes
age > 100
Display “You are blessed!” A
No
Yes
age < 5 Display “You are a smart kid!”
A
No A

END
BETC1313
Programming Fundamental

#include <stdio.h>
#include <stdlib.h>

int main()
{
int yearBorn, age, yearNow = 2017;

printf("What year were you born in? ");


scanf("%d", &yearBorn);

age = yearNow - yearBorn;


printf("You are %d years old.\n", age);

if(age >= 100)


{
printf("You are blessed!\n");
}
else if(age < 5)
{
printf("You are a smart kid!\n");
}

return 0;
}

You might also like