使用C语言中除法和取模运算符查找首位和末位数字之和
问题
如果一个四位数字通过键盘输入,那么如何编写C程序来获取该数字的首位和末位数字之和?
解决方案
在这个程序中,我们将在运行时获取一个四位数字,并尝试使用以下逻辑找到该四位数字的首位和末位数字之和:
a=n%10; b=n/1000; result = a + b;
让我们应用此逻辑来查找四位数字的首位和末位数字之和:
示例
以下是使用除法和取模运算符查找首位和末位数字之和的C程序:
#include<stdio.h>
main(){
int n,a,b,result;
printf("Enter a four digit number: ");
scanf("%d",&n);
a=n%10;
b=n/1000;
result = a + b;
printf("After adding first and last digit is %d", result);
getch();
}输出
当执行上述程序时,它会产生以下结果:
Enter a four digit number: 2345 After adding first and last digit is 7
程序
以下是针对六位数字的C程序,并尝试查找首位和末位数字之和:
#include<stdio.h>
main(){
int n,a,b,result;
printf("Enter a six digit number: ");
scanf("%d",&n);
a=n%10;
b=n/100000;
result = a + b;
printf("After adding first and last digit is %d", result);
getch();
}输出
当执行上述程序时,它会产生以下结果:
Enter a six digit number: 346713 After adding first and last digit is 6
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP