write a program that prompts the user to input a positive integer , if the user enters zero or a negative value for nu, it prompts user to input num again and repets this process untill unser inputs a correct value. it then display the sum of the numbers 1 to num

i am having difficulties to do the loop on how the program will check whether the entered num is 0 or negative...

Recommended Answers

All 11 Replies

First off, we don't do other people's homework for them. Second, we don't do other people's homework for them. And third, we don't do other people's homework for them. Sensing a pattern here yet?

No one here will simply hand you a solution on a silver platter. If you show us what you've done, what you've tried to do, and what problems you've had with it, then we'll be happy to help. If you have specific questions, we can answer them, or at least point you in the right direction. If you have a program with a bug you can't swat on your own, we'll be glad to assist, so long as you pay attention to the forum rules and post sensible questions in an intelligent manner that we have some reasonable hope of answering.

But just cutting and pasting an assignment into a message, without even prefacing it with something like, "I have this homework problem that I can't solve...", is likely to get you booted from the message boards here and elsewhere - if you're lucky. What happens to you if you are unlucky is... well... let's just say that this guy probably won't be trying that again, on that forum or this one.

We take this issue seriously here. Very seriously. Asking us to do homework for you is a grave breach of academic ethics on your part, and actually doing so would be an even bigger breach on ours (not that this stops the many fine mercenaries at Freelancer.com, but still). Simply posting this here, in this way, could get you expelled from your school, if someone happens to notice it and blow the whistle on you. Furthermore, it does neither you nor us any good to help you cheat - especially since there's a good chance some day one of us will have to work with you, manage you, or, Eris forefend, fix code you've written. We have an obligation to our profession and our own future sanity to help you become a good programmer, and doing your coursework for you isn't going to do that.

And please don't insult our intelligence by claiming that it isn't a class assignment. It's very easy to spot one, and we have a lot of practice at it. Trust me on this.

Now, if you actually don't know how to create a program that fits the requirements... hmmmn. Reading the book is definitely called for. As is speaking to the professor; while some can be a--holes about office hours, most are more than willing to give extra help, if only to keep their class grades from slipping to the point where they get re-assigned to teach remedial basketweaving.

i am sorry though i did not put my question right i am having difficulties to do the loop on how the program will check whether the entered num is 0 or negative...

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x=2;
    int num;
    printf("enter a value: ");
    scanf("%d", &num);
    while((num%x!=0)&(num!=x)){
        printf("Enter another number: ");
        scanf("%d", &num);

    }


    system("pause");
    return 1;

}

sample of my codes

Your code?

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x=2;
    int num;
    printf("enter a value: ");
    scanf("%d", &num);
    while((num%x!=0)&(num!=x)){
        printf("Enter another number: ");
        scanf("%d", &num);

    }


    system("pause");
    return 1;

}

if ( num <= 0 ) will check if your input is zero or smaller(negative).

The program would be better if you used a do loop instead of while loop. And you don't need to use the mod % operator in this program like you did in your other program.

start of loop
   display prompt to enter a number
   call scanf() to get the number in variable named N.
if number is <= 0 then go back to start of loop

print the sum of all numbers from 1 to N.  You need another loop for this
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num, i;
    int sum=0;
    do{
        printf("enter a number: ");
        scanf("%d", &num);
    }
    while(num<=0);
    if(num<=0){
        printf("enter another value: ");
        scanf("%d", &num);
        }
    for(i=1;i<num;i++){
        sum=sum+num;
        }
    printf("%d", sum);

    system("pause");
    return 1;

}

i end up with this final code :)

will the above code display the sum from 1 to num?

Run the program and find out if it works correctly or not.

line 11 has a semicolon at the end -- remove the semicolon

The if statement on line 12 is not needed so you should delete it. The while should look like this:

while( <condition here> )
{
   // code here
}

Your for loop on line 16 should look like:

for( i=1; i<=num; i++)
{
    sum=sum+num;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.