Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to write in C in info class a program that tells if a num is even, odd or is equal to 0. I wrote this :

#include <stdio.h>
int main() 
{
    int num;
    scanf("%d", &num);
    if(num=0)
        printf("The num is %d" ,num);
    if(num % 2 == 0)
        printf("The num %d is even.", num);
    else
        printf("The num  %d is odd.", num);
    return 0;
}


And i get the error "
error: suggest parentheses around assignment used as truth value 
" I m new to C and i dont understand cause it have parantheses so why keeps asking me for them

What I have tried:

i tried all paranthesis doesnt leave me alone this error
Posted
Updated 31-Oct-22 4:24am

1 solution

You have used
C++
if(num=0)
instead of
C++
if(num==0)

Edit:
You probably want an else if in there as well
C++
#include <stdio.h>
int main() 
{
    int num;
    scanf("%d", &num);
    if(num==0) {
        printf("The num is %d", num);
    } else if(num % 2 == 0) {
        printf("The num %d is even.", num);
    } else {
        printf("The num  %d is odd.", num);
    }
    return 0;
}
 
Share this answer
 
v3
Comments
Bogdan Alexandru Oct2022 31-Oct-22 10:58am    
yea it was ==0.thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900