# How can I find the even or odd value by checking the final character of a string array?

 1 ``````int T,i; char n [101]; scanf("%d", &T); for(i = 1;i <= T; i++) { } return 0; `````` I want to find out the even or odd by checking the final character of the string here. How can I find it? Is there any process? asked 03 Jul '16, 11:20 shuddha7435 34●9

 1 You can try this, ``````char final_char; if( (final_char - '0') % 2 == 0 ) { // The statement [final_char - '0'] convert final_char to int value // Even } else { // Odd } `````` or, ``````char final_char; if( (final_char - '0') & 1 ) { // Odd } else { // Even } `````` answered 03 Jul '16, 12:54 menon 4.7k●4●37 Got this error after running this code error: invalid operands to binary % (have 'char *' and 'int') Help please. { int T,i; char n [101]; scanf("%d", &T); for(i = 1;i <= T; i++) { scanf("%s", &n); ``````if( (n - '0') % 2 == 0 ) { /// The statement [n - '0'] convert final_char to int value printf("even\n"); } else { printf("odd\n"); } `````` } return 0; } (03 Jul '16, 15:45) shuddha7435 (n - '0') এর n এর জায়গায় আপনি যে স্ট্রিং ইনপুট নিচ্ছেন সেটার লাস্ট char ইনপুট দেন । যেমন ঃ n = "12345" হলে (n[4] - '0') হবে । (03 Jul '16, 16:14) menon
 toggle preview community wiki:

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

×57
×56

question asked: 03 Jul '16, 11:20

question was seen: 830 times

last updated: 03 Jul '16, 16:14