Jump to content

Need help with school task (C PROGRAMMING)

blackviking03

So i have two tasks that i can't seem to get them to work properly.

(we need to use the while command because we are learning the while one currently)

 

(1.)  You can enter as many positive numbers before entering a negative number, the program needs to print all numbers that can be divided with 7 and the ones that are higher than the number 30.

 

This is my code:

 

#include <stdio.h>
int number;
void main()
{
printf("Enter number:"); scanf("%d",&number);
while(number>0)
{
printf("\n Enter number:"); scanf("%d",&number);
if(number>0&&number>30)
printf("\n %d",number); // - It doesn't print the numbers, just ends the program.
}
}

 

(2.)  Enter three-digit numbers while the sum of digits is odd and higher than 12, print how many numbers are there.

This is my code:

#include <stdio.h>
int xyz,x,y,z,sum,number=0;
void main()
{
printf("Enter a three digit number:");
scanf("%d",&xyz);
x=xyz/100;
y=(xyz%100)/10;
z=xyz%10;
sum=x+y+z;
while(sum%2==1&&sum>12)
number++;
printf("\n Total number of numbers is %d",number); // - It always says ZERO
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

42 minutes ago, blackviking03 said:

So i have two tasks that i can't seem to get them to work properly.

(we need to use the while command because we are learning the while one currently)

 

(1.)  You can enter as many positive numbers before entering a negative number, the program needs to print all numbers that can be divided with 7 and the ones that are higher than the number 30.

 

This is my code:

 

#include <stdio.h>
int number;
void main()
{
printf("Enter number:"); scanf("%d",&number);
while(number>0)
{
printf("\n Enter number:"); scanf("%d",&number);
if(number>0&&number>30)
printf("\n %d",number); // - It doesn't print the numbers, just ends the program.
}
}

 

(2.)  Enter three-digit numbers while the sum of digits is odd and higher than 12, print how many numbers are there.

 

This is my code:

#include <stdio.h>
int xyz,x,y,z,sum,number=0;
void main()
{
printf("Enter a three digit number:");
scanf("%d",&xyz);
x=xyz/100;
y=(xyz%100)/10;
z=xyz%10;
sum=x+y+z;
while(sum%2==1&&sum>12)
number++;
printf("\n Total number of numbers is %d",number); // - It always says ZERO
}

Ok , this is the first one:


 

#include <stdio.h>
void main()
{
int number;
do{
    printf("Enter a number: ");
    scanf("%d", &number);
    if(!(number%7) || number > 30)
        printf("%d \n", number);
}while(number > 0);

}

 

For the second one i really don't understand what the program should do, english is not my main language. What does it mean with print how many numebrs are there? If you have some questions ask no problem!

 

Edit: Does it mean how many digits the number actually have? So like, 15 has 2, 7 has one and 156 has 3?

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Critaek said:

Ok , this is the first one:

 

#include <stdio.h>
void main()
{
int number;
do{
    printf("Enter a number: ");
    scanf("%d", &number);
    if(!(number%7) || number > 30)
        printf("%d \n", number);
}while(number > 0);

}

 

For the second one i really don't understand what the program should do, english is not my main language. What does it mean with print how many numebrs are there? If you have some questions ask no problem!

 

Edit: Does it mean how many digits the number actually have? So like, 15 has 2, 7 has one and 156 has 3?

You have to enter a three digit number (example 917) and the sum of it has to be ODD and HIGHER than 12 and the program has to count the numbers there are from the numbers i type in that are meeting the requirements (ODD sum & sum has to be higher than 12) and will print a number of how many numbers like that are there.

For example if i type 917 (odd sum of the digits) it should let me enter a number again, and if it isn't an odd number it will end the sequence and print how many numbers that meet the conditions are there:

for example

if i type in 917

it should let me enter another number and if i put 523 (its digits are added and give number 10) it should print there is only 1 number that is odd and higher than 12 (917)

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, blackviking03 said:

You have to enter a three digit number (example 917) and the sum of it has to be ODD and HIGHER than 12 and the program has to count the numbers there are from the numbers i type in that are meeting the requirements (ODD sum & sum has to be higher than 12) and will print a number of how many numbers like that are there.

For example if i type 917 (odd sum of the digits) it should let me enter a number again, and if it isn't an odd number it will end the sequence and print how many numbers that meet the conditions are there:

for example

if i type in 917

it should let me enter another number and if i put 523 (its digits are added and give number 10) it should print there is only 1 number that is odd and higher than 12 (917)

Ok, got it, here it is:

 

#include <stdio.h>
void main ()
{
    int xyz, x, y, z, sum, number=0, i;
    do{
        printf("Enter a three digit number: ");
        scanf("%d", &xyz);
        x = xyz / 100;
        y = (xyz % 100) / 10;
        z = xyz % 10;
        sum = x + y + z;
        
        if(sum%2 && sum > 12){
            i = 1;
            number++;
        }
        else
            i = 0;
    }while(i);
    
    printf("Total number of numbers is %d", number);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Critaek said:

Ok, got it, here it is:

 

#include <stdio.h>
void main ()
{
    int xyz, x, y, z, sum, number=0, i;
    do{
        printf("Enter a three digit number: ");
        scanf("%d", &xyz);
        x = xyz / 100;
        y = (xyz % 100) / 10;
        z = xyz % 10;
        sum = x + y + z;
        
        if(sum%2 && sum > 12){
            i = 1;
            number++;
        }
        else
            i = 0;
    }while(i);
    
    printf("Total number of numbers is %d", number);
}

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, blackviking03 said:

Thanks!

No problem, just one thing, when you are declaring the variables, do it inside the function. If you declare them outside, they are global variables, it works, but if you have to do big programs you will ran out of names pretty soon, and probably make some mistakes. Bye!

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>
int number; //Should be within main
void main() //It might be me, but I am a fan of using int main and returning 0...in case you ever need to return an error code
{
	printf("Enter number:"); scanf("%d",&number); //You are asking for and reading the number initially.  This would benefit from a do while loop
    //If you don't want to use a do while loop and keep as is, then you need to initialize your number variable to greater than 0
    //number  = 1;
	while(number>0) //do
	{
		printf("\n Enter number:"); scanf("%d",&number);
		if(number>0&&number>30) //number > 0 means nothing when you have number > 30.  You need to check if it is divisible by 7 and greater than 30
		printf("\n %d",number); //Ran on mine, it runs correctly
	} //while(number > 0);
}

I'm not going to provide you with an answer, but will tell you what should be adjusted and where there might be errors

 

int xyz,x,y,z,sum,number=0; //Inside the main.  Global variables should be used sparingly (only when they need to be globals)
void main()
{
	//The next section is good, you did the math properly.  No complaints, until after sum
	printf("Enter a three digit number:");
	scanf("%d",&xyz);
	x=xyz/100;
	y=(xyz%100)/10;
	z=xyz%10;
  	sum=x+y+z;//So the next bit is an issue
	while(sum%2==1&&sum>12) //Correctly identifies the continue condition, but sum never changes!!!  So you will always do the following
		number++; //If you put in 917 it will keep incrementing number forever....so it will never ask for a new number
	printf("\n Total number of numbers is %d",number); // - It always says ZERO //What did you use as the inputs?
}

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Critaek said:

Ok , this is the first one:


 


#include <stdio.h>
void main()
{
int number;
do{
    printf("Enter a number: ");
    scanf("%d", &number);
    if(!(number%7) || number > 30)
        printf("%d \n", number);
}while(number > 0);

}

 

This will print the negative number too if it's divisible by 7. It's not clear from the question if this is desired behaviour but I thought I'd mention it.

 

It also stops upon entering 0, which is not a negative number.

 

More importantly it breaks completely if you input something that isn't an integer.

 

Here is a version without these problems:

#include <stdio.h>

int main(int argc, char** argv){
        int number = 1;
        int reads;
        char a;
        do{
                printf("Enter number: ");
                while(((reads = scanf("%d%c", &number, &a)) != 2 && reads != EOF) || a != '\n'){
                        printf("Please enter an integer only : ");
                        // read at least one character until the next newline
                        do {
                                reads = scanf("%c", &a);
                        }while(reads != EOF && a != '\n');
                }
                if(number >= 0 && (!(number % 7) || number > 30))
                        printf("%d \n", number);
        } while(number >= 0);
}

the input check is borrowed from here.

 

@blackviking03 there are multiple problems with your first code:

 

1) main() should return an int

2) you don't have a printf command after the first input so it never gets printed no matter what

