1 / 11

CSEB114: Principle of programming

CSEB114: Principle of programming. Tutorial 2. Question 1. Write a program to convert an input number of nickels and dime into a total number of cents. For example, if the user inputs 3 and 7 for the number of nickels and dimes, respectively, the screen display at the end of the run would be

lukas
Download Presentation

CSEB114: Principle of programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSEB114: Principle of programming Tutorial 2

  2. Question 1 • Write a program to convert an input number of nickels and dime into a total number of cents. For example, if the user inputs 3 and 7 for the number of nickels and dimes, respectively, the screen display at the end of the run would be • Hints: value for nickel = 5, dimes =10

  3. Answer • Problem Analysis • Input: nickels and dimes • Output: value in cents • Formula: cent = (nickel *5)+(dime*10) • Constraint: none

  4. Answer Pseudocode 1 Flowchart 1 Begin Begin Set vnickel = 5 Set vdime = 10 Read nickel and dime Calculate cent = (nickel * vnickel) + (dime * vdime) Print cent End Set vnickel = 5, Set vdime = 10 Read nickel and dime Calculate cent = (nickel * vnickel) + (dime * vdime) Print cent End

  5. Answer Pseudocode 2 Flowchart 2 Begin Begin Read nickel and dime Calculate cent = (nickel * 5) + (dime * 10) Print cent End Read nickel and dime Calculate cent = (nickel * 5)+ (dime * 10) Print cent End

  6. Answer Pseudocode 3 Flowchart 3 Begin Begin Set vnickel = 5 Set vdime = 10 Read nickel and dime Print cent = (nickel * vnickel) + (dime * vdime) End Set vnickel = 5, Set vdime = 10 Read nickel and dime Print cent = (nickel * vnickel) + (dime * vdime) End

  7. Answer Pseudocode 4 Flowchart 4 Begin Begin Read nickel and dime Print cent = (nickel * 5) + (dime * 10) End Read nickel and dime Print cent = (nickel * 5) + (dime * 5) End

  8. Codes Pseudocode 1 Code 1 Begin Set vnickel = 5 Set vdime = 10 Read nickel and dime Calculate cent = (nickel * vnickel) + (dime * vdime) Print cent End #include<stdio.h> int main () { intvnickel = 5, vdime = 10; int nickel, dime, cent; printf("Enter number of nickels and dimes "); scanf("%d%d", &nickel, &dime); cent = (nickel *vnickel)+(dime*vdime); printf("%d nickels and %d dimes = %d cents\n", nickel, dime, cent); }

  9. Codes Flowchart 2 Code 2 Begin #include<stdio.h> int main () { int nickel, dime, cent; printf("Enter number of nickels and dimes "); scanf("%d%d", &nickel, &dime); cent = (nickel *5)+(dime*10); printf("%d nickels and %d dimes = %d cents\n", nickel, dime, cent); } Read nickel and dime Calculate cent = (nickel * 5)+ (dime * 10) Print cent End

  10. Codes Pseudocode 3 Code 3 Begin Set vnickel = 5 Set vdime = 10 Read nickel and dime Print cent = (nickel * vnickel) + (dime * vdime) End #include<stdio.h> int main () { intvnickel = 5, vdime = 10; int nickel, dime; printf("Enter number of nickels and dimes "); scanf("%d%d", &nickel, &dime); printf("%d nickels and %d dimes = %d cents\n", nickel, dime, (nickel *vnickel)+(dime*vdime)); }

  11. Codes Flowchart 4 Code 4 Begin #include<stdio.h> int main () { int nickel, dime; printf("Enter number of nickels and dimes "); scanf("%d%d", &nickel, &dime); printf("%d nickels and %d dimes = %d cents\n", nickel, dime, (nickel *5)+(dime*10)); } Read nickel and dime Print cent = (nickel * 5) + (dime * 5) End

More Related