Write a program that reads a character on the keyboard and display its numeric code in binary notation.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>

void main()
{
	//Solution 1
	int C, I;
	C = getchar();
	I = 128;
	while (I >= 1)
	{
		printf("%i ", C / I);
		C %= I;
		I /= 2;
	}

	//Solution 2
	int C, I;
	C = getchar();
	for (I = 128; I >= 1; I /= 2)
	{
		printf("%i ", C / I);
		C %= I;
	}

	//Solution 3
	int C, I;
	C = getchar();
	for (I = 128; I >= 1; C %= I, I /= 2)
		printf("%i ", C / I);

	//Solution 4
	int C, I;
	for (C = getchar(), I = 128; I >= 1; printf("%i ", C / I), C %= i, i /= 2);

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Number of occurrence of a character in a string