Last active
December 26, 2015 19:29
-
-
Save d8ta/7201267 to your computer and use it in GitHub Desktop.
14. (1P)Schreibe eine Funktion isPrime(), die testet, ob eine als Parameter übergebene Zahl eine Primzahl ist. Die Funktion soll dabei nichts ausgeben, nur einen passenden Rückgabewert liefern.
Hinweis: Eine Primzahl ist eine natürliche Zahl, die neben 1 durch genau eine weitere Zahl teilbar ist. Schreiben die Main-Methode, welche eine natürlic…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1310601027, fhs36118 | |
* Daniel Raudschus | |
* ueo3 bsp14 | |
*/ | |
using System; | |
namespace bsp14 | |
{ | |
/* 14. (1P)Schreibe eine Funktion isPrime(), die testet, ob eine als Parameter übergebene Zahl eine Primzahl ist. | |
* Die Funktion soll dabei nichts ausgeben, nur einen passenden Rückgabewert liefern. | |
* Hinweis: Eine Primzahl ist eine natürliche Zahl, die neben 1 durch genau eine weitere Zahl teilbar ist. | |
* Schreiben die Main-Methode, welche eine natürliche Zahl einliest, und dann mithilf jeder Funktion ausgibt, | |
* ob diese Zahl prim oder zusammengesetzt ist. | |
*/ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
if (isPrime (16)) { | |
Console.WriteLine ("Prime!"); | |
} else { | |
Console.WriteLine ("Not Prime!"); | |
} | |
} | |
public static bool isPrime( int num ) | |
{ | |
for ( int counter = 2; counter < num; counter ++ ) | |
{ | |
if ( num % counter == 0 ) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment