C Programming – HackerRank Solution | STACKS – DENOMINATIONS |

(HackerRank) Write a Modular C Programming code to solve STACKS – DENOMINATIONS, Frankinson has N number of coins in his pocket. Coins are of denominations of 1, 2, 5 and 10.

Frankinson has N number of coins in his pocket. Coins are of denominations of 1, 2, 5 and 10.

He keeps all the coins on the table one above the other. Then he picks out one coin at a time and counts them of specific denomination.

Help Frankinson to count the coins of all the different denominations.

Input Format

First line is N number of coins.

Second line is coins of different denominations.

Constraints

Denomination of each coin should be either 1,2,5 or 10.

Output Format

Number of Coins of Each denomination in separate line.

Sample Input 0

5
1 2 5 10 1

Sample Output 0

Coins of 1 re = 2
Coins of 2 rs = 1
Coins of 5 rs = 1
Coins of 10 rs = 1
Coins of invalid denominations = 0

Sample Input 1

2
1 3

Sample Output 1

Coins of 1 re = 1
Coins of 2 rs = 0
Coins of 5 rs = 0
Coins of 10 rs = 0
Coins of invalid denominations = 1

Sample Input 2

-5

Sample Output 2

Invalid number of coins

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 100 
struct stack
{
    int coin[SIZE];
    int top;
};
void push(struct stack *sptr, int num);
void pop_count_display(struct stack *sptr);
int main()
{
    struct stack * sptr;
    struct stack s;
    sptr=&s;
    sptr->top=-1;
    int c,n,i;
    scanf("%d",&n);
    if(n<0)
    {
        printf("Invalid number of coins");
        exit(0);
    }
    for(i=0;i<n;i++) 
{ 
scanf("%d",&c); 
push(sptr,c); 
} 
pop_count_display(sptr);
 } 
void push(struct stack *sptr, int num)
 { 
if(sptr->top==SIZE-1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        sptr->top++;
        sptr->coin[sptr->top]=num;
    }
}
void pop_count_display(struct stack *sptr)
{
        int i,c1=0,c2=0,c5=0,c10=0,c=0;
        for(i=sptr->top;i>=0;i--)
        {
            if(sptr->coin[i]==1)
            {
               c1++;
            }

            else if(sptr->coin[i]==2)
            {
               c2++;
            }
            else if(sptr->coin[i]==5)
            {
               c5++;
            }
            else if(sptr->coin[i]==10)
            {
               c10++;
            }
            else
            {
                c++;
            }
            sptr->top--;
        }
        printf("Coins of 1 re = %d\n",c1);
        printf("Coins of 2 rs = %d\n",c2);
        printf("Coins of 5 rs = %d\n",c5);
        printf("Coins of 10 rs = %d\n",c10);
    printf("Coins of invalid denominations = %d",c);
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

5
1 2 5 10 1
Your Output (stdout)

Coins of 1 re = 2
Coins of 2 rs = 1
Coins of 5 rs = 1
Coins of 10 rs = 1
Coins of invalid denominations = 0
Expected Output

Coins of 1 re = 2
Coins of 2 rs = 1
Coins of 5 rs = 1
Coins of 10 rs = 1
Coins of invalid denominations = 0

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!