Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help Me! C Programming

  • 19-10-2011 7: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