《C语言程序设计现代方法:第2版 修订版》第四章课后编程题,水平有限,仅供参考。 1
#include <stdio.h>
int main (void)
{
int number,new_number;
printf("Enter a two-digit number:");
scanf("%d",&number);
new_number=(number%10*10)+(number/10);
printf("The reversal is:%3d",new_number);
return 0;
}
2
#include <stdio.h>
int main (void)
{
int number,new_number;
printf("Enter a three-digit number:");
scanf("%d",&number);
new_number=((number%100)%10)*100+(number%100)/10*10+(number/100);
printf("The reversal is:%3d",new_number);
return 0;
}
3
#include <stdio.h>
int main (void)
{
int hundres,tens,ones;
printf("Enter a three-digit number:");
scanf("%1d%1d%1d",&hundres,&tens,&ones);
printf("The reversal is:%d%d%d",ones,tens,hundres);
return 0;
}
4
#include <stdio.h>
int main (void)
{
int number,fr,se,th,fo,fif;
printf("Enter a number between 0 and 32767:");
scanf("%5d",&number);
fr=number/4096;
se=number/512;
th=(number-se*512)/64;
fo=(number-se*512-64*th)/8;
fif=number%8;
printf("In ocral, your number is: %1d%1d%1d%1d%1d",fr,se,th,fo,fif);
return 0;
}
5
#include <stdio.h>
int main (void)
{
int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,first_sum,second_sum,total;
printf("Enter the first 11 digits of a UPC: ");
scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9,&i10,&i11);
first_sum=i1+i3+i5+i7+i9+i11;
second_sum=i2+i4+i6+i8+i10;
total=3*first_sum+second_sum;
printf("Check digit: %d\n",9-((total-1)%10));
return 0;
}
6
#include <stdio.h>
int main (void)
{
int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,first_sum,second_sum,total;
printf("Enter the frist 12 digits of an EAN: ");
scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9,&i10,&i11,&i12);
first_sum=i2+i4+i6+i8+i10+i12;
second_sum=i1+i3+i5+i7+i9+i11;
total=3*first_sum+second_sum;
printf("Check digit: %d\n",9-((total-1)%10));
return 0;
}
|