使用 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 <stdio.h>
int main(){
unsigned int no,sum;
printf("enter any number:");
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;
}输出
你将看到如下输出 −
enter any number:1242353467 sum of first and last digit is:8
以下是一个 C 语言程序,用于 计算数组中所有元素的和 −
示例
#include<stdio.h>
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
",sum);
}输出
你将看到如下输出 −
Sum of elements in the array is : 150
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP