Oops Lab File 1
Oops Lab File 1
AIM : Write a C++ program to find the sum of individual digits of a positive
number.
Introduction : We can perform the sum of digits program in C++ language by the
help of loop and mathematical operation only. The main logic is extract the each
digit from last and find the sum and to extract the digit from the number just divide
it by 10. The remainder is the digit.
Algorithm :
Source code:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,m;
cout<<"Enter a number: ";
cin>>n;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
cout<<"Sum is= "<<sum;
return 0;
}
Output :