1. 程式人生 > >輸出一個數的奇數位、偶數位和二進位制序列

輸出一個數的奇數位、偶數位和二進位制序列

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
void print_num(unsigned int n)
{
	int count = 0;
	int a[32] = { 0 };
	int i = 0;
	while (n)
	{
		a[i++] = n % 2;
		n = n / 2;
		count++;
	}
	printf("奇數序列\n");
	for (i = count; i >= 0; i = i - 2)
	{
		printf("%d", a[i]);
	}
	printf("\n");
	printf("偶數序列\n");
	for (i = count - 1; i >= 0; i = i - 2)
	{
		printf("%d", a[i]);
	}
	printf("\n");
}
int main()
{
	int n = 0;
	printf("獲取一個二進位制序列中所以得偶數位和奇數位,分別輸出二進位制序列:\n");
	printf("請輸入一個數\n");
	scanf("%d", &n);
	print_num(n);
	system("pause");
	return 0;
}