Program in C++ to find the sum of all digits of a given number
#include<iostream> #include<conio.h> using namespace std; void main() { int n; cout<<"Enter a number:"; cin>>n; int sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } cout<<"Sum of all digits="<<sum; getch(); }Output:-
Program in C++ to find the sum of all digits of a given number using Function
#include<iostream> #include<conio.h> using namespace std; int SumDigit(int ); void main() { int x; cout<<"Enter a number:"; cin>>x; cout<<"Sum of all digits="<<SumDigit(x); getch(); } int SumDigit(int n) { int sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } return (sum); }
0 Comments