Question

In C++ write an application which takes positive or negative integers and returns the sum of...

In C++ write an application which takes positive or negative integers and returns the sum of each place in the number, such that 246 results in 12 and -88 results in -16. The program is called and runs with no prompt for input. The program reads exactly one signed integer from standard input. The program displays the sum of each digit of the number as a signed integer. This should be done in two files called main.cc and makefile.The result of calling the make application should be an executable named 01hw .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ofstream file;
file.open ("01hw.txt");

int value,sum=0,rem;
cout<<"Enter any value : "<<endl;
cin>>value;
while(value)
{
rem=value%10;
value=value/10;
sum=sum+rem;
}
cout<<"Sum of Digits is "<<sum<<endl;

file << "Sum of Digits is "<<sum;
file.close();
return 0;
}

CAUsersvusenDesktopDebugimain.exe Enter any value: 246 Sum of Digits is 12 Press any key to continue . . . 8:32 PM 1/2018 lyp

CAUsersvusenDesktopDebugimain.exe Enter any value: Sum of Digits is -16 Press any key to continue . . . 833 PM 1/2018 lype he

Add a comment
Know the answer?
Add Answer to:
In C++ write an application which takes positive or negative integers and returns the sum of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • Problem 1: Write a program that reads a positive float number and displays the previous and...

    Problem 1: Write a program that reads a positive float number and displays the previous and next integers. Sample Run: Enter a float: 3.5 The previous and next integers are 3 and 4. Problem 2: Write a program that reads two integers and displays their sum, difference, product, and the result of their division. Sample Run: Enter two integers: 8 5 Sum: 8, Difference: 3, Product: 40, Division: 1.6 Problem 3: Write a program that reads a three-digit integer from...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input...

    This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input when a non-integer is read. Use this exact prompt: System.out.print("Enter an integer or Q to quit: "); Note: you will actually quit on any non-integer. Do the following things: Find and print the sum of all the even numbers Find and print the smallest of the inputs Determine if the number 7 is in the input. If 7 is in the inputs, print "7...

  • Write a C program which reads sets of two integers and displays them and their sum....

    Write a C program which reads sets of two integers and displays them and their sum. Continue readings sets of integers until their sum is zero, at which point terminate the program.

  • C++ write a function named subtract that takes two integers as parameters and returns the result...

    C++ write a function named subtract that takes two integers as parameters and returns the result of subtracting the second number from the first. i.e. int1 - int2 Change subtract to have default arguments for its two parameters. Pick whatever non-zero numbers you would like. Write a prototype for subtract before main so the program runs without error. I cant figure out the part where you pass no parameters, I've tried to set defailt values for the parameters but it...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT