Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of: 3456 as 3 4 5 6 8030 as 8 0 3 0 2345526 as 2 3 4 5 5 2 6 4000 as 4 0 0 0 -2345 as 2 3 4 5

Respuesta :

Limosa

Answer:

The following program is written in C++ Programming Language:

#include <iostream>           //header file

using namespace std;        //using namespace

int main(int argc, char* argv[])     //set parameterized main() function

{

   int a=0,b[100]={0},n=0,sum=0;     //declare integer variables and array

   cin>>a;        //get number from the user

   while(a>0)     //set the while loop

   {

       b[n]=a%10;  

       a/=10;

       n++;

   }

   for(int i=n; i>0; i--)      //set the for loop

   {

       printf("%d ",b[i-1]);    

       sum+=b[i-1];

   }

   printf(" Sum: %d ",sum);       //print the result

   cin.get(); cin.get();

   return 0;

}

Input:

8030

Output:

8 0 3 0   sum: 11

Explanation:

Here, we define the header file "iostream" and the namespace "std"

then, we set the parameterized main() method.

Then, we set three integer variables "a" to 0, "n" to 0, "sum" to 0 and an integer array variable "b[100]" to {0}.

Then, we get a number from the user in variable "a".

Then, we set the while loop and pass the condition then, pass the for and pass the condition after that we print the result.

Q&A Education