“C Program to Calculate Affordable Items Within Budget”
C
Easy
5 views
Problem Description
The array contains a number of items, the user needs to specify a budget and check how many items they can afford - the logic to maintain a running shoe?
Official Solution
#include <stdio.h>
int main() {
int prices[] = {120, 50, 70, 30, 90};
int size = sizeof(prices) / sizeof(prices[0]);
int budget;
int runningTotal = 0;
int count = 0;
printf("Enter your budget: ");
scanf("%d", &budget);
for (int i = 0; i < size; i++) {
runningTotal += prices[i];
if (runningTotal <= budget) {
count++;
} else {
break; // budget exceeded
}
}
printf("You can afford %d items within the budget.n", count);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!