The sum of first and last digit for a number can be calculated if we use the below mentioned algorithm in C programming language.
Algorithm
Refer an algorithm given herewith −
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
Example
Following is the C program to find the sum of first and last digit for a number −
#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; }
Output
You will see the following output −
enter any number:1242353467 sum of first and last digit is:8
Following is a C program to calculate sum all elements in an array −
Example
#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); }
Output
You will see the following output −
Sum of elements in the array is : 150