+ 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; }
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;
}
+ 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;
}
+ 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!
+ 3
Thanks Ujjawal sharma
+ 2
https://code.sololearn.com/ccg3RRTMGlwo/?ref=app Harsh Kumar yours worked you just needed an equals operater “=“ between d and num%10.
+ 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
+ 1
Where is error in this code?
+ 1
Harsh Kumar I misread, you want the sum of the literal digits in the number.