Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Help Me! C Programming

  • 19-10-2011 06:20PM
    #1
    Closed Accounts Posts: 327 ✭✭


    I'm having massive trouble with an assignment for a C Programming course.

    I've got to build a program which uses while to work out if a number a user enters is prime or not.

    I've got to make another program which prints all the prime numbers between 1 and 100.

    I've got these working for even numbers but I have no idea how to get the computer to calculate prime.

    I've got to make a program which allows a user to guess a pre-defined number within the range of 1-10. If the user guesses a number outside this range its to ask user to re-enter a guess within the range.

    This is my code... It asks a user to re-enter a number whether the number is within the range or not.
    #include<stdio.h>
    #define ANSWER 4 /*Define constant of ANSWER*/
    
    int main()
    
    {
    int guess;
    printf("This is a number between 1 and 10. Enter your guess:"); /*Read guess from the keyboard*/
    scanf("%d" ,&guess); /*Read user input*/
    
    while 
    	( guess >= 1 && guess <= 10 ); /*While loop to determine if guess is within correct range*/
    	{
    	printf("Please enter another guess within the correct range:\n"); /*Prompts for a new guess*/
    	scanf("%d", &guess);
    }
    
    while 
    	( guess < 1 && guess > 10 ); /*While loop to determine if guess is within correct range*/
    	{
    	
    
    if ( guess == ANSWER ) /*If clause to define if user's guess is correct*/
    {
    printf("You guessed correctly, the answer was 4."); /*If guess is correct this message will show*/
    }
    
    if (guess < ANSWER) /*If clause to define if user's guess is smaller than ANSWER*/
    {
    printf("You guessed incorrectly, the answer is greater."); /*If guess is smaller this message will show.*/
    }
    
    if (guess > ANSWER) /*If clause to to define if user's guess is greater than ANSWER*/
    {
    printf("You guessed incorrectly, the answer is smaller."); /*If guess is smaller then this message will show */
    }
    
    }
    
    return 0;
    }
    

    And I've got to add a count to the above code whereby the program says "You've 2 guesses left. You've 1 guess left. You've 0 guesses left." When the user enters within the correct range but the incorrect number.


Comments

Advertisement