Jump to content

Program doesn't print all the numbers, help.

blackviking03

I have a task for school in which i have to use while command to make one program do certain actions:

 

Allow the person to enter as many numbers that are positive before entering a negative number then, after the negative number has been entered, the program needs to find ALL numbers that are divisible by 7 without residue and the numbers that are divisible by 7 without residue must be HIGHER than 30 so  it has to be liek this if(number%7==0&&number>30)

 

This is my code:
#include <stdio.h>
int x,y;
void main()
{
printf("\n Unesi broj:"); scanf("%d",&x);
while(x>0)
{
printf("\n Unesi broj:"); scanf("%d",&x);
if(x>30&&x%7==0)
y=x;
}
printf("\n %d",y); //- It only prints the last number which is divisible by 7 not all of them, that's my problem.
}

Link to comment
Share on other sites

Link to post
Share on other sites

What was wrong with the answers you got yesterday when you posted the exact same question? 🤔

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sauron said:

What was wrong with the answers you got yesterday when you posted the exact same question? 🤔

 

I wrote thanks because i wasn't home when the person answered so i couldn't check it, but today i checked it and it didn't work.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, blackviking03 said:

I wrote thanks because i wasn't home when the person answered so i couldn't check it, but today i checked it and it didn't work.

1) in that case you can just quote or mention the person in that topic and ask for further help... reposting the same question again from scratch is pointless and bad forum etiquette.

2) I also posted there to explain there were some problems with that person's solution a well as with yours, did you try the code I posted?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sauron said:

1) in that case you can just quote or mention the person in that topic and ask for further help... reposting the same question again from scratch is pointless and bad forum etiquette.

2) I also posted there to explain there were some problems with that person's solution a well as with yours, did you try the code I posted?

I did and it works, but i can't program like that because we haven't learned to code like that. We only learned for,while and will probably learn do while next week.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...

You've written the printf function out of the while{} loop area.

Link to comment
Share on other sites

Link to post
Share on other sites

while(x>0)
{
Xxxxxxxxx
}
printf("\n %d",y);

 

change into this

while(x>0)
{
printf("\n Unesi broj:"); scanf("%d",&x);
if(x>30&&x%7==0)
y=x;

printf(....

 

 

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×