C Programming – HackerRank Solution | MIDDLE-CLASS FAMILY |

(HackerRank) Write a Modular C Programming code to solve MIDDLE-CLASS FAMILY, A middle class family would always like to spend the money wisely, because of their financial status.

A middle class family would always like to spend their money wisely, because of their financial status. They can’t afford to purchase luxurious items, unlike the Rich Class family. Even if they do, they do it very occasionally. In order to keep track of the expenditures of the entire year, the middle-class family maintains a record of how much they spent every month of that year. Consider for the year 2019, a family maintains a record of expenditure for each month. At the end of the year, the family would like to know the following about their expenditure. Which month of the year they spent more? (display month number) Which month of the year they spend less? (display month number). Which are the months they spent more than 35000 rupees? (display month number). Apply Problem solving framework to solve the said middle-class family problem.

Input Format

Number of months in first line.

Expenditure every month in next line.

Constraints

Number of months are fixed to 12.

Starting from 1 to 12, 1 indicates January month, 2 indicates February month and so on upto 12 indicates December month.

Output Format

First line should display month number for spent more.

Second line should display month number for spent less.

Third line should indicate month numbers spent more than 35000.

Sample Input 0

12
49832 34829 21102 58222 10238 33390 12220 47472 25000 11200 34000 29005

Sample Output 0

4
5
1 4 8

Sample Input 1

5

Sample Output 1

Expenditure should be for 12 months.

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>

struct node{
    int spent;
    struct node *link;
};
typedef struct node *NODE;
NODE create_node();
NODE insert_end(NODE head);
void display_max(NODE head);
void display_min(NODE head);
void display_more_than_35000(NODE head);
int main() {
    NODE head=NULL;
    int n;
    scanf("%d",&n);
    if(n==12)
    {
        for(int i=0;i<12;i++) { head=insert_end(head); } display_max(head); display_min(head); display_more_than_35000(head); }else{ printf("Expenditure should be for 12 months."); } return 0; } NODE create_node() { NODE newnode; newnode=malloc(sizeof(struct node)); newnode->link=NULL;
    return newnode;
}
NODE insert_end(NODE head)
{
    NODE newnode=create_node();
    scanf("%d",&newnode->spent);
    if(head==NULL)
        head=newnode;
    else{
        NODE cur=head;
        while(cur->link!=NULL)
            cur=cur->link;
        cur->link=newnode;
    }
    return head;
}
void display_max(NODE head)
{
    NODE cur=head;
    int max=cur->spent;
    int i=1;
    int count=1;
    while(cur!=NULL)
    {
        if(maxspent)
        {
            max=cur->spent;
            i=count;
        }
        cur=cur->link;
        count++;
    }
    printf("%d\n",i);
}
void display_min(NODE head)
{
    NODE cur=head;
    int min=cur->spent;
    int i=1;
    int count=1;
    while(cur!=NULL)
    {
        if(min>cur->spent)
        {
            min=cur->spent;
            i=count;
        }
        cur=cur->link;
        count++;
    }
    printf("%d\n",i);    
}
void display_more_than_35000(NODE head)
{
    NODE cur=head;
    int count=1;
    while(cur!=NULL)
    {
        if(cur->spent>35000)
        {
            printf("%d ",count);
        }
        cur=cur->link;
        count++;
    }   
}

OUTPUT

Congratulations, you passed the sample test case.

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

Input (stdin)

12
49832 34829 21102 58222 10238 33390 12220 47472 25000 11200 34000 29005
Your Output (stdout)

4
5
1 4 8 
Expected Output

4
5
1 4 8

 

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







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 !!!