W.a.p enter a no. Find and display the sum of all digit of that no. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

W.a.p enter a no. Find and display the sum of all digit of that no.

only for Loop , no, do and while

20th Oct 2017, 5:41 PM
Satya Rebel
Satya Rebel - avatar
1 Answer
+ 1
Try using this: #include<iostream> using namespace std; // declaring a function for your task void task(int num){ int sum = 0, pro = 1; while(num != 0){ dgt = (num%10); num = int(num / 10); sum += dgt; pro *= dgt; } cout<<"Sum of digits: "<<sum; cout<<"Product of digits: "<<pro; } int main(){ int n; cout<<"Enter a number: "; cin>>n; // using the function with number task(n); return 0; }
27th Oct 2017, 3:16 PM
#RahulVerma
#RahulVerma - avatar