Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

almost there!

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I was wondering if anyone could tell me how I can get cents. I can get dollars fine but not the right amount of cents.For example, if i input 100 20 10 4 1 1 (pennies,nickels,dimes,quarters,loonies,toonies) i should get 7 dollars and 0 cents. but i get

Please enter the number of pennies, nickels, dimes, quarters, loonies, toonies,
each separated by a single space.
100 20 10 4 1 1
7 dollar(s) and 700 cents
Press any key to continue . . .

my code is

// Convert strings to integer values.

int pen1 = Integer.parseInt(pen);
int nick1 = Integer.parseInt(nick);
int dime1 = Integer.parseInt(dime);
int quart1= Integer.parseInt(quart);
int loon1 = Integer.parseInt(loon);
int toon1 = Integer.parseInt(toon);

// Calculates the total cent value.

int coins= (pen1*1) + (nick1*5) + (dime1*10) + (quart1*25)
+ (loon1*100) + (toon1*200);




// Calculate the total dollar value.

int dollars=(coins/100);







System.out.println(dollars +" "+ "dollar(s) " + "and " + coins + " " + "cents"); // Prints out total money value.

}
}
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, No need to respond to this post. I have figured it out. I used the %modulus to calculate cents.
cheers, and good health to all.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

May be you have to add one more line

coins = coins%100;

before printing the value.
If you need to store the value of coins then
int Coins1;
Coins1= coins%100;


Kashyap
 
Hang a left on main. Then read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic