Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
a) Write a command to compile a C program and execute the file at terminal in Linux Ubuntu
OS.
[2Marks]
#include <stdio.h>
int main ( )
{
printf(“Hello World\n”);
return 0;
}
float x ;
int y ;
char z ;
Question 2
a) Write a C program to receive input (integer number) from user and store in variable x. Then
display the stored number at terminal(black screen). [12Marks]
#include <stdio.h>
int main ( )
{
float x;
printf(“Please enter your choice of number:”);
scanf(“%f”,&x);
printf(“You have entered: %2.f”,x);
return 0;
}
Question 3
You are required by your client to build a C program to calculate perimeter and area of a right
triangle. The client wants you to define all input variables as integer. Then, user must input the
base(b) and height(a) of the triangle.
Given formula is:
Area= 0.5*(a)*(b)
Perimeter = a + b +
Start
Start
c) Write C program and the output is as shown in example below.
[20Marks]
#include<stdio.h>
#include<math.h>
int main ()
{int a,b ;
float area,perimeter;
printf("Enter height in cm:");
scanf("%d",&a);
printf("Enter base in cm:");
scanf("%d",&b);
area=0.5*(a)*(b);
printf("\nThe area of triangle is (cm2): %.2f",area);
perimeter=a+b+(sqrt(pow(a,2)+pow(b,2)));
printf("\nThe perimeter of triangle is (cm): %.2f",perimeter);
return 0;
}
Question 4
Write a C program to calculate sum and average of electricity bills charge. Then the average of the
bill charge determines the status usage consumption of the electricity. User is required to provide
numbers of month to calculate and followed by the electricity charge of the months. In summary,
Table 2 shows the relation of average bill charge and usage consumption status.
Table 2
Average Bill Charge (RM) Usage Consumption
0.00 – 39.99 LOW
40.00 – 109.99 MEDIUM
110.00 ABOVE HIGH
Example output.
***T&B care-system***
Please enter the bill account number: 12345
Please enter number of months: 3
Please enter amount for month 1(RM): 32.30
Please enter amount for month 2(RM): 27.70
Please enter amount for month 3(RM): 40.00
1.Start
2.Input_ bill account number
3.Input _ number of month
4.Input _ the amount for each month
5.Calculate sum and average of electricity usage
6.Determine the level usage consumption
7.Output _ sum and average of bills electricity
8.Output _ level of usage consumption
9.End
b) Draw flowchart for the program. [10 marks]
START
True
a+1
#include <stdio.h>
int main()
{
int ban,a;
float mnth,average,sum=0,number;
printf("\n******T&B Care-System******");
if (average <=39.99)
{
printf("\nYour electricity usage is LOW");
}
else if((average >=40.00) && (average <=109.99))
{
printf("\nYour electricity usage is MEDIUM");
}
else if(average >=110.00)
{
printf("\nYour electricity usage is HIGH");
}
return 0;
}