+ 1

Write a program to accept any number and display the sum of digit of this number in c++? Where is error in this code?

#include <iostream> using namespace std; main() { int num ,d,sum=0; cout <<"Enter any number "<<endl; cin>>num; while(num!=0) { d, num % 10; sum=sum+d; num=num/10; } cout<< "enter any digit ="<<sum; return 0; }

6th Dec 2020, 1:32 PM
Harsh Kumar
Harsh Kumar - avatar
8 Respostas
+ 6
//Check this code... #include <iostream> using namespace std; int main() { int num,sum=0; cout <<"Enter any number "<<endl; cin>>num; while(num!=0) { int d = num%10; sum=sum+d; num=num/10; } cout<< "enter any digit ="<<sum; return 0; }
6th Dec 2020, 1:56 PM
ㅤㅤㅤ
+ 3
#include <iostream> using namespace std; main() { int num ,d,sum=0; cout <<"Enter any number "<<endl; cin>>num; while(num!=0) { d, num % 10; sum=sum+d; num=num/10; } cout<< "enter any digit ="<<sum; return 0; }
6th Dec 2020, 1:50 PM
Harsh Kumar
Harsh Kumar - avatar
+ 3
Harsh Kumar Try to attach your code in description rather then posting it as an answer.. It will make more chances of getting answers!
6th Dec 2020, 1:52 PM
Piyush
Piyush - avatar
+ 3
Thanks Ujjawal sharma
6th Dec 2020, 2:40 PM
Harsh Kumar
Harsh Kumar - avatar
+ 2
https://code.sololearn.com/ccg3RRTMGlwo/?ref=app Harsh Kumar yours worked you just needed an equals operater “=“ between d and num%10.
6th Dec 2020, 2:00 PM
Roderick Davis
Roderick Davis - avatar
+ 1
so id loop from 1 to the number entered and add each iteration to a sum variable ( sum += number ) then just print sum to the screen outside of the loop
6th Dec 2020, 1:36 PM
Roderick Davis
Roderick Davis - avatar
+ 1
Where is error in this code?
6th Dec 2020, 1:50 PM
Harsh Kumar
Harsh Kumar - avatar
+ 1
Harsh Kumar I misread, you want the sum of the literal digits in the number.
6th Dec 2020, 1:56 PM
Roderick Davis
Roderick Davis - avatar