求解一道简单的C语言题

2024-11-30 03:28:32
推荐回答(5个)
回答1:

一:输入十进制,输出八、十、十六进制。

#include
int main(void)
{
int n;
scanf("%d", &n);
printf("八进制:%o\n", n);
printf("十进制:%d\n", n);
printf("十六进制:%x\n", n);
return 0;
}

例子:
16
八进制:20
十进制:16
十六进制:10
Press any key to continue

二:输入二进制,输出八、十、十六进制

#include
#include
#include
int main(void)
{
char string[128];
long n;
scanf("%s", string);
n = strtol(string, NULL, 2);
printf("八进制:%o\n", n);
printf("十进制:%d\n", n);
printf("十六进制:%x\n", n);
return 0;
}

例子:

00010000
八进制:20
十进制:16
十六进制:10
Press any key to continue

回答2:

先用scanf("%d",a)输入a为十进制
再用printf("&x",a)输出,%x为十六进制,%o为八进制

回答3:

#include "stdio.h"
main()
{int num;
printf("Input a number please:\n");
scanf("%d",&num);
printf("The num in 16 type is %h",num);
}

回答4:

/*
name : deny
date : 2010/4/2
function : exchange the number you want change
*/
#include
void Binary_date (int num,int change_num); /*function prototype of Binary_date(int,int)*/
void Binary_exchange (int number,int x);

int main (void)
{
int li_number;
int li_change_num;

printf("please enter a number you want to exchange and enter a change numebr(q to quit):");
while (scanf("%d%d",&li_number,&li_change_num))
{
while (getchar () != '\n');
printf("the number %d is :",li_number);
Binary_date (li_number,li_change_num); /*call the function of Binary_date (int,int)*/
printf("\n");
printf("please enter a number you want to exchange and enter a change numebr(q to quit):");
}

return 0;
}

void Binary_date (int num,int change_num) /*the function of changing the number to binary,octal,hexdecimal number*/
{
int li_temp,li_changed;

li_temp = num / change_num; /*calcualte the diviece*/
li_changed = num % change_num + '0'; /*calculate the mode*/

if(li_temp >= 1)
Binary_date (li_temp,change_num); /*return to call the "himeself"*/

if (li_changed > '9') /*if the changed number beyond character '9',change it to 'A','B','C'....*/
li_changed += 7;

printf("%c",li_changed); /*printting the character*/
}

/*exchange the decimal number*/

void Binary_exchange (int number,int x)

{

SeqStack ls_temp;

int li_temp = number,r;

int i;

InitSeqStack (&ls_temp);

while (li_temp > 0)

{

r = li_temp%x + '0';

if (r >'9')

r += 7; /*ASCII's exchange*/

Push (&ls_temp,r);

li_temp /= x;

}

i = ls_temp.top;

printf("number %d is : ",number);

while (i >= 0)

printf("%c",ls_temp.num[i--]); /*move the index,and printing the date*/

putchar (10);

}

一个函数是基于递归写的,一个是基于栈调用写的,不知道能否帮到你

回答5:

#include
int
main()
{
long
i,j,k;
k=10;
for
(i=1;i<=99;i++)
{
if
(i==k)
k*=10;
j=i*i;
if(j%k==i)
printf("%ld\t%ld\n",i,j);
}
}