(HackerRank) Write a Modular C Programming code to solve QUEUES – Candy Wending Machine, The first line contains n, the number of coins to be inserted in machine.
Shrirish is student participating in coding competition held by KPIT company. He is given a problem statement “Candy Wending Machine”. A machine will request the selected coins to be inserted, they are 1re, 5rs, 10rs and 20rs coins only. When the value of a coin inserted is other than mentioned coins, those coins are returned by the machine.
If the sum of all coins is greater than or equal to 25rs but less than 34rs then fetch Mango Candy from machine, if the sum of all the coins is greater than or equal to 35rs but less than 49rs then fetch Strawberry Candy, if the sum of all the coins is greater than or equal to 50rs but less than 70rs fetch Pista Candy, otherwise display No candy.
Input Format
The first line contains n, the number of coins to be inserted in machine.
The second line contains n space-separated coins with given denominations.
Constraints
1<=n<=100
0<=i<=n
Coins must be either 1,5,10,20.
Output Format
Dislay the candy name.
Sample Input 0
4
5 10 5 20
Sample Output 0
Strawberry candy
Explanation 0
Sum off all coins will be 40, therefore Strawberry candy will be fetched from machine.
Sample Input 1
3
2 5 10
Sample Output 1
No candy
Explanation 1
Denomination 2 is returned by machine. Sum off coins will be 15, therefore No candy.
CODE:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define SI 100 struct queue { int data[SI],front,rear; }; void enqueue(struct queue *qptr,int num) { if(qptr->rear==SI-1) { printf("Queue Overflow\n"); } else { qptr->rear++; qptr->data[qptr->rear]=num; } } int dequeue(struct queue *qptr) { int num=0; if(qptr->front==qptr->rear) { return num; } else { qptr->front++; num=qptr->data[qptr->front]; return num; } } void check(int sum) { if(sum>=25&&sum<34) { printf("Mango candy"); } else if(sum>=35&&sum<49) { printf("Strawberry candy"); } else if(sum>=50&&sum<70) { printf("Pista candy"); } else { printf("No candy"); } } int main() { struct queue * qptr; struct queue q; qptr=&q; qptr->front=qptr->rear=-1; int num,n,i,sum=0,c; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&num); enqueue(qptr,num); } while(qptr->rear!=qptr->front) { c=dequeue(qptr); switch(c) { case 1:sum=sum+1; break; case 5:sum=sum+5; break; case 10:sum=sum+10; break; case 20:sum=sum+20; break; } } check(sum); return 0; }
OUTPUT
Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 4 5 10 5 20 Your Output (stdout) Strawberry candy Expected Output Strawberry candy
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University