Ich schrieb ein Programm, das eine ganze Zahl nimmt und abhängig von dieser Zahl weitere Maßnahmen, wie die Berechnung von Factorial, Fibonacci und Beenden des Programms. Aber mit dem Code stimmt etwas nicht. Wenn es das Fibonacci oder das Factorial berechnet, zeigt es Zufallszahl in der letzten Ausgabe, die seltsam ist, und ich kann es nicht herausfinden. Code und Screenshot unterhalb Extra ganze Zahl in der Ausgabe
#include <stdio.h>
#include <conio.h>
int Menu(int num) {
printf("Press 1 to calculate Factorial\n");
printf("Press 2 to calculate Fibannaci series\n");
printf("Press 0 to Quit\n");
scanf("%d", &num);
LOOP:
if (num == 1) {
int n;
printf("Enter the number whose Factorial you wanna calculate: ");
scanf("%d", &n);
printf("%d", Fact(n));
} else
if (num == 2) {
int n;
printf("Enter the term whose Fibannaci series you wanna calculate: ");
scanf("%d", &n);
printf("%d", Fib(n));
} else
if (num == 0) {
printf("Program is Quitting!");
exit(0);
} else {
while (num < 0^num > 2) {
printf("Enter Correct number: ");
scanf("%d", &num);
}
goto LOOP;
}
}
int Fact(int n) {
int Factorial;
if (n == 1 || n == 0) {
return 1;
} else {
Factorial = n * Fact(n - 1);
}
}
int Fib(int n) {
int Fibonacci;
if (n <= 1) {
return n;
} else {
Fibonacci = Fib(n - 1) + Fib(n - 2);
}
}
int main() {
printf("%d", Menu(1));
}
'goto', wirklich ?! –
@LeeTaylor Er lernt gerade :) –
1) Poste Text als Text, nicht Bilder. 2) Formatieren Sie den Code und ziehen Sie ihn richtig ein. es ist nicht lesbar. 3) 'goto' hat seine Verwendungen. Aber nicht für dieses Problem! 4) Siehe [fragen]. – Olaf