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\n",sum);
} 출력
다음 출력이 표시됩니다 -
Sum of elements in the array is : 150