Friday, July 4, 2025

Today's exam program

 Java::


 import java.util.*;


public class KennyCoolDrinks {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        int n = sc.nextInt(); // total number of shops

        int[] prices = new int[n];


        // read prices of each shop

        for (int i = 0; i < n; i++) {

            prices[i] = sc.nextInt();

        }


        int t = sc.nextInt(); // number of test cases


        for (int testCase = 0; testCase < t; testCase++) {

            int m = sc.nextInt(); // number of days (not used)

            int p = sc.nextInt(); // money per day


            int count = 0;


            // check each shop

            for (int i = 0; i < n; i++) {

                if (prices[i] <= p) {

                    count++;

                }

            }


            // print: test case number and count

            System.out.print(testCase + " " + count + " ");

        }

    }

}



In c language::


#include <stdio.h>


int main() {

    int n;

    scanf("%d", &n); // total shops


    int prices[n];

    for (int i = 0; i < n; i++) {

        scanf("%d", &prices[i]); // shop prices

    }


    int t;

    scanf("%d", &t); // number of test cases


    for (int testCase = 0; testCase < t; testCase++) {

        int m, p;

        scanf("%d %d", &m, &p); // m is unused, p is money per day


        int count = 0;

        for (int i = 0; i < n; i++) {

            if (prices[i] <= p) {

                count++;

            }

        }


        printf("%d %d ", testCase, count);

    }


    return 0;

}


 Concepts used in above program::


Array--> A list of values To store the prices of Coca Cola at each shop


Loop (for)--> Repeat something multiple times To go through each shop and check if Kenny can afford it


If Condition---> Decide whether something is true or false To check: if (rate ≤ p)


Input Reading--> Getting values from user or keyboard Use Scanner in Java or scanf in C


Output --->Showing result to the user Use System.out.print in Java or printf in C


 Program Flow:


1) Read total shops and prices


2) For each test case:


Read money Kenny has per day


Check how many shops have prices ≤ Kenny's money


Print: test case number and how many shops he can afford


 Final Output :


0 4 1 5





Kenny = Customer


Shop = Vendor


Rate = Cost of service or product


This program filters which vendors fall under the customer’s daily budget

No comments:

Post a Comment

English basics example

 To identify parts of speech (noun, verb, adjective, etc.), examine a word’s **function and role** in a sentence. Here’s a step-by-step anal...