Bti1022 lab sheet 8

6
UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 1022: COMPUTER PROGRAMMING Study the design below LAB EXERCISE 7 LOOP DESIGN

description

 

Transcript of Bti1022 lab sheet 8

Page 1: Bti1022 lab sheet 8

UNIVERSITI TUN HUSSEIN ONN MALAYSIAFACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 1022: COMPUTER PROGRAMMING

Study the design below

LAB EXERCISE 7 LOOP DESIGN

Page 2: Bti1022 lab sheet 8

For Loop

for (int i = 1; i <= n; i++){   double interest = balance * rate / 100;   balance = balance + interest;}

Page 3: Bti1022 lab sheet 8

Exercise 1:Draw a flow chart for:

for (int item=0;item<7;item++){ printf("\n Enter price of item: RM"); scanf("%lf", &price); totalprice +=price;

}

While Loop

Page 4: Bti1022 lab sheet 8

Exercise 2:Draw a flow chart for:

int bil, nilai = 3;for (bil = 5; bil >0; bil--){ if( bil % 2 == 1) printf("\n %d", bil + nilai); }

Exercise 3:Draw a flow chart for:

int x = 1; while ( x < 10 ) { printf( "%d\n", x );

}

Exercise 4:Draw a flow chart for:

int row,space,star;for(row=1;row<=9;row++)

{ for(space=8;space>=row;space--) { printf("%c",288); }; for (star=1;star<row;star++) { printf("* "); } printf("\n");}

Exercise 5: Write an run this program from compiler

#include<stdio.h>#include<conio.h>

char matrik[20],nama[50];

float kiratotal(float x, float y, float z){

Page 5: Bti1022 lab sheet 8

float a; a=x+y+z; return a; }

float kirapurata(float b){ return (b/3); } int display (float total, float average){printf("\n\n=========================");printf("\n STUDENT'S RESULT");printf("\n=========================");

printf("\nname:%s",nama); printf("\n\nmatrix number:%s",matrik); printf("\n\ntotal:%.2f",total); printf("\n\naverage:%.2f\n",average); if (average<=100 && average>=80) { printf("\ngrade:A",average); } else if (average<=79 && average>=60) { printf("\ngrade:B",average); } else if (average<=59 && average>=40) { printf("\ngrade:c",average); } else if (average<=39 && average>=20) { printf("\ngrade:D",average); } else if (average<=19 && average>=0) { printf("\ngrade:E",average);} }

int main(){float total,average,test,assign,final;char ulang='y';

while (ulang=='y'){printf("Enter student information\n");printf("----------------------------");printf("\nMatrik No. : "); scanf("%s",&matrik);

Page 6: Bti1022 lab sheet 8

printf("\nName : "); scanf("%s",&nama);//gets(nama);printf("\nTest Mark : "); scanf("%f",&test);printf("\nAssignment Mark : "); scanf("%f",&assign);printf("\nFinal Exam Mark : "); scanf("%f",&final);

total= kiratotal(test, assign, final);average= kirapurata(total);

display(total,average);

printf("\n========================="); printf ("\nDo you want to proceed? Enter 'y' for yes or else for no : "); scanf ("%s",&ulang); printf ("\n\n\n\n\n");}

getch();return 0;}