22. Program that inputs five-digit number through the keyboard and calculates the sum of its digits.

Chapter 4: 

Input And Output

Programming Exercise

Problem # 22:

Write a program that inputs five-digit number through the keyboard and calculates the sum of its digits.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int n;
    cout<<"Enter a five digit number : ";
    cin>>n;
    int a, b, c, d, e, sum=0;
    a = n % 10;
    n = n / 10;
    b = n % 10;
    n = n / 10;
    c = n % 10;
    n = n / 10;
    d = n % 10;
    n = n / 10;
    e = n % 10;
    sum = a + b + c + d + e;
    cout<<"Sum of "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" is : "<<sum<<endl;
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
21. Program to enter a letter and display the next two letters.

Comments

Post a Comment