我需要一个输出,例如如果数字是 5,则输出应该是 5 个二进制数直到 5,但从 1 开始,下面给出了示例。不使用数组即可完成
输入:5
输出:1,10,11,100,101
但我得到了
输出像
这输入=5
输出=1,11,11,111,111
这个错误是因为错误的指令吗
任何提示或解决方案
#include <stdio.h>
#include <math.h>
void binary_number(int n)
{
int bin_n = 0;
int i = 0;
while (n > 0)
{
bin_n = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
printf("%d", bin_n, j);
printf("\n");
}
void binary_number_generation(int n)
{
for (int i = 0; i <= n; i++)
{
binary_number(i);
}
}
int main()
{
int n;
printf("Enter the end value:");
scanf("%d", &n);
printf("\nBinary Number from 1 to %d:", n);
printf("\n");
binary_number_generation(n);
}
这里我有一个新代码 idk 我做了什么,但它确实给出了结果
#include <stdio.h>
int main()
{
int n,i=0,bin=0,plc,dig=0,j;
printf("Enter the n value : ");
scanf("%d",&n);
printf("\nBinary numbers from 1 to %d : ",n);
for(i=0;i<=n;i++)
{
plc=0;bin=0;dig=0;
for(plc=1,j=i;j>0;j=j/2)
{
dig=j%2;
bin=bin+(dig*plc);
plc=plc*10;
}
printf("%d\n",bin);
}
}