dimanche 22 mars 2015

Need assistance reviewing program

I am trying to create a program where you select one of the four numbers, each giving a different result besides 2 of them. Selecting 10 will square it, making the answer 100. Selecting 9 will ask you to select a different number, selecting 2 or 3 will multiply the number by 99.


The issue I am having is that one out of the four numbers work, if entered 9 it will prompt the user to select a different number, which is correct. However, selecting 10, 3 or 2 will give the answer 0, which is not correct. Although directly typing in 10, 3 or 2 will not provide the right answer, if you type in 9 first, then select 2 or 3, it'll output the correct answer, typing 10 will give you 990.


Here is what I have. Where did I go wrong? Thank you in advance!



#include <stdio.h>
int main (void)
{
int num1, answer;
num1=answer=0;

printf ("Choose a number 2, 3, 9, 10: ");
scanf ("%d", &num1);

while (num1 == 9)
{
if (num1 == 10)
{ answer = num1^2;
}
if (num1 == 9)
{ printf ("Please choose another number: ");
scanf ("%d", &num1);
}
if (num1 == 2||3)
answer = num1 * 99;
}
printf ("Your answer is: %d", answer);
return 0;
}

Aucun commentaire:

Enregistrer un commentaire