Here is my C code for question 1:
#include <stdio.h>
int main(void) {
//printing our menu
printf("Please select from the list:n");
printf("1. Lemons - $0.21 each,n");
printf("2. Lettuce - $2.39 each,n");
printf("3. Potatoes - $0.79/lb,n");
printf("4. Beans - $0.99/lb,n");
printf("5. Avocado - $1.50 each,n");
printf("6. Tomatoes - $1.99/lbn");
int choice;
float total_amount; //variable to store total amount
printf("Enter selection: ");
scanf("%d",&choice); //taking choice of user as input
while(choice!=0) //we need to stop when user enters 0
{
switch(choice) //switch choice
{
case 1: { //when user enters 1.
int items;
printf("Enter number of items: ");
scanf("%d",&items); //taking input items
total_amount+=(0.21*items); //cost of 1 lemon is 0.21 so we multiply it with number of items
break;
}
case 2: { //when user enters 2
int items;
printf("Enter number of items: ");
scanf("%d",&items);
total_amount+=(2.39*items); //cost of 1 lettuce is 2.39 so we multiply it with number of lettuce
break;
}
case 3: { //when user enters 3
float weight;
printf("Enter weight (lb): ");
scanf("%f",&weight); //taking input weight of potatoes
total_amount+=(0.79*weight); //cost of 1 lb potatoes is 0.79 so we multiply it with weight
break;
}
case 4: { //when user enters 4
float weight;
printf("Enter weight (lb): ");
scanf("%f",&weight); //taking input weight of beans
total_amount+=(0.99*weight); //cost of 1 lb beans is 0.99 so we multiply it with weight
break;
}
case 5: { //when user enters 5
int items;
printf("Enter number of items: ");
scanf("%d",&items);
total_amount+=(1.50*items); //cost of 1 avocado is 1.50 so we multiply it with items
break;
}
case 6: { //when user enters 6
float weight;
printf("Enter weight (lb): ");
scanf("%f",&weight);
total_amount+=(1.99*weight); //cost of 1 lb tomatoes is 1.99 so we multiply it weight
break;
}
default: printf("Invalid selection, select from 1 to 6,enter 0 to stop selectionn"); //when user enters a wrong choice
}
printf("Enter selection: "); //taking input the choice of user
scanf("%d",&choice);
}
printf("Amount due($): %.2fn",total_amount); //total amount due
float cash; //cash received
printf("Enter cash received: ");
scanf("%f",&cash); //taking input cash
printf("Your change is $%.2f",cash-total_amount); //displaying the change that needs to be given
return 0;
}
Here is my C code for question 2
C Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch=’ ‘;
//Reading phone number
printf(“Enter Phone Number: “);
//Reading input from user
while(ch != ‘n’)
{
//Reading a character
ch = getchar();
//Checking character
switch(ch)
{
//Checking corresponding character
case ‘A’:
case ‘B’:
case ‘C’: printf(“2”); break;
case ‘D’:
case ‘E’:
case ‘F’: printf(“3”); break;
case ‘G’:
case ‘H’:
case ‘I’: printf(“4”); break;
case ‘J’:
case ‘K’:
case ‘L’: printf(“5”); break;
case ‘M’:
case ‘N’:
case ‘O’: printf(“6”); break;
case ‘P’:
case ‘Q’:
case ‘R’:
case ‘S’: printf(“7”); break;
case ‘T’:
case ‘U’:
case ‘V’: printf(“8”); break;
case ‘W’:
case ‘X’:
case ‘Y’:
case ‘Z’: printf(“9”); break;
default: printf(“%c”, ch); break;
}
}
return 0;
}


0 comments