Program to Compute Change

Calculate US currency change

Problem Explained

  1. Input an amount from a user as a decimal number, such as 11.56
  2. Conver the amount (e.g, 11.56) as (1156 cents)
  3. Calculate the change as per the following rules...

1 Doller 100 Cents
1 Quarter 25 Cents
1 Dime 10 Cents
1 Nickel 5 Cents
1 Pannie 1 Cent

C Programming

#include<stdio.h>
int main() {
float amount;
int dollars,quarters,dimes,nickels,cents,pennies;
printf("Enter the amount: ");
scanf("%f",&amount);
cents = (int)(amount * 100);
dollars = cents / 100;
cents = cents % 100;
quarters = cents / 25;
cents = cents % 25;
dimes = cents / 10;
cents = cents % 10;
nickels = cents / 5;
pennies = cents % 5;
printf("Your amount %.2f consists of \n",amount);
printf("%d dollers\n",dollars);
printf("%d quarters\n",quarters);
printf("%d dimes\n",dimes);
printf("%d nickles\n",nickels);
printf("%d pennies\n",pennies);
return 0;
}

C++

#include<iostream>
using namespace std;
int main() {
float amount;
cout << "Enter the amount: ";
cin >> amount;
int cents = (int)(amount * 100);
int dollars = cents / 100;
cents = cents % 100;
int quarters = cents / 25;
cents = cents % 25;
int dimes = cents / 10;
cents = cents % 10;
int nickels = cents / 5;
int pennies = cents % 5;
cout << "Your amount " << amount << " consists of " << endl;
cout << dollars << " dollars" << endl;
cout << quarters << " quarters " << endl;
cout << dimes << " dimes " << endl;
cout << nickels << " nickels " << endl;
cout << pennies << " pennies " << endl;
return 0;
}

Java

import java.util.Scanner;
class ComputeChange {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the amount: ");
float amount = sc.nextFloat();
int cents = (int)(amount * 100);
int dollars = cents / 100;
cents = cents % 100;
int quarters = cents / 25;
cents = cents % 25;
int dimes = cents / 10;
cents = cents % 10;
int nickels = cents / 5;
cents = cents % 5;
int pennies = cents;
System.out.println("Your amount "+amount+" consists of");
System.out.println(dollars+" dollars");
System.out.println(quarters+" quarters");
System.out.println(dimes+" dimes");
System.out.println(nickels+" nickels");
System.out.println(pennies+" pennies");
}
}

PHP

<?php
if(isset($_POST['sub'])) {
$amount = $_POST['amount'];
$cents = $amount * 100;
$dollars = (int)($cents / 100);
$cents = $cents % 100;
$quarters = (int)($cents / 25);
$cents = $cents % 25;
$dimes = (int)($cents / 10);
$cents = $cents % 10;
$nickels = (int)($cents / 5);
$pennies = $cents % 5;
echo "Your amount ".$amount." consists of <br>";
echo $dollars." Dollars<br>";
echo $quarters." Quarters<br>";
echo $dimes." Dimes<br>";
echo $nickels." Nickels<br>";
echo $pennies." Pennies<br>";
}
?>

Go Lang

package main
import "fmt"
func main() {
var amount float32;
fmt.Print("Enter the amount: ")
fmt.Scan(&amount)
cents := (int)(amount * 100)
dollars := cents / 100
cents = cents % 100
quarters := cents / 25
cents = cents % 25
dime := cents / 10
cents = cents % 10
nickels := cents / 5
pennies := cents % 5;
fmt.Printf("Your amount %.2f consists of\n",amount)
fmt.Println(dollars," Dollars")
fmt.Println(quarters," Quarters")
fmt.Println(dime," Dimes")
fmt.Println(nickels," Nickels")
fmt.Println(pennies," Pennies")
}

Comments

Popular posts from this blog

Program to calculate telephone bill

Sum of two numbers in go lang

Program to check a year is leap year or not.

String in golang

Identifiers

Arrays in C Language

Reserved Words in Java

Data types in Java

Pointers in C Language

Literals in Java