C++ :: Aufgabe #239
3 Lösungen
Summe alle Teiler einer positven Ganzzahl
Anfänger - C++
von hollst
- 18.06.2019 um 20:49 Uhr
Man schreibe eine Funktion SIGMA(n) mit n > 0 und ganzzahlig, wobei der Funktionswert sich
aus der Summe aller ganzzahligen, positven Teiler von n ergibt.
Beispiele (Zahl; Teiler; Funktionswert);
1; 1; 1
2; 1, 2; 3
4; 1, 2, 4; 7
5; 1, 5; 6
...
12; 1, 2, 3, 4, 6, 12; 28
...
100; 1, 2, 4, 5, 10, 20, 25, 50, 100; 217
Für 0 < n <= 200 sollte sich der beigefügte Pinplot ergeben.
Viel Spaß!
aus der Summe aller ganzzahligen, positven Teiler von n ergibt.
Beispiele (Zahl; Teiler; Funktionswert);
1; 1; 1
2; 1, 2; 3
4; 1, 2, 4; 7
5; 1, 5; 6
...
12; 1, 2, 3, 4, 6, 12; 28
...
100; 1, 2, 4, 5, 10, 20, 25, 50, 100; 217
Für 0 < n <= 200 sollte sich der beigefügte Pinplot ergeben.
Viel Spaß!
Lösungen:
#include <iostream>
using namespace std;
int main()
{
int Zahl;
int x;
int Fktwert = 0;
cout << "Geben Sie eine ganze Zahl zwischen 0 und 200 ein!" << endl;
cout << "Ihre Zahl: ";
cin >> Zahl;
if(Zahl <= 0 || Zahl > 200){
cout << "Falsche Eingabe!" << endl;
return 1;
}
cout << endl;
cout << "Nun werden die Teiler und der Funktionswert bestimmt. (Zahl; Teiler; Funktionswert)" << endl;
cout << endl;
cout << Zahl;
for(int Teiler = 1; Teiler <= 200; Teiler++)
{
x = Zahl % Teiler;
if(x == 0)
{ cout << "; " << Teiler;
Fktwert = Fktwert + Teiler;
}
}
cout << "; " << Fktwert << endl;
return 0;
}
#include <iostream>
using namespace std;
unsigned long long SIGMA(unsigned long inputValue)
{
if (inputValue == 0) { cout << "Eingabe 0 ungültig"; return 0; }
else
{
unsigned long long summe = 0;
for (unsigned long long teiler = 1; teiler <= inputValue; teiler++)
{
if (inputValue % teiler == 0)
{
cout << "Teiler: " << teiler << endl;
summe += teiler;
}
}
return summe;
}
}
int main()
{
unsigned long long eingabe = 0;
cout << "Eingabewert:" << endl;
cin >> eingabe;
cout << eingabe << endl;
cout << SIGMA(eingabe) << endl;
system("pause");
return 0;
}#include <stdio.h>
void Teiler(int n);
int main( )
{
Teiler(1);
Teiler(2);
Teiler(4);
Teiler(5);
Teiler(12);
Teiler(100);
return 0;
}
void Teiler(int n)
{
printf("%i;",n);
int counter = 0;
for (int i = 1; i <= n ; i++)
{
if (n%i == 0)
{
printf(" %i;",i);
counter += i;
}
}
printf(" %i\n",counter);
}