Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to write a program to print me if the imput is 1,3,5 something..if it is 2,4,6 something else..i wrote this :

#include <stdio.h>
int main() {
    int a , b , c ,d ;
    scanf("%d %d %d %d" , &a ,&b , &c , &d);
    if{a=1,3,5}
    printf("American feminine citizen");
    if{b=2,4,6}
    printf("Foreign citizen masculine");


My question is if i wrote correctly those values in those parantheses .

What I have tried:

I searched the internet but i find solution for another similar questions
Posted
Updated 10-Nov-22 5:26am

The statement :
if{a=1,3,5}
is not valid C syntax. You probably want any of those values to be a true condition so you need to an OR comparison using the || operator. That will look like this :
C
if( ( a == 1 ) || ( a == 3 ) || ( a == 5 ) )
I inserted spaces and parenthesis so it is clearer and does not rely on precedence.
 
Share this answer
 
Comments
Bogdan Alexandru Oct2022 10-Nov-22 10:54am    
ohh ok.Thank you
Firstly, "=" is an assignment operator, not a comparison. Equality is tesed for with "==" instead.

Secondly, you need the AND or OR operator: && or ||
a AND b is true only if both conditions a and b are true - otherwise it returns false
a OR b is true only if either condition a and b are true - it returns false only if both are false.
For example:
C
if (a == 1 && b == 2 && c == 3)
   {
   ... 
   }
If this isn't what you meant, then you need to explain in a lot more detail exactly what you need!
 
Share this answer
 
Spend some time reading Elements of C | Microsoft Learn[^] as you seem to be missing the basics of the language.
 
Share this answer
 
There are several options. If you cannot combine numbers to a range, the switch-case statement is recommended.
C
switch(a) {
  case 1:
  case 3:
  case 5:
    printf("American feminine citizen");
    break;
  }
  switch(b) {
   case 2:
   case 4:
   case 6:
    printf("Foreign citizen masculine");
    break;
  }
 
Share this answer
 

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