Bharathiar University
Programming in C++ LabC++ Program 3Write a C++ Program to read an integer number and find the sum of all digits until it reduces to a single digit using constructor, destructor and inline member functions
Write a C++ Program to read an integer number and find the sum of all digits until it reduces to a single digit using constructor, destructor and inline member functions
In this page
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE#include <iostream>
using namespace std;
class SumofDigits { public: long int n; SumofDigits() { n=0; } inline void getInteger() { cout << "Enter an Integer Number:"; cin >> n; }
int sum() { int s = 0, r;
while (n>0||s>9) {if(n == 0) { n = s; s = 0; } r = n % 10; s = s + r; n = n / 10;} return s; } ~SumofDigits() { cout<<"End of Program. Object Deleted"; }};
int main() {
SumofDigits SD; int result; cout<<"Sum Of Digits"<<endl; cout<<"*************"<<endl; SD.getInteger(); result = SD.sum(); cout << "Sum of all digits of a number is " << result<<endl; return 0;}
OutputSum Of Digits*************Enter an Integer Number:12343Sum of all digits of a number is 4End of Program. Object Deleted
Turbo c++
#include <iostream.h>#include<conio.h>
class SumofDigits {
public: long int n; SumofDigits() { n=0;
} int sum(); inline void getInteger() { cout << "Enter an Integer Number:"; cin >> n; } ~SumofDigits() { cout<<"End of Program. Object Deleted"; }}; int SumofDigits::sum() { int s = 0, r;
while (n>0||s>9) {if(n == 0) { n = s; s = 0; } r = n % 10; s = s + r; n = n / 10; } return s; }
int main() {
SumofDigits SD; int result; clrscr(); cout<<"Sum Of Digits"<<endl; cout<<"*************"<<endl; SD.getInteger(); result = SD.sum(); cout << "Sum of all digits of a number is " << result<<endl; getch(); return 0;}
OUTPUT
#include <iostream>
using namespace std;
class SumofDigits {
public:
long int n;
SumofDigits()
{
n=0;
}
inline void getInteger() {
cout << "Enter an Integer Number:";
cin >> n;
}
int sum()
{
int s = 0, r;
while (n>0||s>9) {
if(n == 0)
{
n = s;
s = 0;
}
r = n % 10;
s = s + r;
n = n / 10;
}
return s;
}
~SumofDigits()
{
cout<<"End of Program. Object Deleted";
}
};
int main() {
SumofDigits SD;
int result;
cout<<"Sum Of Digits"<<endl;
cout<<"*************"<<endl;
SD.getInteger();
result = SD.sum();
cout << "Sum of all digits of a number is " << result<<endl;
return 0;
}
Output
Sum Of Digits
*************
Enter an Integer Number:12343
Sum of all digits of a number is 4
End of Program. Object Deleted
Turbo c++
#include <iostream.h>
#include<conio.h>
class SumofDigits {
public:
long int n;
SumofDigits()
{
n=0;
}
int sum();
inline void getInteger() {
cout << "Enter an Integer Number:";
cin >> n;
}
~SumofDigits()
{
cout<<"End of Program. Object Deleted";
}
};
int SumofDigits::sum()
{
int s = 0, r;
while (n>0||s>9) {
if(n == 0)
{
n = s;
s = 0;
}
r = n % 10;
s = s + r;
n = n / 10;
}
return s;
}
int main() {
SumofDigits SD;
int result;
clrscr();
cout<<"Sum Of Digits"<<endl;
cout<<"*************"<<endl;
SD.getInteger();
result = SD.sum();
cout << "Sum of all digits of a number is " << result<<endl;
getch();
return 0;
}
No comments:
Post a Comment