3) your if statement only checks if the number is larger than 30, not if it's divisible by 7.

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

18 hours ago, Sauron said:

This will print the negative number too if it's divisible by 7. It's not clear from the question if this is desired behaviour but I thought I'd mention it.

 

It also stops upon entering 0, which is not a negative number.

 

More importantly it breaks completely if you input something that isn't an integer.

 

Here is a version without these problems:


#include <stdio.h>

int main(int argc, char** argv){
        int number = 1;
        int reads;
        char a;
        do{
                printf("Enter number: ");
                while(((reads = scanf("%d%c", &number, &a)) != 2 && reads != EOF) || a != '\n'){
                        printf("Please enter an integer only : ");
                        // read at least one character until the next newline
                        do {
                                reads = scanf("%c", &a);
                        }while(reads != EOF && a != '\n');
                }
                if(number >= 0 && (!(number % 7) || number > 30))
                        printf("%d \n", number);
        } while(number >= 0);
}

the input check is borrowed from here.

 

@blackviking03 there are multiple problems with your first code:

 

1) main() should return an int

2) you don't have a printf command after the first input so it never gets printed no matter what

3) your if statement only checks if the number is larger than 30, not if it's divisible by 7.

Your program works correctly, but i can't use it like that because we haven't learned to code like that (professionally), we only learned for,while and will probably go onto do while next week.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, blackviking03 said:

Your program works correctly, but i can't use it like that because we haven't learned to code like that (professionally), we only learned for,while and will probably go onto do while next week.

You can substitute do...while blocks with a normal while block so long as you set the condition to be true for the first run. For example these two are equivalent:

int number;
do{
//stuff
} while(number > 0)
int number = 1;
while(number > 0){
//stuff
}

because, while in the first case the variable number needed to be initialized inside the do...while block, in the second case the variable is already initialized and the loop's first comparison is already true.

 

if you don't need or want to do the input check you can just replace that entire while block with a scanf call:

#include <stdio.h>

int main(int argc, char** argv){
        int number = 1;
        while(number >= 0){
                printf("Enter number: ");
                scanf("%d", &number);
                if(number >= 0 && (!(number % 7) || number > 30))
                        printf("%d \n", number);
        }
}

You should make sure you understand what's going on here and why your original code was wrong.

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

I always forget about do while loops. For me, I 

 

1. Get input for number 

2. While (number >0)

3. Do logic

4. Get next number

5. Close of while loop. 

 

Do while is more efficient in less rewriting code, but just another way of doing it. 

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

×