C++ program to find sum of digits of given number
#include<iostream>
using namespace std;
int main()
{
int n,r,s=0;
cout<<"Enter a
number : ";
cin>>n;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
cout<<"Sum of
digits of given number is "<<s;
return 0;
}
OUTPUT:
Enter a number : 123
Sum of digits for a given number is 6
C++ program for sum of digits of given
number using for loop
#include<iostream>
using namespace std;
int main()
{
int n,r,s=0;
cout<<"Enter a
number : ";
cin>>n;
for(;n>0;)
{
r=n%10;
s=s+r;
n=n/10;
}
cout<<"Sum of
digits of given number is "<<s;
return 0;
}
OUTPUT:
Enter a number : 123
Sum of digits for a given number is 6
0 comments:
Post a Comment