Pages

Thursday, December 22, 2011

Program to find sum of first and last digit of a number of any digit

This program finds the sum of first and last digit of a number that would be of any length


If a number of any digit is input through the keyborad,write a program to obtain the sum of first and last digit of this number.


#include<stdio.h>

void main(){

/*
since integer range is between -32768 to 32767
it will support till 5 digit number only
so for higher digits you have to declare
with long
*/

long firstdigit,sum=0,r,num;
clrscr();

printf("Enter number : ");
scanf("%ld",&num);

sum=sum+num%10; // num%10 gives lastdigit

while(num!=0){
firstdigit=num; // gives first digit
num=num/10;
}

sum=sum+firstdigit;

printf("Sum of first and last digit : %ld",sum);
getch();


}

No comments: