110 likes | 230 Views
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
E N D
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 • Hints: value for nickel = 5, dimes =10
Answer • Problem Analysis • Input: nickels and dimes • Output: value in cents • Formula: cent = (nickel *5)+(dime*10) • Constraint: none
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
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
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
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
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); }
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
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)); }
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