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's gravatar image

shuddha7435
347

edited 03 Jul '16, 11:20


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
}
permanent link

answered 03 Jul '16, 12:54

menon's gravatar image

menon
4.7k335

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
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×56
×56

question asked: 03 Jul '16, 11:20

question was seen: 687 times

last updated: 03 Jul '16, 16:14