Answer to Question #303483 in C for Mansi

Question #303483

A company is planning to provide an extra discount to it's customers. Every order has an order ID associated with it which is a sequence of digits. The discount is calculated as the count of unique repeating digits in the order ID. Write a code to find the discount percentile given to the customers.

1
Expert's answer
2022-02-28T01:06:53-0500
#include <stdio.h>

int count[10];

int count_unique_digits(int id) {
  while (id > 0) {
    int digit = id % 10;
    count[digit]++;
    id = id / 10;
  }
  int res = 0;
  for (int i = 0; i < 10; i++) {
    if (count[i] == 1) res++;
  }
  return res;
}

int main() {
  int id;
  printf("Enter 'id': ");
  scanf("%d", &id);
  printf("The discount is: %d\n", count_unique_digits(id));
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS