190 likes | 416 Views
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART. LAB INTRODUCTION. The printf function. Formatted Output with “printf”. #include < stdio.h > int main (void) { int iMonth ; float fExpense , fIncome ; iMonth = 12; fExpense = 111.1; fIncome = 1000.0;
E N D
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad
The printf function Prepared by: Cik Noor Syazana bt Arshad
Formatted Output with “printf” #include <stdio.h> int main (void) { intiMonth; float fExpense, fIncome; iMonth = 12; fExpense = 111.1; fIncome = 1000.0; printf (“The Month is %4d and the Expense is RM%9.2f\n”,iMonth, fExpense); } Declaring variable (iMonth) to be integer Declaring variables (fExpense and fIncome) to be real Assignment statements store numerical values in the memory cells for the declared variables ‘,’ separates string literal from variable names Correspondence between variable names and %...in string literal Prepared by: Cik Noor Syazana bt Arshad
Formatted Output with printf-cont • printf (“The Month is %4d and the Expense=RM %9.2f \n” ,iMonth, fExpense); • %4d refer to variableiMonth value (decimal number format) • %9.2f refer to variablefExpense value. (floating point format) • The output of printf function will be displayed as Prepared by: Cik Noor Syazana bt Arshad
The scanf function Prepared by: Cik Noor Syazana bt Arshad
Formatted Output with “printf & scanf” #include <stdio.h> int main (void) { intiMonth; float fExpense, fIncome; printf(“Please enter month”); scanf (“%d”,&iMonth); printf(“Please enter the expense and income”); scanf (“%f%f”,&fExpense,&fIncome); printf (“The Month is %4d and the Expense is RM%9.2f\n”, iMonth, fExpense); } Declaring variable (iMonth) to be integer Function scanf reads value typed on keyboard Function scanf reads the first value as fExpense and the second value as fIncome Prepared by: Cik Noor Syazana bt Arshad
Formatted input with scanf-cont Prepared by: Cik Noor Syazana bt Arshad
Data types Prepared by: Cik Noor Syazana bt Arshad
Escape sequence Prepared by: Cik Noor Syazana bt Arshad
How to print a line? • Command • printf(“Welcome to UNIMAP”); • printf(“----------------------------”); • Display Welcome to UNIMAP ---------------------------- Prepared by: Cik Noor Syazana bt Arshad
Algorithm-Basic symbols in a flowchart Flow direction Start/End Process Connector Input/Output Decision Prepared by: Cik Noor Syazana bt Arshad
Q: Write a program to convert distance in meter(m) to centimeter(cm)... Start Get input or distance value in m perform conversion from m to cm using formula: cm=100*m print output or distance value in centimeter (cm) End Prepared by: Cik Noor Syazana bt Arshad
Pseudo code Begin Get distance value in meter (m) Perform conversion from m to cm using formula: cm=100* m Print distance value in centimeter (cm) End Prepared by: Cik Noor Syazana bt Arshad
Program Coding #include <stdio.h> int main () { // variables declaration float fcm,fm; // to display message printf(“\nProgram to convert distance from meter to centimeter”); Prepared by: Cik Noor Syazana bt Arshad
Program Coding (Continued) printf (“\n\nPlease enter the value of distance in meter: ”); scanf(“%f”, &fm); fcm = 100*fm; printf(“\nThe value of distance in centimeter is %5.2f cm\n,fcm); return 0; } //end of main Prepared by: Cik Noor Syazana bt Arshad
Q: Write a program to calculate the volume of a cylinder Start Get radius and height values in inches Calculate volume of a cylinder perform conversion from inches to cm using formula: 1 inches=2.54cm print output or cylinder volumein centimeter cubed (cm3) End Prepared by: Cik Noor Syazana bt Arshad
Pseudo code Begin Get radius and height values in inches Calculate volume of cylinder using formula volume=pi*radius2*height Perform conversion from inches to cm using formula: 1inch=2.54cm Print volume of cylinder in cm3 End Prepared by: Cik Noor Syazana bt Arshad
Program Coding #include <stdio.h> int main () { // variables declaration const double PI=3.141594; double dVolume; float fR,fH; // to display message printf(“\nProgramto calculate the volume of a Cylinder\n”); Prepared by: Cik Noor Syazana bt Arshad
Program Coding (Continued) printf (“\nPleaseenter the value of radius and height in inches:\n ”); scanf(“%f%f”, &fR,&fH); dVolume= PI*fR*2.54*fR*2.54*fH*2.54; printf(“\nThevolume of a cylinder in centimeter cubed is %7.2f cm\n”,dVolume); return 0; } //end of main Prepared by: Cik Noor Syazana bt Arshad