用C语言求一个数字的第一位和最后一位数字之和
如果我们在C编程语言中使用下面提到的算法,可以计算一个数字的第一位和最后一位数字的总和。
算法
请参阅此处给出的算法-
START Step 1: Declare no, sum variables of type int Step 2: Read a number at runtime Step 3: Compute sum=no%10 Step 4: While loop no>9 No=no/10 Step 5: Compute sum=sum+no; Step 6: Print sum STOP
示例
以下是用于查找数字的第一位和最后一位数字之和的C程序-
#include输出结果int main(){ unsigned int no,sum; printf("输入任意数字:"); scanf("%u",&no); sum=no%10; while(no>9){ no=no/10; } sum=sum+no; printf("sum of first and last digit is:%d",sum); return 0; }
您将看到以下输出-
输入任意数字:1242353467 sum of first and last digit is:8
以下是一个计算数组中所有元素总和的C程序-
示例
#include输出结果void main(){ //Declaring the array - run time// int array[5]={10,20,30,40,50}; int i,sum=0,product=1; //Reading elements into the array// //For loop// for(i=0;i<5;i++){ //Calculating sum and product, printing output// sum=sum+array[i]; } //Displaying sum // printf("Sum of elements in the array is : %d\n",sum); }
您将看到以下输出-
Sum of elements in the array is : 150