+ 2

How To find the given no is odd or even using point to structure

C language

4th Apr 2020, 11:17 AM
Bharathkannan R
Bharathkannan R - avatar
4 Answers
+ 1
#include <stdio.h> struct evenOdd{ int n; }; int main() { struct evenOdd *num, nums; num = &nums; printf("Enter the number: \t"); scanf("%d", &num->n); if(num->n % 2 == 0){ printf("even"); } else{ printf("odd"); } return 0; }
6th Apr 2020, 5:31 AM
Akash Moradiya
Akash Moradiya - avatar
+ 2
Thank you
6th Apr 2020, 1:43 PM
Bharathkannan R
Bharathkannan R - avatar
+ 2
@Brett Dahmer thanks for answer
19th Apr 2020, 4:36 AM
Bharathkannan R
Bharathkannan R - avatar
+ 1
You could look at the numbers binary form using the & operator instead of performing a modulus operation (which takes more time) //below looks at the least significant bit or the bit on the far right if it is a 1 then it is always odd, if 0 then it is even. if(num->n & 1) { printf("odd"); } else printf("even"); return 0;
15th Apr 2020, 7:18 PM
Brett Dahmer
Brett Dahmer - avatar