Home > Mobile >  Create a C program that outputs the sum and individual digits
Create a C program that outputs the sum and individual digits

Time:10-06

So, I'm struggling with a small coding problem. We're using a while or do while loop to and inputting a series of numbers and outputting the sum as well as the individual numbers.

For example if we put in 123 it should output as "1 2 3" and the total should amount to 6

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
        cout << "Enter a number";
        int individual_number, input_num, sum;
        cin >> input_num;
        cout << endl;
        do
        {
                individual_number = input_num % 10; 
                sum  = individual_number;
                input_num = input_num / 10;

        } while (input_num > 0);

        cout << "The sum is: " << sum << endl;
        return 0;
}

The code outputs the sum but it doesn't separate the numbers. I.e. if I input 123, it will just output the sum and not "1 2 3"

CodePudding user response:

Below is the working example that shows what you want.

Version 1: Using istringstream

#include <string>
#include <iostream>
#include<sstream>

int main()
{
    std::cout << "Enter a number: ";
   
    int individual_number = 0,  sum = 0;//these are local built in types so initialize them
    char c;//for reading char
    std::string input_num;
    
    std::cin >> input_num;

    std::istringstream ss(input_num);
    
    //lets out what number the user entered
    //std::cout<<"the number you entered is: "<<input_num<<std::endl;
    
    while(ss >> c)
    {
        individual_number = c -'0';
        std::cout<<individual_number<<" ";
        sum = individual_number;
    }
    std::cout<<"total amount: "<<sum<<std::endl;
   // std::cout<<"The sum comes out to be: "<<sum<<std::endl;
    
    
    
  return 0;
}

Version 2: Without istringstream

#include <string>
#include <iostream>

int main()
{
    std::cout << "Enter a number: ";
   
    int individual_number = 0,  sum = 0;//these are local built in types so initialize them
  
    std::string input_num;
    
    std::cin >> input_num;
    
    for(char c : input_num)
    {
        individual_number = c -'0';
        std::cout<<individual_number<<" ";
        sum = individual_number;
    }
    std::cout<<"total amount: "<<sum<<std::endl;
   // std::cout<<"The sum comes out to be: "<<sum<<std::endl;
    
    
    
  return 0;
}

Version 3: Without any loops

#include <iostream>
int findDigit(int passed_num)
{
    
    static int localSum = 0;
    
    int lastDigit;
  
    
    if (passed_num == 0) {
     
        return localSum;
    }
 
    // find the last didit
    lastDigit = passed_num % 10;
    
    localSum = lastDigit;
  
   //call findDigit() repeatedly
    findDigit(passed_num / 10);
  
    std::cout<<lastDigit<<" ";
    
    return localSum;
}
  
int main()
{
    std::cout << "Enter a number: ";
    int input_num, sum;
    std::cin>>input_num;
    
    sum = findDigit(input_num);
    
    std::cout<<"sum is: "<<sum<<std::endl;
    
    return 0;
}

CodePudding user response:

Here is a very simple solution using a string to hold the original value:

#include <iostream>
#include <string>

int main()
{
   int individual_number, input_num, sum;
   input_num = 123;
   std::string numStr = std::to_string(input_num);     
   std::cout << std::endl;

   sum = 0;
   do
   {
       individual_number = input_num % 10; 
       sum  = individual_number;
       input_num = input_num / 10;
   } while (input_num > 0);

   std::cout << "The sum is: " << sum << std::endl;

   // Copies the string to the output
   for (auto ch : numStr)
     std::cout << ch << " ";
}

Output:

The sum is: 6
1 2 3 
  •  Tags:  
  • c
  • Related