Need help with C prgramming Question please

This is a discussion on Need help with C prgramming Question please within the C Programming forums, part of the General Programming Boards category; Question: write a program that gives this output: Enter the number of numbers: 69 Sorry! a maximum of 20 is ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Unhappy Need help with C prgramming Question please

    Question: write a program that gives this output:
    Enter the number of numbers: 69
    Sorry! a maximum of 20 is allowed
    Enter the number of numbers; 2
    Enter 1st number: -15
    No negative numbers!
    Enter 1st number: 8
    Number must be divisible my 3!
    Enter 1st number: 3
    Enter 2nd number: 12
    The sum is: 15

    here's what i tried and failed ofcourse :'(
    #include <stdio.h>
    #include <conio.h>
    int main (void)
    {
    int numofnums, num, sum = 0, count = 0;
    printf("Enter Number of numbers ");
    scanf("%d", &numofnums);
    do {
    if (numofnums <= 20){
    printf("Enter Number");
    scanf("%d", &num);
    if (num >0){
    if (num%3 == 0) {
    sum = sum + num;
    count = count + 1;
    } else {printf("Sorry! Number must be divisible by 3!");}
    } else {printf("Sorry! Number must be positive!");}
    }else {printf("Number must not exceed 20!");}
    }while (count != numofnums);
    printf("The Sum of the numbers entered is " "%d", sum);
    getch();
    }

    it only runs one way, as in you can enter any number over 20 >.<
    please help
    Last edited by wanaBprogrammer; 11-18-2010 at 11:22 AM. Reason: because someone says it needs to be indented -_-

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Before you wanna be a programmer, learn how to indent your code so it's legible for others.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, use code tags.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Question: write a program that gives this output:
    Enter the number of numbers: 69
    Sorry! a maximum of 20 is allowed
    Enter the number of numbers; 2
    Enter 1st number: -15
    No negative numbers!
    Enter 1st number: 8
    Number must be divisible my 3!
    Enter 1st number: 3
    Enter 2nd number: 12
    The sum is: 15
    It seems pretty straight forward to me:
    Code:
    puts("Enter the number of numbers: 69");
    puts("Sorry! a maximum of 20 is allowed");
    puts("Enter the number of numbers; 2");
    puts("Enter 1st number: -15");
    puts("No negative numbers!");
    puts("Enter 1st number: 8");
    puts("Number must be divisible my 3!");
    puts("Enter 1st number: 3");
    puts("Enter 2nd number: 12");
    puts("The sum is: 15");
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Quote Originally Posted by itsme86 View Post
    It seems pretty straight forward to me:
    Code:
    puts("Enter the number of numbers: 69");
    puts("Sorry! a maximum of 20 is allowed");
    puts("Enter the number of numbers; 2");
    puts("Enter 1st number: -15");
    puts("No negative numbers!");
    puts("Enter 1st number: 8");
    puts("Number must be divisible my 3!");
    puts("Enter 1st number: 3");
    puts("Enter 2nd number: 12");
    puts("The sum is: 15");
    OMG >_<
    the program is not to print those lines....
    it is to Accept a number from the user check if that number is more than 20, if it is display the correct message and if its not check if the number less than 0, if it is print the correct message etc etc...
    basically those lines are what is to come up on the screen when the user enters 69, 2, -15, 8, 3 and 12.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Where it's asking for number of numbers you shouldn't put that part in the main loop. Handle it seperately:
    Code:
    do {
        printf("Enter Number of numbers ");
        scanf("%d", &numofnums);
        if (numofnums > 20)
            printf("Sorry! a maximum of 20 is allowed\n");
        else if (numofnums < 0)
            printf("No negative numbers!\n");
        } while (numofnums > 20 || numofnums < 0);
    Then the part asking for the individual numbers is in a do...while after that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21