Пример 8
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int n; // input
int r = 1; // remainder
int s = 0; // sum of the digits
cout << "Please enter a positive integer: ";
cin >> n;
while (n < 0 || n == 0)
{
cout << "Please enter a positive integer: ";
cin >> n;
}
if (n <10)
{
s = n;
cout << "Sum of digits of N =" << s << endl;
}
else
{
while (r > 0)
{
r = n/10;
s = s + (n-r*10);
n = r;
}
cout << "Sum of digits of N = " << s << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Пример 9
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int n; // input
int s;
do
{
cout << "Please enter an integer: ";
cin >> n;
s = s + n;
}
while (n != 0);
cout << "Sum of inputed numbers is = " << s << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Не знайшли потрібну інформацію? Скористайтесь пошуком google:
|
|