I'm not able to find the write line for third case. I'm a beginner in programming world. Much appreciated if someone helped me with this Thanks in advance!
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%3==0) {
printf("\n 1");}
else if(n%5==0){
printf("\n 2");}
else if((n%3==0) && (n%5==0)){
printf("\n 3");}
else{
printf("\n 4");
}
return 0;
}
The 3. test condition, else if ((n % 3 == 0) && (n % 5 == 0)), will never be true, because if n % 3 == 0 the 1. test, if (n % 3 == 0), will catch it before. The same goes for the case if n % 5 == 0 with the 2. test, else if (n % 5 == 0).
You need to place if ((n % 3 == 0) && (n % 5 == 0)) at the start instead to proof both sub-expressions before the testing for each sub-expression:
scanf("%d",&n);
if ((n % 3 == 0) && (n % 5 == 0)) {
printf("\n 1");
}
else if (n % 5 == 0) {
printf("\n 2");
}
else if (n % 3 == 0) {
printf("\n 3");
}
else {
printf("\n 4");
}
First you check if the number can be divided by 3, then you check if it can be divided by 5 and then if it can be divided by both.
If it can be divided by both (in other words it can be divided by 15), then it can be divided by 3, so you'll get out of the first loop and print "\n 1".
Change the order of your test. If you put the longest case first, then if both n%3 and n%5 == 0, it will print. Otherwise, it can be one or the other.
if (n%3 == 0 && n%5 == 0)
{
printf("\n 3"); // You might want this to be printf("3\n"); instead
}
else if... // other cases.
Another way to do it if you really want to keep the order is to test on the single cases that they aren't both true, ie:
if (n%3 == 0 && n%5 != 0)
{
// Only a multiple of 3
}
else if (n%5 == 0 && n%3 != 0)
{
// Only a multiple of 5
}
else if (n%3 == 0 && n%5 == 0)
{
// Multiple of 3 and 5
}
I'm not able to find the write line for third case.
else if((n%3==0) && (n%5==0))
3rd line never true as first if(n%3==0) would have also been true and code would have executed that block.
Simplify with
// v------v 1 for multiple of 3
// v------v 2 for multiple of 5
int m15 = (n%3 == 0)*1 + (n%5 == 0)*2;
if (m15 == 0) m15 = 4;
printf("\n %d", m15);`
Related
I am trying to make this short program work, but it gives a message of "expect expression" in the following part:
else
{
printf("program error!");
}
It looks like I am not using the if-elseif-else correctly. I've searched the web and found that the format is how I have it. Please help.
#include <stdio.h>
int main( )
{
double height, weight, bmi;
int bmievalcode;
while( 1 )
{
/* --> add code to input weight and height between here */
printf("Please enter weight in pounds and height in inches\n");
scanf("%lf %lf", &weight, &height);
bmi = (weight * 703.0)/(height * height);
bmievalcode = -1;
if (bmi < 18.5)
{
bmievalcode = 1;
}
else if (bmi >= 18.5 && bmi <25.0)
{
bmievalcode = 2;
}
else if (bmi >=25.0 && bmi < 30.0)
{
bmievalcode = 3;
}
else if (bmi >=30.0)
{
bmievalcode = 4;
}
/* bmievalcode:
* 1 = underweight
* 2 = normal
* 3 = overweight
* 4 = obese
*/
if (bmievalcode == 1 || bmievalcode == 2 || bmievalcode == 3 || bmievalcode == 4)
printf("bmi = %6.1lf, evaluation is ",bmi);
{
if (bmievalcode == 1)
printf("underweight");
else if (bmievalcode == 2)
printf("normal");
else if (bmievalcode == 3)
printf("overweight");
else if (bmievalcode == 4)
printf("obese");
printf(".\n");
}
else
{
printf("program error!");
}
}
return 0;
}
Your code is wrong here:
if (bmievalcode == 1 || bmievalcode == 2 || bmievalcode == 3 || bmievalcode == 4)
printf("bmi = %6.1lf, evaluation is ",bmi);
{
if (bmievalcode == 1)
printf("underweight");
else if (bmievalcode == 2)
printf("normal");
else if (bmievalcode == 3)
printf("overweight");
else if (bmievalcode == 4)
printf("obese");
printf(".\n");
}
else
{
printf("program error!");
}
The {} block isn't part of the if, it's separate. Then you have an else, but it's not directly after an if statement, which causes the error you see.
Move the line printf("bmi = %6.1lf, evaluation is ",bmi); to be after the opening {.
The statement directly after the if is what is affected by the condition. If this could compile, only that first printf would be conditionally executed. The curly-brace block would always run.
In this case, GCC is a little more helpful than Clang, telling you error: 'else' without a previous 'if'
I created a program that calculates the divisibility by 3 and 5. If divisible by 3 print 'CS' and if divisible by 5 print 'CS1714'. If divisible by both print 'CS1714'. If not divisible by 3 or 5 print 'ERROR'.
My code executes properly. However is it possible for the Boolean && to break if say both values are false. Also why does the code print 'CSERROR' when input is 98988?
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if((userInput % 3) == 0){
printf("CS");
}
if((userInput % 5) == 0){
printf("1714");
}
else if(!((userInput % 5) == 0 && (userInput % 3) == 0)){
printf("ERROR");
}
return 0;
}
When the input is 98988 it is printing CSERROR because the number 98988 is divisible by 3 and that is why the first if condition is true and thus it prints CS, and then you gave another if where you are checking if the number is divisible by 5 or not, if the number is divisible by 5 then you print 1714, else you print ERROR, as 98988 is not divisible by 5 that is why it goes to the else part and prints ERROR (The last else will always true if your second if is false. So, here is a logical error). Previously it printed CS and now it printed ERROR, combinedly you are seeing CSERROR.
One is need to be clear that, the if-else blocks structures are like below, and it started implementing from a if to go further until finds a true condition or else.
if(condition){
}
else if(condition){
}
else{
}
You should check both conditions (when the number is divisible by 3 and also divisible by 5) first, then check whether it is divisible by 3 , if not then check by 5. And finally if all are false that means it is not divisible by 3 and also not divisible by 5, so print error.
The code should be like:
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if(((userInput % 5) == 0 && (userInput % 3) == 0)){
printf("CS1714");
}
else if((userInput % 3) == 0){
printf("CS");
}
else if((userInput % 5) == 0){
printf("1714");
}
else{
printf("ERROR");
}
return 0;
}
Input: 98988
Output: CS
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if((userInput % 3) == 0){
printf("CS");
}
if((userInput % 5) == 0){
printf("1714");
}
else if(((userInput % 5) != 0 && (userInput % 3) != 0)){
printf("ERROR");
}
return 0;
}
Say I have a few conditions inside a if statement:
if(x < 0 || x > 10 || x == 5)
{
}
If either x is greater than 10 or less than 0 or equal to 5, I want the program to stop working.
if(x < 0 || x > 10 || x == 5)
{
stop(); // PSEUDO CODE
}
However, I want the program to say something depending on which condition was true. Something like this:
if(x < 0 || x > 10 || x == 5)
{
if(x < 0)
{
printf("your number was less than 0");
}
if(x > 10)
{
printf("your number was greater than 10");
}
if(x == 5)
{
printf("wow, your number is equal to 5!");
}
stop(); // PSEUDO CODE
}
That's a horrible way of doing it because it unnecessarily checks for the conditions twice; how can I do the same in a more efficient way?
Why make things complicated? Just remove the outer check, and put stop() inside both of the inner ones:
if(x < 0)
{
printf("your number was less than 0");
stop();
}
if(x > 10)
{
printf("your number was greater than 10");
stop();
}
You can even use an else if if you want.
Save the boolean result in a variable and reuse it inside. Redundancy is ok sometimes. Just check/count the number of executions if it's possible to be lessen down.
bool isXNegative = x < 0;
bool isXMoreThan = x > 10;
bool isXFive = (x == 5);
//for specific conditions
if(isXNegative)
{
printf("your number was less than 0");
}
if(isXMoreThan)
{
printf("your number was greater than 10");
}
if(isXFive)
{
printf("wow, your number is equal to 5!");
}
//for combination of the conditions.
if(isXNegative || isXMoreThan || isXFive)
{
//common code here for all the conditions.
stop(); // PSEUDO CODE
}
//else if(another condition combination here...){...}
//else {...}
You can set a flag in the failure cases, then check the flag afterward to do the cleanup.
int do_stop = 0;
if(x < 0)
{
printf("your number was less than 0");
do_stop = 1;
}
if(x > 10)
{
printf("your number was greater than 10");
do_stop = 1;
}
if(x == 5)
{
printf("wow, your number is equal to 5!");
do_stop = 1;
}
if (do_stop) {
stop();
}
Assuming you do want to be complicated, save some keystrokes and take advantage of short-circuiting, you can assign expression results to variables in the if condition like so:
int c1, c2, c3;
if ((c1 = x < 0) || (c2 = x > 10) || (c3 = x == 5)) {
if (c1) {
printf("your number was less than 0\n");
} else if (c2) {
printf("your number was greater than 10\n");
} else {
printf("wow, your number is equal to 5!\n");
}
}
Just be aware that if the condition short-circuits (say x < 0), then the value of c2 and c3 is not initialized.
I don't actually advocate writing your code this way, but this language feature can be useful and I didn't see it mentioned.
As a mandatory preface, I am new to C, and am likely simply missing something extremely obvious. I appreciate any and all time and effort taken to look over my silly problem.
I have a recursive function whose purpose is to print out a large "x" made of smaller x characters, where width is the length of each side of the x. For example, a width of "3" would have the following output:
Shape:
X X
X
X X
Returning.
Where "Returning." prints just before returning to main.
The following function does just this for a width of 1 and 3, but fails to do so with 5, 7, 9, etc.
void Recurse(int left, int right, int flag, int num){
int i;
if(((left && right) == num/2) && (flag == 0)){
for(i=0;i<num;i++){
if (i == (num/2) ){
printf("X");
}
else
printf(" ");
}
printf("\n");
flag = 1;
Recurse(left-1, right+1, flag, num);
}
else if(flag == 0){
for(i=0;i<num;i++){
if((i == left) || (i == right)){
printf("X");
}
else
printf(" ");
}
printf("\n");
Recurse(left+1, right-1, flag, num);
}
else if(flag == 1){
for(i=0;i<num;i++){
if((i == left) || (i == right)){
printf("X");
}
else
printf(" ");
}
printf("\n");
if (((left == 0) && (right == num-1)) && (flag == 1))
printf("\nReturning.\n");
return;
Recurse(left-1, right+1, flag, num);
}
}
The only thing I have in my main function is an initialization of width with some odd int, and a call to the function. I would like to get the code actually... printing correctly prior to cleaning up the logic a bit. Thank you for any help provided.
In
if(((left && right) == num/2) && (flag == 0)){
left && right is a boolean value, probably you wanted
if ((left == num/2) && (right == num/2) && (flag == 0)){
Below is a code I wrote for a dice game called cho han. To input your guess I've used number to represent the words 'odd' and 'even'. Since then I have tried to write it again, but to actually write odd or even in the scanf section, but can't get it to work. Any help would be appreciated :)
//cho-han
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;
printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter '1'. To guess even, enter '2'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");
scanf_s("%d", &guess);
if (guess == 2)
{
printf("\nyour guess is even.\n");
}
if (guess == 1)
{
printf("\nyour guess is odd.\n");
}
if (guess > 2 || guess < 1)
{
printf("\nInvalid guess.\nYou lose!\n");
return (1);
}
printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);
result = x + y;
printf("\ncombined total of both rolls is %d", result);
if (result == 1 || result == 3 || result == 5 || result == 7 || result == 9 || result == 11)
{
printf("\ncombined total of both rolls is odd.\n");
}
else
{
printf("\ncombined total of both rolls is even.\n");
}
if (guess == 1 && result == 1 || guess == 1 && result == 3 || guess == 1 && result == 5 || guess == 1 && result == 7 || guess == 1 && result == 9 || guess == 1 && result == 11)
{
printf("\nYou win!\n");
}
else if (guess == 2 && result == 2 || guess == 2 && result == 4 || guess == 2 && result == 6 || guess == 2 && result == 8 || guess == 2 && result == 10 || guess == 2 && result == 12)
{
printf("\nYou win!\n");
}
else
{
printf("\nYou lose!\n");
}
return 0;
}
You should change scanf_s to scanf
The line if (result == 1 || result == 3 ... could be if (result % 2 == 1) {
You could use strcmp to solve your question
The following code could work:
//cho-han
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void)
{
srand(time(NULL));
int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;
char buf[10];
printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter 'odd'. To guess even, enter 'even'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");
fgets(buf, sizeof buf, stdin);
if (strcmp(buf, "even\n") == 0) {
guess = 2;
printf("\nyour guess is even.\n");
} else if (strcmp(buf, "odd\n") == 0) {
guess = 1;
printf("\nyour guess is odd.\n");
} else {
printf("\nInvalid guess.\nYou lose!\n");
return 1;
}
printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);
printf("\ncombined total of both rolls is %d", x + y);
result = (x + y) % 2;
if (result == 1)
printf("\ncombined total of both rolls is odd.\n");
else
printf("\ncombined total of both rolls is even.\n");
if (guess == result)
printf("\nYou win!\n");
else
printf("\nYou lose!\n");
return 0;
}
You need to change your guess to char type and scanf to capture string.
char guess[256];
scanf("%s", guess);
And then the best way would be to call toupper() and compare with your text using strcmp().