Digit split and sum
Write a program to split all the digit from a given integer input and make sum of all digits.
Sample Input: 13245
Sample Output:
The digits are:
1
3
2
4
5
Sum: 6
Sample Input: 13245
Sample Output:
The digits are:
1
3
2
4
5
Sum: 6
Id: 201820244
ReplyDeleteBatch:58th
Section B
#include
using namespace std;
class split_number
{
public:
int number;
split()
{
cout <<"Enter a number to separate it's digits: ";
cin >> number;
while (number != 0)
{
cout << number%10 << " ";
number /= 10;
}
cout << endl;
return number;
}
};
int main()
{
split_number obj;
cout<< "split numer is" <<obj.split()<<endl;
}
Id: 201820244
ReplyDeleteBatch:58th
Section B
#include
using namespace std;
class split_number{
public:
int number,reverseNum=0;
split(){
cout << "Enter a number to separate it's digits: ";
cin >> number;
while (number > 0)
{
reverseNum += (number%10);
reverseNum *= 10;
number /= 10;
}
reverseNum /= 10;
while (reverseNum != 0)
{
cout << reverseNum%10 << " ";
reverseNum /= 10;
}
cout<<endl;
return 0;
}
};
int main()
{
split_number obj;
cout<<obj.split()<<endl;
}
Finally Done it
ReplyDeleteId:201820244
Batch:58th
section:B
#include
#include
using namespace std;
class split_number{
public:
int number,reverseNum=0;
split(){
cout << "Enter a number to separate it's digits: ";
cin >> number;
while (number > 0)
{
reverseNum += (number%10);
reverseNum *= 10;
number /= 10;
}
reverseNum /= 10;
int sum = reverseNum%10;
while (reverseNum != 0)
{
cout << reverseNum%10 << endl;
reverseNum /= 10;
sum+=reverseNum%10;
}
cout<<"\nSummation is: "<<sum<<endl;
getch();
}
};
int main()
{
split_number obj;
cout<<obj.split()<<endl;
return 0;
}