Monday, March 30, 2009

Calculate sum of first and fourth(last) digit of four digit number

/*If a four digit number is input through the keyboard,write a program to obtain the sum of first & fourth(last) digit of the number*/

#include <stdio.h>
int main(void)
{
int num,temp = 0,a ;
printf("Enter a Four digit +ve number : ");
scanf("%d",&num);
if (( num <= 9999) && ( num > 0 ))
{
printf("\nYou entered %d Number ",num);
a = num / 1000;
printf("\n First digit is %d",a);
temp = a ;
a = num % 10;
printf("\n Fourth digit is %d",a );
temp = temp + a;
printf("\nSum of 1st & fourth digit is %d",temp);
}
else
printf("Pl. enter valid 4 digit +ve number");

return 0;
}

No comments: