C语言程序设计现代方法第四章课后习题

本文展示了五个C语言程序,用于处理数字输入并进行特定操作:第一题和第二题分别将两位数拆分为个位和十位;第三题实现不按回车输入三个数并反转顺序输出;第四题将十进制数转换为八进制;第五题和第六题计算UPC和EAN条形码的校验码。这些练习涵盖了基本的输入输出、数字处理和简单的数学运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题
#include <stdio.h>
#include)
int main(void)
{
int a;
printf(“Enter a two-digit number:”);
scanf_s("%d", &a);
printf(“THE : %d%d”,a%10,a/10);
system(“pause”);
return 0;
}
第二题
#include <stdio.h>
#include)
int main(void)
{
int a;
printf(“Enter a two-digit number:”);
scanf_s("%d", &a);
printf(“THE : %d%d%d”,a%10,(a/10)%10,(a/10)/10);
system(“pause”);
return 0;
}
第三题
#include <stdio.h>
#include)
int main(void)
{
int a,b,c;
printf(“Enter a two-digit number:”);
scanf_s("%1d%1d%1d",&a,&b,&c);//////通过限定位宽来实现不按回车输入三个数
printf(“THE reversal: %1d%1d%1d”,c,b,a);
system(“pause”);
return 0;
}
第四题
#include <stdio.h>
#include)
int main(void)
{
int a;
printf(“Enter a number between 0 and 32767:”);
scanf_s("%d",&a);
printf(“in octal,your number is: %o”,a);
system(“pause”);
//或者:
//int num10, num8;

//printf("enter a number between 0 and 32767: ");
//scanf("%d", &num10);

//int a, b, c, e, d;
//a = num10 % 8;
//b = (num10 / 8) % 8;
//c = (num10 / 8 / 8) % 8;
//d = (num10 / 8 / 8 / 8) % 8;
//e = (num10 / 8 / 8 / 8 / 8) % 8;

//printf("in octal, your number is: %d%d%d%d%d", e, d, c, b, a);

return 0;

}
第五题
#include <stdio.h>
#include)
int main(void)
{
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,b,bb,c;
printf(“Enter the first 11digits of a UPC:”);
scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11);
b = a1 + a3 + a5 + a7 + a9 + a11;
bb = a2 + a4 + a6 + a8 + a10;
c = 9-(b * 3 + bb - 1) % 10;
printf(“Check digit: %d\n”,c);
system(“pause”);
return 0;
}
第六题
#include <stdio.h>
#include)
int main(void)
{
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,a12,b,bb,c;
printf(“Enter the first 12 digits of a EAN:”);
scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11,&a12);
bb = a1 + a3 + a5 + a7 + a9 + a11;
b = a2 + a4 + a6 + a8 + a10+a12;
c = 9-(b * 3 + bb - 1) % 10;
printf(“Check digit: %d\n”,c);
system(“pause”);
return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值