I know the first 8 perfect numbers. This part of my assignment is basically just wanting me to do what I have already done, so I'm not going to mess around with it now. I find the void main(void) function unreliable, yet when I confronted my tutor he merely told me not to take advice from these boards since many people don't know what they're talking about...
Since I find your style of code more reliable, I will continue to do so.
For part 2, I had to create a guess the number game, I found asking relevant questions hard, but anyway here it is, please tell me how it could be improved!
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int number;
int b;
int c=0;
int z=50;
int fin=0;
int constant=0;
printf("Think of a number between -100 and 100...\n\nI will attempt to guess it. To answer the questions you must enter:\n\n1 for Higher\n2 for Lower\n3 for the Correct answer\n");
/* Welcome message for the program */
while(constant==0)
/* This loops the program, but the welcome message does not loop repeatedly as the variable "constant" doesn't change */
{
z=50;
number=0;
fin=0;
printf("\nThought of that number yet? - Ready when you are!\n\n");
/* This prepares for the first question */
printf("Is your number Higher or Lower than 0? ");
scanf("%d", &b);
/* This asks the user the first question */
if(b==1)
{
number=50;
}
/* This set of code determines the reply to your answer. Higher then 0 and the question will be half way between 0 and 100 (50) */
if(b==2)
{
number=-50;
}
/* Should you answer lower then 0, the question will be half way between 0 and -100 (-50) */
if(b==3)
{
printf("Your number is 0");
}
if(b!=3)
{
while(fin==c)
/* This while loop is used to run the code to ask the next question while the game is not over i.e. "fin" is not set to 1 */
{
printf("Is your number Higher, Lower or Correct at %d? ", number);
scanf("%d", &b);
/* The code below checks to see if "z" divided by two leaves a remainer, if it does it adds one */
/* to the result. In other words it makes sure if "z" cannot be divided by two without a remainder */
/* it is rounded up */
if(z%2==1)
{
z=(z/2)+1;
}
else
{
z=z/2;
}
// If the response to the question was 1 (Higher) then z is ADDED to "number"
if(b==1)
{
number=number+z;
}
// If the response to the question was 2 (Lower) then z is SUBTRACTED from "number"
if(b==2)
{
number=number-z;
}
// If the response to the question was 3 (Equal to) "number" is displayed on the screen
// and "fin" is set to 1 which makes it drop out of the while loop
if(b==3)
{
printf("\nYour number is %d\n\n", number);
fin = 1;
}
}
}
}
system("PAUSE");
return 0;
}