Program for Conversion of dollar and cents in C++

Dollar and 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

Logic to follow to implement the code

1. Declare the require variables.
2. Get the user inputs values.
3. Perform simple addition of dollars and cents seperately.
4. Perform the divide and modulo operation on the summation of cents.
5. This is done to check that if cents have greater value than the required maximum cents then convert it to the dollar value and add it to the dollar variable and gives the remaining cents.
6. Hence finally, print the dollar and cents added value.

Writing the require code in C++:

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main()
{
 int d1,c1,d2,c2,m,n;
 cin>>d1>>c1>>d2>>c2;

  int d=d1+d2;
  int c=c1+c2;

  m=c/100;
  n=c%100;

  int a=d+m;

 cout<<a<<"\n"<<n;
 return 0;
 }

Status of the Sample test case passed 

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Next Post Previous Post
No Comment
Add Comment
comment url