Why is the answer f?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why is the answer f??

char x ='a'; cout<< char(x*5); please explain this code, came across in one of the quizes and im not getting why the answer is f. please help

8th Feb 2019, 8:49 AM
Buhle 💃💃💃
Buhle 💃💃💃 - avatar
7 Answers
+ 3
I ran this code and it printed the 'o' with the line in the top. If I did the x+5, I got the 'f'. It multiplies the 'a' character number by 5 then prints it by character.
8th Feb 2019, 8:55 AM
EoflaOE
EoflaOE - avatar
+ 3
thanks for answering but im still confused on this gues i need a full tutorial on chars
8th Feb 2019, 9:00 AM
Buhle 💃💃💃
Buhle 💃💃💃 - avatar
+ 3
chars have corresponding integer numbers, defined on the so called ASCII table. They reach from 0 to 255. Small 'a' is defined to 97. So if you add 5 on it, it will be on place 102, and print f. The letters are assigned consecutively. If you multiply, you will get a value greater than 255. But that is not a problem, the numbers repeat so to say. If you know modulus, it's nothing else than the number mod 256. So e.g. 256 will be 0 again, 257 will be 1, etc. You can learn how it works in this assignment (and comments below it 😉) https://www.sololearn.com/learn/6105/?ref=app
8th Feb 2019, 9:21 AM
Matthias
Matthias - avatar
+ 2
Oviya arumugam Not in this case, since there is also an extended ASCII set. 97*5 = 485 485 mod 256 = 229 greek sigma (But I think there was a plus in the quiz, otherwise it would be too complicated to know by heart) One can easily check the table by printing it in a loop: https://code.sololearn.com/cK9RQiYmaKoB/?ref=app If you let it run double the length (i<512), you see the repeating pattern and small sigma on place 229 and 485.
8th Feb 2019, 1:44 PM
Matthias
Matthias - avatar
+ 1
You are welcome
8th Feb 2019, 9:12 AM
EoflaOE
EoflaOE - avatar
0
The ASCII value range is from 0-128 The multiply of a*5 is 485 .It does not have ASCII value
8th Feb 2019, 12:02 PM
Oviya arumugam
Oviya arumugam - avatar
0
The output is black screen no output will show
8th Feb 2019, 12:04 PM
Oviya arumugam
Oviya arumugam - avatar