Tuesday, 25 August 2015

. If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)

#include"stdafx.h"
#include<iostream>
usingnamespace std;
void main ()
{
int num,a,b,c,d,e,d1,d2,d3,d4,d5,sum;
 cout<<"Enter a five digit number\n";
 cin>>num;
 a=num/10;
 d1=num%10;
 b=a/10;
 d2=a%10;
 c=b/10;
 d3=b%10;
 d=c/10;
 d4=c%10;
 e=d/10;
 d5=d%10;
 sum=d1+d2+d3+d4+d5;
 cout<<"Sum of five digits is "<<sum<<endl;
system("pause");
}

4 comments: