# 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. ## 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 >.<

2. Before you wanna be a programmer, learn how to indent your code so it's legible for others.

3. Also, use code tags.

4. 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");```

5. Originally Posted by itsme86
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. 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.