Amflying Posted June 23, 2008 at 11:32 AM Report Share #192987 Posted June 23, 2008 at 11:32 AM Boas pessoal Estou com um problema num programa em C que ainda não descobri a solução. Talvez algum de vós me possa ajudar: Tenho o seguinte programa mas não consigo que após fazer as funções volte ao menu escolherOpcao: #include <stdio.h> void mostrarMenu(void); void escolherOpcao(int); void verificarPrimo(int); void factorial(int); int main() { int num; printf("Introduza um numero inteiro: "); scanf("%d", &num); mostrarMenu(); escolherOpcao(num); return 0; } void mostrarMenu(void) { printf("Para o numero introduzido pretende:\n"); printf("1 - Determinar se e primo\n"); printf("2 - Calcular factorial\n"); printf("3 - Sair\n"); printf("Escolha uma opcao:"); } void escolherOpcao(int x) { int valor; do { scanf("%d", &valor); switch(valor) { case 1: verificarPrimo(x); break; case 2: factorial(x); break; case 3: printf("Sair\n"); } } while (valor != 1 && valor != 2 && valor != 3); } void factorial(int a) { int fact = 1, i; for(i = 1; i<= a; i++) fact = fact * i; printf("O factorial do numero e %d", fact); } void verificarPrimo(int a) { int b, primo = 0; for(b = 2; b <= a/2; b++) if (a % b == 0) { primo=1; break; } if (primo == 0) printf("O numero e primo."); else printf("O numero nao e primo."); } Link to comment Share on other sites More sharing options...
lufinima Posted June 23, 2008 at 06:08 PM Report Share #193098 Posted June 23, 2008 at 06:08 PM usando um while na parte principal deve funcionar assim a modos que rápido eu faria da seguinte maneira, não tive a testar mas deve funcionar: defines uma variável global pa usar no ciclo while já que precisas de usar essa variável em 2 funções #include <stdio.h> void mostrarMenu(void); void escolherOpcao(int); void verificarPrimo(int); void factorial(int); int sair=0; int main() { int num; while(sair!=1) { printf("Introduza um numero inteiro: "); scanf("%d", &num); mostrarMenu(); escolherOpcao(num); } return 0; } void escolherOpcao(int x) { int valor; do { scanf("%d", &valor); switch(valor) { case 1: verificarPrimo(x); break; case 2: factorial(x); break; case 3: sair=1;//printf("Sair\n"); } } while (valor != 1 && valor != 2 && valor != 3); } Link to comment Share on other sites More sharing options...
Amflying Posted June 24, 2008 at 10:26 AM Author Report Share #193182 Posted June 24, 2008 at 10:26 AM 😛 Obrigado Link to comment Share on other sites More sharing options...
lufinima Posted June 24, 2008 at 07:31 PM Report Share #193282 Posted June 24, 2008 at 07:31 PM 😉 Obrigado De nada, estamos aqui para nos ajudar-mos uns aos outros 🙂 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now