Dollars & Cents
Write a C++ program to add two dollars and cents.
INPUT & OUTPUT FORMAT:
Input consists of 4 integers. First two inputs correspond to the value of the first dollar and cent. Next two inputs correspond to the value of the second dollar and cent. Output should print the sum of dollar and cent.
SAMPLE INPUT:
30
10
140
99
SAMPLE OUTPUT:
171
9
Answers
Answer:
#include<iostream>
using namespace std;
int main()
{
int d1, c1, d2, c2;
std::cin>>d1>>c1>>d2>>c2;
int dollar=d1+d2;
int cent=c1+c2;
while(cent>100)
{
cent=cent-100;
dollar=dollar+1;
break;
}
std::cout<<dollar<<"\n"<<cent;
return 0;
}
Answer:
C++ program to add two dollars and cents.
#include <iostream>
using namespace std;
int NormalizeMoney(int);
int main()
{
int cents, dollars;
cout << "How many cents do you have: ";
cin >> cents;
dollars = NormalizeMoney(cents);
return 0;
}
int NormalizeMoney (int cents)
{
int dollars;
static int sum=0; // Local static variable.
// Convert cents to dollars and cents.
dollars = cents/100;
cents = dollars - cents;
// Accumulate a Running Total.
for (int i = 1;i <= sum; i++)
{
cout << "The Sum is : " << sum << endl;
sum+=dollars;
}
return dollars;
}
Explanation:
- For adding the dollars to the cents first we have to normalize all the money in the same unit.
- After that, we can add dollars and sum together.
- The resultant of that is converted into a dollar again that is returned by the function.
#SPJ2