Epic Systems Interview Question
Software Engineer / Developerstotal_change=(total_paid-need_payment)*100;
change_10_dollar=total_change/1000;
total_change%=1000;
change_5_dollar=total_change/500;
total_change%=500;
change_1_dollar=total_change/100;
total_change%=100;
change_25_cent=total_change/25;
total_change%=25;
change_10_cent=total_change/10;
total_change%=10;
change_5_cent=total_change/5;
total_change%=5;
change_1_cent=total_change/1;
double deno[]= {10, 5, 1, 0.25, 0.10, 0.5, 0.1};
Scanner reader = new Scanner(System.in);
float cost = reader.nextFloat();
float cash = reader.nextFloat();
float change = cash-cost;
System.out.println(change);
for(int i=0;i<deno.length;i++)
{
float quo = (float) ((float) change/deno[i]);
float rem = (float) ((float) change%deno[i]);
if (quo>=1)
{
float num = (float) ((float) (change-rem)/deno[i]);
System.out.printf("Number of "+deno[i]+"s: "+num+"\n");
change = (float) (change-(num*deno[i]));
}
}
}
}
Good Logic.. But needs a slight Modification ion denominations
double deno[]= {10, 5, 1, 0.25, 0.10, 0.05, 0.01};
//Scanner reader = new Scanner(System.in);
float cost = 67.9500f;
float cash = 100.00f;
float change = cash-cost;
System.out.println(change);
for(int i=0;i<deno.length;i++)
{
float quo = (float) ((float) change/deno[i]);
float rem = (float) ((float) change%deno[i]);
if (quo>=1)
{
float num = (float) ((float) (change-rem)/deno[i]);
System.out.printf("Number of "+deno[i]+"s: "+num+"\n");
change = (float) (change-(num*deno[i]));
}
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/* Function declarations */
void giveTheChange(int dollar, int cents, vector<int> &inDollar, vector<int> &inCent);
void process(int &sum,vector<int> &denom,bool dollar);
/* Function definitions */
void giveTheChange(int dollar, int cents, vector<int> &inDollar, vector<int> &inCent){
/* Find minmum number of dollars needed to give the change */
int sum=dollar;
int centsLeft;
int sumCent=cents;
process(sum,inDollar,true);
if(sum!=0){
centsLeft=sum*100;
sumCent+=centsLeft;
}
process(sumCent,inCent,false);
}
void process(int &sum,vector<int> &denom,bool dollar){
while(sum!=0 && denom.size()!=0)
{
if(denom.back()<= sum )
{
sum-=denom.back();
if(dollar)
cout<<"$"<<denom.back()<<" ";
else
cout<<"##"<<denom.back()<<" ";
//inDollar.pop_back();
}
else
{
denom.pop_back();
}
}
}
int main()
{
char key;
int dollarArray[]={4};
int centArray[]={25,10};
int dollar,cents;
vector <int> inDollar(dollarArray,dollarArray+sizeof(dollarArray)/sizeof(int));
sort(inDollar.begin(), inDollar.end());
vector<int> inCent(centArray, centArray+sizeof(centArray)/sizeof(int));
sort(inCent.begin(), inCent.end());
cout<<"Enter the amount that you wanna make change "<<endl;
cin>>dollar ; cout<<" "; cin >>cents;
cout<<endl;
giveTheChange(dollar, cents, inDollar, inCent);
cin>>key;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the cost of the item: ");
double cost = in.nextFloat();
System.out.print("Enter the cash given by the customer: ");
double cashGiven = in.nextFloat();
double coins[] ={10,5, 1, 0.25 , 0.05, 0.01 };
double change = cashGiven - cost;
if(change ==0){
System.out.println("No change will be given back");
}
else if(change <0){
System.out.println("Cash given is lesser than the cost");
}
else{
for(int i=0;i<coins.length;i++){
int qou = (int)(change/coins[i]);
if(qou >=1){
System.out.println("No of "+ coins[i] + ": "+qou);
change = (double)(change-(coins[i]*qou));
}
}
}
}
}
<pre lang="" line="1" title="CodeMonkey92099" class="run-this">import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the cost of the item: ");
double cost = in.nextFloat();
System.out.print("Enter the cash given by the customer: ");
double cashGiven = in.nextFloat();
double coins[] ={10,5, 1, 0.25 , 0.05, 0.01 };
double change = cashGiven - cost;
if(change ==0){
System.out.println("No change will be given back");
}
else if(change <0){
System.out.println("Cash given is lesser than the cost");
}
else{
for(int i=0;i<coins.length;i++){
int qou = (int)(change/coins[i]);
if(qou >=1){
System.out.println("No of "+ coins[i] + ": "+qou);
change = (double)(change-(coins[i]*qou));
}
}
}
}
}
</pre><pre title="CodeMonkey92099" input="yes">
</pre>
int main(){
double total_paid,need_payment,total_change,change_10_dollar, change_5_dollar, change_1_dollar, change_25_cent, change_10_cent, change_1_cent;
cout<<"enter the amount need to be paid: ";
cin>>need_payment;
cout<<"enter the amount paid: ";
cin>>total_paid;
total_change=(total_paid-need_payment)*100;
cout<<"Total money need to be returned is: " << total_change <<" cents." << endl << endl;
cout <<"Denominations could be:::"<<endl;
change_10_dollar = floor(total_change/1000);
cout<<"10 dollar bill is: "<<change_10_dollar<<endl;
total_change -= change_10_dollar*1000;
change_5_dollar = floor(total_change/500);
cout<<"5 dollar bill is: "<<change_5_dollar<<endl;
total_change -= change_5_dollar*500;
change_1_dollar = floor(total_change/100);
cout<<"1 dollar bill is: "<<change_1_dollar<<endl;
total_change -= change_1_dollar*100;
change_25_cent = floor(total_change/25);
cout<<"25 cents coins is: "<<change_25_cent<<endl;
total_change -= change_25_cent*25;
change_10_cent = floor(total_change/10);
cout<<"10 cent coin is: "<<change_10_cent<<endl;
total_change -= change_10_cent*10;
change_1_cent = floor(total_change/1);
cout<<"1 cent coin is: "<<change_1_cent<<endl;
total_change -= change_1_cent*1;
return 0;
}
public class Denominations {
public static void main(String[] args) {
// We assume we have following denominations
// $10, $5, $1, 25 cents, 10 cents, 5 cents and 1 cent.
double denom[] = new double[] { 10, 5, 1, 0.25, 0.1, 0.05, 0.01 };
double input = 27.8;
for (int i = 0; i < denom.length; i++) {
int x = (int) Math.floor(input / denom[i]);
if (x != 0) {
System.out.println(x + " * " + denom[i]);
}
input = input - (x * denom[i]);
}
}
}
- disun March 13, 2013